# Deployment Guide - Demo Experience UI

## Overview

The demo-experience-ui is deployed to Vercel, providing automatic deployments from Git and global CDN distribution.

## Deployment URLs

- **Preview (Latest)**: https://demo-experience-boengh1nc-buzztrail.vercel.app
- **Production**: https://demo-experience-ui-buzztrail.vercel.app (or custom domain when configured)
- **Staging** (planned): demo-staging.buzztrail.ai
- **Production** (planned): demo.buzztrail.ai

## Project Configuration

**Vercel Project**: `demo-experience-ui`
**Team**: buzztrail
**Repository**: Monorepo at `/Users/lars/mycode/buzztrail/extension`
**Build Framework**: Vite + React + NX

## Build Configuration

The project uses a custom `vercel.json` at the monorepo root:

```json
{
  "$schema": "https://openapi.vercel.sh/vercel.json",
  "buildCommand": "pnpm exec vite build --config apps/demo-experience-ui/vite.config.ts",
  "outputDirectory": "dist/demo-experience-ui",
  "framework": null,
  "installCommand": "pnpm install --frozen-lockfile"
}
```

### Why This Configuration?

- **`buildCommand`**: Runs Vite from the monorepo root with explicit config path
- **`outputDirectory`**: Points to NX's output directory (`dist/demo-experience-ui`)
- **`framework: null`**: Disables Vercel's auto-detection (we handle build manually)
- **`installCommand`**: Uses pnpm with frozen lockfile for reproducible builds

## Environment Variables

All environment variables are configured in Vercel for all environments (production, preview, development):

| Variable | Value | Purpose |
|----------|-------|---------|
| `VITE_SUPABASE_URL` | https://dwdytxxqcvlrmyemonxz.supabase.co | Supabase project URL |
| `VITE_SUPABASE_ANON_KEY` | eyJhbGciOiJIUzI1NiIsInR5cCI6... | Supabase anonymous key (public) |
| `VITE_DAILY_DOMAIN` | buzztrail.daily.co | Daily.co video domain |
| `VITE_MOTIA_API_URL` | https://slide-system-production-5813.up.railway.app | Railway Motia API endpoint |

### Adding/Updating Environment Variables

```bash
# Add a new environment variable for production
printf "VALUE\n" | vercel env add VARIABLE_NAME production

# Add for all environments
printf "VALUE\n" | vercel env add VARIABLE_NAME production
printf "VALUE\n" | vercel env add VARIABLE_NAME preview
printf "VALUE\n" | vercel env add VARIABLE_NAME development
```

## Deployment Commands

### Deploy to Preview (Staging)

```bash
cd /Users/lars/mycode/buzztrail/extension
vercel --yes
```

This creates a preview deployment at: `https://demo-experience-<hash>-buzztrail.vercel.app`

### Deploy to Production

```bash
cd /Users/lars/mycode/buzztrail/extension
vercel --prod --yes
```

This deploys to: `https://demo-experience-ui-buzztrail.vercel.app`

### Check Deployment Status

```bash
vercel ls
```

### View Project Configuration

```bash
vercel project ls
```

## Custom Domain Setup

### Adding Custom Domains

1. **Via Vercel Dashboard** (Recommended):
   - Go to: https://vercel.com/buzztrail/demo-experience-ui/settings/domains
   - Click "Add Domain"
   - Enter: `demo-staging.buzztrail.ai` or `demo.buzztrail.ai`
   - Follow DNS configuration instructions

2. **Via CLI** (requires domain ownership):
   ```bash
   vercel domains add demo-staging.buzztrail.ai
   ```

### DNS Configuration

After adding the domain in Vercel, update your DNS provider (e.g., Cloudflare) with:

**For CNAME (recommended)**:
```
CNAME  demo-staging  cname.vercel-dns.com.
```

**For A Record**:
```
A      demo-staging  76.76.21.21
```

### Planned Domains

- **Staging**: `demo-staging.buzztrail.ai` → Preview deployments
- **Production**: `demo.buzztrail.ai` → Production deployment (after migration from legacy demos)

