# Local Development with Vercel Dev

This guide explains how to set up and use `vercel dev` for local testing of the BuzzTrail proxy server.

## 🚀 **Quick Start**

### **1. Prerequisites**
```bash
# Install Vercel CLI globally (if not already installed)
# Install Vercel CLI globally using npm (Vercel CLI is not in pnpm registry)
npm install -g vercel@latest

# Ensure you're logged into Vercel
vercel login
```

### **2. Setup Environment**
```bash
# Navigate to proxy server directory
cd apps/proxy-server

# Copy environment template
cp .env.example .env

# Edit .env with your actual values
# At minimum, set TAVUS_API_KEY
```

### **3. Start Development Server**
```bash
# Option 1: Using npm scripts
pnpm run dev

# Option 2: Using Nx
pnpm nx serve proxy-server

# Option 3: Direct vercel command
vercel dev
```

## 🛠 **Development Commands**

### **Available Scripts**
- `pnpm run dev` - Start Vercel dev server (recommended)
- `pnpm run dev:local` - Start local Node.js server (without Vercel simulation)
- `pnpm run deploy:preview` - Deploy to preview environment
- `pnpm run deploy` - Deploy to production

### **Nx Commands**
- `pnpm nx serve proxy-server` - Start Vercel dev server
- `pnpm nx serve:local proxy-server` - Start local Node.js server
- `pnpm nx deploy proxy-server` - Deploy to production
- `pnpm nx test proxy-server` - Run tests

## 🌐 **Local Testing**

### **Default URLs**
- **Vercel Dev**: `http://localhost:3000`
- **Health Check**: `http://localhost:3000/api/health`
- **Create Conversation**: `POST http://localhost:3000/api/conversations/create`

### **Test CORS Locally**
```bash
# Test CORS with curl
curl -X OPTIONS http://localhost:3000/api/health \
  -H "Origin: https://www-buzztrail-ai.filesusr.com" \
  -H "Access-Control-Request-Method: GET" \
  -v

# Test actual API call
curl -X POST http://localhost:3000/api/conversations/create \
  -H "Origin: https://www-buzztrail-ai.filesusr.com" \
  -H "Content-Type: application/json" \
  -d '{"personaKey": "sendcloud-lead-qualifier"}' \
  -v
```

### **Test with Frontend**
Update your frontend to point to local development server:
```javascript
// Development configuration
const API_BASE = 'http://localhost:3000';

// Test API call
const response = await fetch(`${API_BASE}/api/conversations/create`, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    personaKey: 'sendcloud-lead-qualifier'
  })
});
```

## ⚙️ **Configuration**

### **Environment Variables**
Create `.env` file in `apps/proxy-server/`:
```bash
# Required
TAVUS_API_KEY=your_actual_tavus_api_key

# Optional (defaults shown)
NODE_ENV=development
ENABLE_OPENTELEMETRY=false
TESTING_MODE=full-testing-with-safe-real
FAKE_SERVER_URL=http://localhost:3001
REAL_SERVER_URL=https://tavusapi.com
```

### **Port Configuration**
Vercel dev defaults to port 3000. To change:
```bash
vercel dev --listen 3001
```

## 🐛 **Debugging**

### **Common Issues**

**1. Port Already in Use**
```bash
# Kill process on port 3000
lsof -ti:3000 | xargs kill -9

# Or use different port
vercel dev --listen 3001
```

**2. API Key Not Working**
```bash
# Check environment variables are loaded
curl http://localhost:3000/api/health
# Should show "tavusApiKey": "configured"
```

**3. CORS Errors**
```bash
# Test specific origin
curl -X OPTIONS http://localhost:3000/api/health \
  -H "Origin: YOUR_ORIGIN_HERE" \
  -v
```

**4. Function Not Found**
```bash
# Ensure file structure is correct:
# apps/proxy-server/api/health.js ✅
# apps/proxy-server/api/conversations/create.js ✅
```

### **Debug Mode**
```bash
# Enable debug output
DEBUG=vercel* vercel dev

# Or with custom debug
DEBUG=true pnpm run dev
```

## 📝 **Development Workflow**

### **Recommended Flow**
1. **Start local server**: `pnpm run dev`
2. **Make changes** to API endpoints
3. **Test immediately** - Vercel dev auto-reloads
4. **Test CORS** with your frontend
5. **Run tests**: `npm test`
6. **Deploy preview**: `pnpm run deploy:preview`
7. **Test preview** with production-like setup
8. **Deploy production**: `pnpm run deploy`

### **Testing Strategy**
```bash
# 1. Test health endpoint
curl http://localhost:3000/api/health

# 2. Test CORS headers
curl -I -X OPTIONS http://localhost:3000/api/health \
  -H "Origin: https://www-buzztrail-ai.filesusr.com"

# 3. Test conversation creation
curl -X POST http://localhost:3000/api/conversations/create \
  -H "Content-Type: application/json" \
  -d '{"personaKey": "sendcloud-lead-qualifier"}'

# 4. Run automated tests
npm test
```

## 🔄 **Hot Reloading**

Vercel dev automatically reloads when you change:
- ✅ **API endpoint files** (`api/**/*.js`)
- ✅ **Environment variables** (restart required)
- ✅ **vercel.json configuration** (restart required)

## 🚀 **Deployment Testing**

### **Preview Deployments**
```bash
# Deploy to preview URL
pnpm run deploy:preview

# Test preview
curl https://your-preview-url.vercel.app/api/health
```

### **Production Deployment**
```bash
# Deploy to production
pnpm run deploy

# Verify production
curl https://buzztrail-proxy.vercel.app/api/health
```

## 💡 **Tips**

1. **Use Vercel dev by default** - most accurate simulation of production
2. **Keep .env file private** - never commit actual API keys
3. **Test CORS early** - easier to debug locally
4. **Use preview deployments** - test before production
5. **Monitor logs** - `vercel logs` to debug production issues

This setup gives you a production-like local development environment that will catch most issues before deployment!