# Recall AI — Deployment Guide

## Architecture
- **recall-ai-app/** → Laravel 11 on cPanel shared hosting
- **recall-ai-whatsapp/** → Node.js on Railway.app (free tier)

---

## PART 1: Database Setup (cPanel)

1. Login to cPanel → **MySQL Databases**
2. Create a new database: `yourprefix_recallai`
3. Create a user and assign ALL PRIVILEGES to that database
4. Go to **phpMyAdmin** → select your database
5. Click **Import** → upload `database/recall_ai_database.sql`
6. Click Go — all 13 tables + seed data will be created

---

## PART 2: Laravel App (cPanel)

### Step 1 — Upload files
Upload the entire `recall-ai-app/` folder contents to your hosting.

**For cPanel subdomain or addon domain:**
```
public_html/recallai/        ← upload everything here
public_html/recallai/public/ ← this becomes your document root
```

**Set document root** in cPanel → Domains → point to:
`/home/yourusername/public_html/recallai/public`

### Step 2 — Configure .env
```bash
cp .env.example .env
```

Fill in ALL values in `.env`:
```env
APP_URL=https://yourdomain.com
APP_KEY=                         # generate below

DB_DATABASE=yourprefix_recallai
DB_USERNAME=yourprefix_dbuser
DB_PASSWORD=your_db_password

JWT_SECRET=                      # generate below

GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_secret
GOOGLE_REDIRECT_URI=https://yourdomain.com/auth/gmail/callback

CLAUDE_API_KEY=sk-ant-...

WHATSAPP_SERVICE_URL=https://your-app.railway.app
WHATSAPP_SERVICE_SECRET=generate_32_char_random_string
WHATSAPP_WEBHOOK_SECRET=generate_another_32_char_string

MAIL_HOST=mail.yourdomain.com
MAIL_PORT=465
MAIL_USERNAME=noreply@yourdomain.com
MAIL_PASSWORD=your_smtp_password
```

### Step 3 — Install via cPanel Terminal (or SSH)
```bash
cd /home/yourusername/public_html/recallai

# Install PHP dependencies
composer install --no-dev --optimize-autoloader

# Generate app key
php artisan key:generate

# Generate JWT secret
php artisan jwt:secret

# Clear and cache config
php artisan config:cache
php artisan route:cache
php artisan view:cache

# Set permissions
chmod -R 775 storage bootstrap/cache
chown -R nobody:nobody storage bootstrap/cache
```

### Step 4 — Storage symlink
```bash
php artisan storage:link
```

### Step 5 — Cron job (cPanel → Cron Jobs)
Add this cron to run every minute:
```
* * * * * cd /home/yourusername/public_html/recallai && php artisan schedule:run >> /dev/null 2>&1
```

---

## PART 3: WhatsApp Microservice (Railway)

### Step 1 — Create Railway account
Go to https://railway.app → sign up free

### Step 2 — Deploy from GitHub
1. Push `recall-ai-whatsapp/` to a GitHub repo
2. In Railway: New Project → Deploy from GitHub → select repo
3. Railway auto-detects Node.js and deploys

### Step 3 — Set environment variables in Railway dashboard
```
PORT=3000
NODE_ENV=production
SERVICE_SECRET=same_value_as_WHATSAPP_SERVICE_SECRET_in_laravel
LARAVEL_WEBHOOK_URL=https://yourdomain.com/api/whatsapp/webhook
LARAVEL_WEBHOOK_SECRET=same_value_as_WHATSAPP_WEBHOOK_SECRET_in_laravel
SESSION_DIR=./sessions
```

### Step 4 — Get your Railway URL
After deploy, Railway gives you a URL like:
`https://recall-ai-whatsapp-production.railway.app`

Paste that into your Laravel `.env`:
```env
WHATSAPP_SERVICE_URL=https://recall-ai-whatsapp-production.railway.app
```

---

## PART 4: Google OAuth Setup

1. Go to https://console.cloud.google.com
2. Create a new project: "Recall AI"
3. APIs & Services → Enable: **Gmail API** + **Google People API**
4. Credentials → Create OAuth 2.0 Client ID
   - Application type: **Web application**
   - Authorized redirect URIs: `https://yourdomain.com/auth/gmail/callback`
5. Copy Client ID and Secret → paste into `.env`

---

## PART 5: Claude API Key

1. Go to https://console.anthropic.com
2. API Keys → Create Key
3. Paste into `.env` as `CLAUDE_API_KEY`

---

## PART 6: Enable Paid Plans (When Ready)

To activate the Pro plan and enable payments:

1. In phpMyAdmin, run:
```sql
UPDATE plans SET is_active = 1, price_monthly = 29.00 WHERE slug = 'pro';
```

2. Add Stripe keys to `.env`:
```env
STRIPE_KEY=pk_live_...
STRIPE_SECRET=sk_live_...
```

3. Update Pro plan with Stripe price IDs:
```sql
UPDATE plans
SET stripe_monthly_price_id = 'price_xxxxx',
    stripe_yearly_price_id  = 'price_yyyyy'
WHERE slug = 'pro';
```

---

## Verify Everything Works

### Check 1 — App loads
Visit `https://yourdomain.com` → should see login page

### Check 2 — Register + verify email
Register an account → check email → click verify link

### Check 3 — Gmail sync
Dashboard → Connect Gmail → OAuth flow → should redirect back with messages

### Check 4 — WhatsApp QR
Dashboard → Connect WhatsApp → QR code appears → scan with phone

### Check 5 — AI Sync
Click "AI Sync" button → should process messages through Claude

---

## Folder Permissions Reference
```
storage/           → 775
bootstrap/cache/   → 775
public/            → 755
.env               → 640 (never 644 in production)
```

---

## Support Checklist
- [ ] Database imported successfully (13 tables visible in phpMyAdmin)
- [ ] `.env` fully configured (no empty required values)
- [ ] `composer install` completed without errors
- [ ] `php artisan key:generate` ran
- [ ] `php artisan jwt:secret` ran
- [ ] Google OAuth credentials created with correct redirect URI
- [ ] Railway deployed and health check passes (`/health` returns `{"status":"ok"}`)
- [ ] Both SERVICE_SECRET values match between Laravel and Railway
