# Deployment, Backup & Troubleshooting Guide

## 1. Local Development (Windows / Linux / macOS)

### Backend (PHP 8.2+ & SQLite)
Ensure PHP extensions `pdo_sqlite`, `openssl`, `curl`, `bcmath`, `mbstring`, and `json` are enabled in `php.ini`.

```bash
cd api
cp .env.example .env
php -d extension=pdo_sqlite database/migrate.php
php -d extension=pdo_sqlite database/seed.php
php -d extension=pdo_sqlite tests/run.php
php -d extension=pdo_sqlite -S localhost:8000 index.php
```

Default Super Admin seeded for local testing:
- Email: `admin@ffglory.local`
- Password: `Admin@123456` (change immediately in production)

### Frontend (React + Vite)
```bash
cd frontend
npm install
npm run dev
```

## 2. Production Deployment

### Backend (VPS / Shared Hosting with MySQL 8+)
1. Create a MySQL database (`ffglory_saas`) with `utf8mb4_unicode_ci`.
2. Configure `api/.env`:
   - `APP_ENV=production`, `APP_DEBUG=false`
   - `APP_KEY=base64:...` (32 random bytes)
   - `DB_DRIVER=mysql`, `DB_HOST`, `DB_DATABASE`, `DB_USERNAME`, `DB_PASSWORD`
   - `COOKIE_SECURE=true`, `CORS_ALLOWED_ORIGINS=https://yourdomain.com`
   - `FFGLORY_USE_MOCK=false` (or configure keys in Super Admin Panel)
3. Run migrations and seeders:
   ```bash
   php api/database/migrate.php
   php api/database/seed.php
   ```
4. Configure Cron (every 5 minutes for glory monitoring, zero-growth auto-refunds, and Telegram queue):
   ```cron
   */5 * * * * php /var/www/ffglory-saas/api/workers/cron.php >> /var/log/ffglory-cron.log 2>&1
   ```
   Or on shared hosting without CLI cron, call `POST https://api.yourdomain.com/api/cron/trigger` with header `X-Cron-Secret: <CRON_SECRET>`.

### Frontend (Static Host / Vercel / Netlify / Nginx)
```bash
cd frontend
VITE_API_BASE_URL=https://api.yourdomain.com npm run build
```
Deploy `frontend/dist/` and configure SPA fallback to `index.html`.

## 3. Backup & Restore
- **Admin JSON Export**: `GET /api/admin/backup/export` (requires Super Admin session).
- **MySQL**: `mysqldump -u user -p ffglory_saas > backup_$(date +%F).sql`
- **SQLite**: `cp api/database/database.sqlite api/database/backup_$(date +%F).sqlite`

## 4. Troubleshooting
- **`could not find driver` on SQLite**: Enable `extension=pdo_sqlite` in `php.ini` or run with `php -d extension=pdo_sqlite`.
- **403 CSRF error**: Ensure `CORS_ALLOWED_ORIGINS` includes your exact frontend origin and `withCredentials: true` is active.
- **Zero-growth auto-refund not firing**: Verify `cron.php` is running every 5 minutes and `auto_refund_enabled` is enabled in Admin → Provider & Regions.