## CORS Configuration

The Railway Motia API needs to allow requests from Vercel domains:

```python
# In Railway Motia API (apps/slide-system/main.py)
ALLOWED_ORIGINS = [
    "http://localhost:4500",  # Local dev
    "https://demo-staging.buzztrail.ai",  # Staging
    "https://demo.buzztrail.ai",  # Production
    "https://*.vercel.app",  # Preview deployments
]
```

## Troubleshooting

### Build Failures

**Issue**: `NX   The externalDependency 'vite' could not be found`
**Solution**: This was resolved by running Vite directly instead of through NX:
```bash
pnpm exec vite build --config apps/demo-experience-ui/vite.config.ts
```

**Issue**: `Could not resolve entry module "index.html"`
**Solution**: Ensure vite.config.ts has `root: __dirname` and the build command references the correct config file.

### Environment Variable Issues

**Issue**: Variables not loading in build
**Solution**:
1. Check variables are set in Vercel dashboard for the correct environment
2. Verify variable names start with `VITE_` (Vite only exposes prefixed vars)
3. Redeploy after changing environment variables

### Domain Configuration

**Issue**: `Not authorized to use demo-staging.buzztrail.ai (403)`
**Solution**:
1. Domain must be added through Vercel dashboard first
2. Ensure you have team ownership of buzztrail.ai in your DNS provider
3. Add domain in Vercel dashboard → Settings → Domains

## Monitoring & Analytics

### Vercel Analytics

View deployment analytics at:
- https://vercel.com/buzztrail/demo-experience-ui/analytics

### Build Logs

View detailed build logs:
```bash
vercel logs <deployment-url>
```

Or in dashboard:
- https://vercel.com/buzztrail/demo-experience-ui/deployments

## Rollback

### Via CLI

```bash
# List deployments
vercel ls

# Promote a specific deployment to production
vercel promote <deployment-url>
```

### Via Dashboard

1. Go to: https://vercel.com/buzztrail/demo-experience-ui/deployments
2. Find the deployment you want to roll back to
3. Click "⋯" → "Promote to Production"

## Auto-Deployment from Git

**Current Setup**: Manual deployments via CLI

**Future Setup** (when GitHub integration is configured):
1. Push to `main` branch → Deploys to production
2. Push to any other branch → Creates preview deployment
3. Pull requests → Automatic preview deployments with unique URLs

To enable:
1. Go to: https://vercel.com/buzztrail/demo-experience-ui/settings/git
2. Connect the GitHub repository
3. Configure branch deployment settings

## Performance Optimization

### Caching

Vercel automatically caches:
- Static assets (JS, CSS, images) at edge locations globally
- Build output for faster subsequent builds

### Build Time

- **First build**: ~30-40 seconds (includes pnpm install)
- **Subsequent builds**: ~20-30 seconds (cached dependencies)

## Security

### API Keys

- **Supabase anon key**: Safe to expose (public, row-level security)
- **Daily.co domain**: Public (room URLs are generated server-side)
- **Motia API URL**: Public endpoint (authentication handled by Railway)

### HTTPS

- Automatic HTTPS with Vercel's SSL certificates
- Certificate renewal handled automatically

## Next Steps

1. **Configure Custom Domains**:
   - Add `demo-staging.buzztrail.ai` in Vercel dashboard
   - Update DNS records in Cloudflare
   - Test staging deployment

2. **Update Railway CORS**:
   - Add Vercel domains to Railway Motia API CORS configuration

3. **Test End-to-End Flow**:
   - Once Pipecat backend is deployed
   - Verify HairCheck → Discovery → Presentation modes

4. **Production Migration**:
   - Add `demo.buzztrail.ai` custom domain
   - Migrate from legacy `apps/demos/` to this app
   - Update all customer-facing links

## Support

- **Vercel Dashboard**: https://vercel.com/buzztrail/demo-experience-ui
- **Vercel Docs**: https://vercel.com/docs
- **Project Location**: `apps/demo-experience-ui/`
