# Once - Deployment Guide

## Prerequisites

- Node.js 18+
- Flutter 3.19+ (for mobile)
- Firebase CLI: `npm install -g firebase-tools`
- Vercel CLI (optional): `npm install -g vercel`

## Firebase Setup

1. **Enable Services** in Firebase Console (`arimage-f84ba`):
   - Authentication → Phone provider (enable)
   - Firestore Database → Create (start in test mode for dev)
   - Storage → Enable
   - Cloud Messaging → Enable
   - Cloud Functions → Upgrade to Blaze plan (pay-as-you-go)

**First time only — Log in to Firebase:**
```bash
npx firebase-tools login
```

2. **Deploy Rules & Indexes**:
```bash
cd d:\work\httpsonce.film\backend
npx firebase-tools deploy --project arimage-f84ba --only firestore:rules
npx firebase-tools deploy --project arimage-f84ba --only firestore:indexes
npx firebase-tools deploy --project arimage-f84ba --only storage:rules
```

3. **Deploy Functions**:
```bash
cd d:\work\httpsonce.film\backend\functions
npm install
npm run build
npx firebase-tools deploy --project arimage-f84ba --only functions
```

## Web App Deployment

### Option A: Vercel (Recommended for Next.js)

```bash
cd d:\work\httpsonce.film\web
npm install
npx vercel --prod
```

### Option B: Firebase Hosting (Static Export)

```bash
cd d:\work\httpsonce.film\web
npm run build
cd ..\backend
npx firebase-tools deploy --project arimage-f84ba --only hosting
```

### Run Locally:
```bash
cd d:\work\httpsonce.film\web
npm run dev
```

## Mobile App Setup

### Android
1. Add `google-services.json` to `mobile/android/app/` ✅ Done
2. Enable Firebase Auth (Phone) in console
3. Add SHA-1 fingerprint in Firebase Console → Project Settings → Android

```bash
cd d:\work\httpsonce.film\mobile
flutter pub get
cd android
.\gradlew signingReport  # Get SHA-1
cd ..\..
flutter run
```

### iOS (Mac only)
1. Download `GoogleService-Info.plist` from Firebase Console
2. Add to `mobile/ios/Runner/` via Xcode
3. Run `pod install` in `ios/` directory

```bash
cd d:\work\httpsonce.film\mobile\ios
pod install
cd ..
flutter run
```

## Environment Variables (Web)

Firebase config is hardcoded in `web/src/lib/firebase.ts`. For production env vars, create `web/.env.local`:

```env
NEXT_PUBLIC_FIREBASE_API_KEY=AIzaSyCDNYvI5t5za07WCc5R9o4eeVC7hx84M0A
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=arimage-f84ba.firebaseapp.com
NEXT_PUBLIC_FIREBASE_PROJECT_ID=arimage-f84ba
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=arimage-f84ba.firebasestorage.app
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=85337517437
NEXT_PUBLIC_FIREBASE_APP_ID=1:85337517437:web:76c7507200d0b00d91b014
NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID=G-F86SC4M0XC
```

## Features Status

| Feature | Web | Mobile | Backend |
|---------|-----|--------|---------|
| Phone OTP Auth | ✅ | ✅ | ✅ |
| Event Creation | ✅ | ✅ | ✅ |
| QR Code (scan/generate) | ✅ | ✅ | ✅ |
| Photo Capture | ✅ (browser) | ✅ (native) | ✅ |
| Photo Gallery | ✅ | ✅ | ✅ |
| Host Approval | ✅ | ✅ | ✅ |
| Sharing Permissions | ✅ | ✅ | ✅ |
| Admin Dashboard | ✅ | - | ✅ |
| AI Face Detection | ✅ | - | ✅ |
| Smart Curation | ✅ | - | ✅ |
| Photo Challenges | ✅ | - | ✅ |
| Social Sharing | ✅ | - | ✅ |
| Push Notifications | - | - | ✅ |
| Analytics | ✅ | - | ✅ |

## Production Setup Checklist

### 1. Enable Cloud Scheduler API
The `checkEventReveal` scheduled function runs every hour. You must enable Cloud Scheduler API:

```bash
gcloud services enable cloudscheduler.googleapis.com --project arimage-f84ba
```

Or via [Google Cloud Console](https://console.cloud.google.com/apis/library/cloudscheduler.googleapis.com?project=arimage-f84ba)

### 2. Configure VAPID Key for Push Notifications

1. Go to Firebase Console → Project Settings → Cloud Messaging
2. Under "Web Push certificates", click "Generate key pair"
3. Copy the **Public key** and add it to `web/src/lib/firebase.ts`:
   ```ts
   const messaging = getMessaging(app);
   // Use VAPID key for web push
   ```
4. (Optional) Add the public key to `web/.env.local`:
   ```env
   NEXT_PUBLIC_FIREBASE_VAPID_KEY=your_vapid_public_key_here
   ```

### 3. Enable Google Cloud Vision API (Production AI)

The `analyzePhotoFaces` function currently uses simulated data. To enable real face detection:

```bash
gcloud services enable vision.googleapis.com --project arimage-f84ba
```

Then update `backend/functions/src/ai-features.ts` to call the Vision API instead of random data.

### 4. Set Stripe Secret Key

```bash
npx firebase-tools functions:config:set stripe.secret="sk_live_..." --project arimage-f84ba
```

Or set as environment variable in Google Cloud Console → Cloud Functions → `createCheckoutSession` → Edit → Runtime environment variables.

### 5. Firestore Indexes

After adding the `status + revealDate` index, deploy:

```bash
cd d:\work\httpsonce.film\backend
npx firebase-tools deploy --project arimage-f84ba --only firestore:indexes
```

### 6. Higgsfield AI Effects (Photo Effects Templates)

The `applyPhotoEffect` Cloud Function uses the [Higgsfield CLI](https://github.com/higgsfield-ai/cli) (`@higgsfield/cli`) via `npx` to transform guest photos using 10 built-in effect templates (Cinematic, Vintage, B&W, Anime, Oil Painting, Neon Cyberpunk, Dreamy Bokeh, Studio Portrait, Cinematic Motion video, Slow-Mo Wedding video).

**Setup (recommended — via Admin UI):**

1. Sign in as an admin user (`users/{uid}.isAdmin = true` in Firestore).
2. Open `/admin` → **System** tab.
3. Paste your Higgsfield API token in the **Higgsfield API Token** field and click **Save**.

The token is stored at `system/config.higgsfieldApiToken` (read-protected by Firestore rules; only Cloud Functions via the Admin SDK can read it). The admin UI shows only the last 4 characters after save.

**Setup (alternative — env var):**

```bash
gcloud functions deploy applyPhotoEffect --set-env-vars HIGGSFIELD_API_TOKEN=YOUR_TOKEN
```

The function reads the Firestore value first and falls back to the env var.

3. Function timeout is set to 540s and memory to 1GB to accommodate video models (~90–120s).
4. Templates are defined in:
   - `backend/functions/src/effect-templates.ts`
   - `web/src/lib/effect-templates.ts`
   Keep both in sync if you add new effects.

**Cost note:** Higgsfield is a paid API (per-generation). Consider rate-limiting per user via Firestore counters for production.

## Important Notes

- **Cloud Functions** require Blaze plan for external API calls
- **Google Cloud Vision API** needs to be enabled for production AI features
- **PWA** works offline with cached assets
- **Firestore indexes** are defined in `backend/firestore.indexes.json`

## URLs

- **Local Dev**: http://localhost:3003
- **Firebase Console**: https://console.firebase.google.com/project/arimage-f84ba
