# Production Deployment - n8n Webhook Routing

## Prerequisites

Ensure your production environment has:
- `docker-compose.yml` and `docker-compose.local.yml` updated with new n8n webhook routing
- `supabase/kong.yml` updated with n8n webhook service
- `frontend/admin.js` updated with new sendOrderEmailDirect function
- Production domain configured (e.g., `your-domain.com`)

## Deployment Steps

### 1. Update Production Config

Edit `frontend/config.js` and replace `localhost:55521` with your production domain:

```javascript
window.MCCARS_CONFIG={
  SUPABASE_URL:"https://your-domain.com",
  SUPABASE_ANON_KEY:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  N8N_WEBHOOK_URL:"https://your-domain.com/webhook/manual-email-send"
};
```

Replace:
- `your-domain.com` with your actual production domain
- Keep the same ANON_KEY value

### 2. Optional: Configure WEBHOOK_DOMAIN

If you want n8n to know its public webhook URL (for n8n UI display), set environment variable:

```bash
export WEBHOOK_DOMAIN=https://your-domain.com
```

This tells n8n that webhooks are accessible at `https://your-domain.com/webhook/*` from the internet.

### 3. Deploy Updated Files

Push these files to production:
- `supabase/kong.yml` (updated with n8n webhook service)
- `docker-compose.yml` (updated WEBHOOK_URL variable syntax)
- `frontend/config.js` (updated with production domain)
- `frontend/admin.js` (updated sendOrderEmailDirect function)

### 4. Restart Stack on Production Server

```bash
# On production host
cd /mnt/user/appdata/mc-cars  # or your deployment path

# Pull latest code
git pull origin dev  # or your deployment branch

# Restart with new config
docker-compose down
docker-compose up -d --build

# Verify services are healthy
docker-compose ps
```

### 5. Verify Webhook Routing

Test webhook from production domain:

```bash
curl 'https://your-domain.com/webhook/manual-email-send' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'sales_order_id=YOUR_ORDER_ID'
```

Expected response: 200 OK with n8n workflow result

## Network Setup

Kong must be accessible from the internet:
- **Port 55521** exposed via reverse proxy (nginx/Apache) or firewall rule
- Domain DNS points to production server
- SSL certificate configured (recommended to use Kong's 8443 port with cert)

## Troubleshooting

### "Failed to fetch" on send email button

1. Check Kong is routing webhook:
   ```bash
   docker-compose exec kong curl -v http://n8n:5678/webhook/manual-email-send
   ```

2. Verify Kong config loaded:
   ```bash
   docker-compose logs kong | grep "n8n-webhooks"
   ```

3. Check n8n workflow is active:
   ```bash
   docker-compose logs n8n | grep "webhook"
   ```

### CORS errors

Ensure Kong's CORS plugin is enabled for `/webhook/` routes (should be in kong.yml):

```yaml
plugins:
  - name: cors
```

### Webhook not triggering from browser

Verify in browser DevTools:
1. Network tab shows POST to `/webhook/manual-email-send`
2. Response status is 200 (not 404 or 500)
3. Check n8n logs for workflow execution

## Rollback

If issues occur:

```bash
# Rollback config.js to localhost for debugging
git checkout frontend/config.js
docker-compose up -d

# Then fix and redeploy
```

## Verification Checklist

- [ ] Kong routing `/webhook/*` to n8n ✓
- [ ] Frontend config.js has production domain ✓
- [ ] Admin portal can reach Kong on correct port ✓
- [ ] Webhook accepts POST requests ✓
- [ ] n8n workflow triggers and sends email ✓
- [ ] Email appears in order record ✓
