# BuzzTrail Wix Demo - Build & Environment Guide

## 🏗️ Build System Overview

This NX monorepo project uses **Vite + Terser minification** with environment-specific builds via the Vite define plugin to ensure correct configuration for local development vs production deployment.

### **Environment Configurations**

| Environment | Proxy URL | Video URL | Daily.co API Key |
|-------------|-----------|-----------|------------------|
| **Local** | `null` (direct Daily.co) | Full Wix video URL | Injected via env var |
| **Production** | `https://buzztrail-proxy.vercel.app/api` | Relative path | `null` (no key needed) |

## 🚀 Build Commands

### **Production Build (for Wix)**
```bash
pnpm nx build:wix demos
# Shorthand:
pnpm wix
```
- Creates: `wix-custom-code-production.html`
- Size: ~6,484 characters (43% of Wix limit)
- Uses: BuzzTrail proxy service
- Ready for: Wix Custom Code deployment

### **Unified Build Process**
```bash
pnpm nx build:wix demos
```
- Creates: Both production and local HTML files
- Opens: Automatic local testing on port 8080
- Uses: Environment-specific configurations
- Ready for: Both production deployment and local testing

## 🔧 Local Development Setup

### **1. Build Both Versions**
```bash
pnpm nx build:wix demos
```

### **2. Test Automatically**
- Local server starts automatically on port 8080
- Test page opens automatically in browser

### **3. Test**
- Open: `http://localhost:8080/buzztrail-video-demo/`
- Content automatically loads from `wix-custom-code-local.html`
- Test full conversation flow with real Daily.co integration

## 🌍 Environment Configuration

### **Vite Define Plugin** (`vite.wix.config.ts`)
```javascript
define: {
  'process.env.VITE_PROXY_URL': JSON.stringify(
    isLocal ? null : 'https://buzztrail-proxy.vercel.app/api'
  ),
  'process.env.VITE_VIDEO_URL': JSON.stringify(
    isLocal ? 'https://video.wixstatic.com/...' : 'longer-with-text.mp4'
  ),
  'process.env.VITE_DAILY_API_KEY': JSON.stringify(
    isLocal ? 'your-daily-api-key' : null
  )
}
```

### **Source Code Usage** (`src/wix-demo.js`)
```javascript
const CONFIG = {
    proxyUrl: process.env.VITE_PROXY_URL,
    videoUrl: process.env.VITE_VIDEO_URL,
    dailyApiKey: process.env.VITE_DAILY_API_KEY
};
```

## ⚙️ Daily.co Integration

### **Local Development**
- **Direct Daily.co API** with full room configuration control
- **15-minute expiration** for adequate testing time
- **Leave button hidden** via `enable_prejoin_ui: false`
- **Same UI/UX** as production

### **Production**
- **BuzzTrail proxy service** handles Tavus integration
- **URL parameter workarounds** for consistent behavior
- **Limited configuration** due to Tavus API constraints
- **Same minification** as local build for parity

## 📁 File Structure

```
apps/demos/
├── vite.wix.config.ts           # Vite build configuration
├── project.json                 # NX project configuration
└── buzztrail-video-demo/
    ├── src/
    │   ├── wix-demo.js              # Main source (environment-agnostic)
    │   ├── shared-demo.js           # Shared utility functions
    │   └── index.html               # Local development page
    ├── wix-custom-code-production.html  # Built for Wix
    ├── wix-custom-code-local.html       # Built for local development
    └── BUILD-GUIDE.md               # This guide
```

## 🔍 Key Differences

| Aspect | Local Development | Production |
|--------|-------------------|------------|
| **API Endpoint** | Direct Daily.co API | BuzzTrail proxy service |
| **Video URL** | Full Wix media URL | Relative path |
| **Daily.co Rooms** | Created directly | Created via Tavus |
| **Room Configuration** | Full control | Limited by Tavus API |
| **Build Process** | Same minification | Same minification |
| **Character Count** | ~6,484 chars | ~6,484 chars |

## 🛠️ Development Workflow

1. **Edit source**: `src/wix-demo.js` (single source of truth)
2. **Build both**: `pnpm nx build:wix demos` (builds production + local, opens test)
3. **Test locally**: `python3 -m http.server 8080`
4. **Build production**: `pnpm wix`
5. **Deploy to Wix**: Copy `wix-custom-code-production.html` to Wix custom code
6. **Verify**: Check Wix site and console for successful initialization

## 📋 Character Limits

- **Wix limit**: 15,000 characters
- **Current usage**: ~6,484 characters (43%)
- **Remaining**: ~8,516 characters
- **Optimization**: Aggressive Terser minification

## 🔐 Security

- **Environment variables** injected at build time via Vite
- **Local Daily.co key** never exposed in production builds
- **Production** uses server-side proxy (no keys in client code)
- **React-safe** implementation prevents content flashing in Wix