# Deploy Edge Functions to Supabase

## Problem
The Flutter app is getting "HTTP 401: Invalid JWT" because the Edge Functions either:
1. Haven't been deployed yet, OR
2. Are deployed but don't have environment variables set

## Prerequisites

### 1. Install Supabase CLI

**Option A: npm (if you have Node.js)**
```bash
npm install -g supabase
```

**Option B: Chocolatey (Windows)**
```powershell
choco install supabase
```

**Option C: Direct download**
https://github.com/supabase/cli/releases

### 2. Login to Supabase CLI

```bash
supabase login
```

This will open a browser to generate an access token.

## Deployment Steps

### Step 1: Link Project

```bash
cd c:\Webs\Sojorn
supabase link --project-ref zwkihedetedlatyvplyz
```

Enter your database password when prompted.

### Step 2: Deploy All Functions

```bash
# Deploy all Edge Functions at once
supabase functions deploy signup
supabase functions deploy profile
supabase functions deploy publish-post
supabase functions deploy publish-comment
supabase functions deploy appreciate
supabase functions deploy save
supabase functions deploy follow
supabase functions deploy block
supabase functions deploy report
supabase functions deploy feed-personal
supabase functions deploy feed-sojorn
supabase functions deploy trending
supabase functions deploy calculate-harmony
```

**Or deploy all at once:**
```bash
for func in signup profile publish-post publish-comment appreciate save follow block report feed-personal feed-sojorn trending calculate-harmony; do
  supabase functions deploy $func --no-verify-jwt
done
```

### Step 3: Set Environment Variables (Critical!)

The Edge Functions need these environment variables:

```bash
# Get your service role key from:
# https://app.supabase.com/project/zwkihedetedlatyvplyz/settings/api

supabase secrets set \
  SUPABASE_URL=https://zwkihedetedlatyvplyz.supabase.co \
  SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inp3a2loZWRldGVkbGF0eXZwbHl6Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3Njc2Nzk3OTUsImV4cCI6MjA4MzI1NTc5NX0.7YyYOABjm7cpKa1DiefkI9bH8r6SICJ89nDK9sgUa0M \
  SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inp3a2loZWRldGVkbGF0eXZwbHl6Iiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc2NzY3OTc5NSwiZXhwIjoyMDgzMjU1Nzk1fQ.nfXAU7m5v5PyaMJSEnwOjXxKnTiwpOWM_apIh91Rtfo
```

### Step 4: Verify Deployment

```bash
# List deployed functions
supabase functions list

# Test a function
curl -i --location --request GET 'https://zwkihedetedlatyvplyz.supabase.co/functions/v1/feed-sojorn?limit=10' \
  --header 'Authorization: Bearer YOUR_USER_JWT' \
  --header 'apikey: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inp3a2loZWRldGVkbGF0eXZwbHl6Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3Njc2Nzk3OTUsImV4cCI6MjA4MzI1NTc5NX0.7YyYOABjm7cpKa1DiefkI9bH8r6SICJ89nDK9sgUa0M'
```

## Alternative: Deploy via Supabase Dashboard

If CLI doesn't work, you can deploy manually:

1. Go to: https://app.supabase.com/project/zwkihedetedlatyvplyz/functions
2. Click "Create a new function"
3. For each function:
   - Name: `signup` (etc.)
   - Copy code from `supabase/functions/signup/index.ts`
   - Click "Deploy"
4. Set environment variables:
   - Go to Settings > Edge Functions > Environment Variables
   - Add: `SUPABASE_URL`, `SUPABASE_ANON_KEY`, `SUPABASE_SERVICE_ROLE_KEY`

## Troubleshooting

### Error: "supabase: command not found"
Install Supabase CLI (see Prerequisites above)

### Error: "Failed to deploy function"
Check that:
1. You're logged in: `supabase login`
2. Project is linked: `supabase link --project-ref zwkihedetedlatyvplyz`
3. You have permissions on the project

### Error: "Missing SUPABASE_URL"
Run Step 3 to set environment variables

### Functions deployed but still getting 401
1. Check environment variables are set:
   ```bash
   supabase secrets list
   ```
2. Make sure secrets match `.env` file
3. Try redeploying after setting secrets

## Quick Check: Are Functions Deployed?

Visit these URLs in your browser (should show CORS error, not 404):

- https://zwkihedetedlatyvplyz.supabase.co/functions/v1/feed-sojorn
- https://zwkihedetedlatyvplyz.supabase.co/functions/v1/profile

**If you get 404**: Functions not deployed
**If you get CORS or auth error**: Functions deployed (good!)
**If you get JSON error response**: Functions deployed and working!

## After Deployment

1. Restart Flutter app
2. Try signing in
3. JWT errors should be gone

The "Invalid JWT" error should change to a more specific error (or success!) once functions are deployed with correct environment variables.
