# Deployment guide

Read `docs/PROVIDER.md` (provider go-live checklist) and `docs/ADMIN.md` before opening to customers. Items marked **BLOCKING** must be complete first.

## 1. Server
- Linux with PHP 8.2+ (extensions: `bcmath`, `mbstring`, `pdo_mysql`, `curl`, `openssl`, `intl` recommended), Composer, MySQL 8.x, Redis (recommended for cache and queue).
- Web server (Nginx or Apache) with **HTTPS only**. Point the document root at `public/`.
- Serve the app over HTTPS with a valid certificate. The security headers and secure cookies assume HTTPS.

## 2. Application
```bash
composer install --no-dev --optimize-autoloader
cp .env.example .env            # then edit values (below)
php artisan key:generate        # run once; NEVER again on a live system
php artisan migrate --force
php artisan db:seed --class=ProductSeeder
php artisan trevo:admin:create you@example.com "Owner Name"
php artisan config:cache && php artisan route:cache && php artisan view:cache
```

## 3. Environment (set on the server, never in git or chat)
| Variable | Production value |
|---|---|
| `APP_ENV` | `production` |
| `APP_DEBUG` | `false` (**BLOCKING**: debug pages would leak internals) |
| `APP_URL` | `https://your-domain` |
| `DB_*` | MySQL credentials for a dedicated database user |
| `SESSION_SECURE_COOKIE` | `true` |
| `CACHE_STORE` / `QUEUE_CONNECTION` | `redis` (or `database`), shared between all servers |
| `MAIL_*` | a real mail provider (password reset and verification depend on it) |
| `TREVO_REQUIRE_EMAIL_VERIFICATION` | `true` once mail works |
| `TREVO_ADMIN_IP_ALLOWLIST` | your office or VPN addresses |
| `PINBOT_*` | keep `PINBOT_ENABLED=false` until the provider checklist is complete (**BLOCKING**) |
| `TREVO_PAYMENT_GATEWAY` | keep `null` until a gateway is chosen and integrated (**BLOCKING** for deposits) |

## 4. Processes
- **Queue worker** under systemd or Supervisor, for example:
  `php artisan queue:work --sleep=3 --tries=1 --max-time=3600`
  Restart it after each deploy (`php artisan queue:restart`).
- **Scheduler** every minute: `* * * * * cd /var/www/trevo && php artisan schedule:run >> /dev/null 2>&1`
  This runs order reconciliation, webhook dispatch, and pruning of API logs and webhook records.

## 5. Proxies and client IPs
If TREVO sits behind a load balancer or CDN, configure trusted proxies in `bootstrap/app.php` (`$middleware->trustProxies(at: [...])`). Otherwise IP allowlists, rate limits and logs see the proxy's address. Do not trust `X-Forwarded-For` from anywhere except your own proxies.

## 6. Backups and keys (**BLOCKING**)
- Back up the database **and** `APP_KEY`. Without `APP_KEY`, voucher codes, webhook secrets and encrypted settings cannot be decrypted.
- Store the backup key separately from the database backup.
- Test a restore on a separate machine before launch.

## 7. Logs and monitoring
- Keep Laravel logs in `storage/logs`, rotated (for example with `logrotate`). Never log request bodies or headers containing keys.
- Alert on: failed jobs (`php artisan queue:failed`), orders in `manual_review` older than an hour, webhook deliveries `failed`, and provider configuration status (`php artisan trevo:provider:status`).

## 8. Go-live checklist
- [ ] HTTPS works; HTTP redirects to HTTPS.
- [ ] `APP_DEBUG=false`; `php artisan about` shows production.
- [ ] Super admin can sign in; `/admin` reachable only from allowed IPs.
- [ ] Test reseller can register, see the dashboard, and create an API key.
- [ ] Prices set; services and products activated deliberately.
- [ ] Provider checklist in `docs/PROVIDER.md` complete, then one small real order end to end.
- [ ] Webhook test delivery received by a test endpoint with a valid signature.
- [ ] Queue worker and scheduler running (check `php artisan schedule:list`).
- [ ] Backups of database and `APP_KEY` taken and a restore tested.
- [ ] Maintenance message and support contact set under Website settings.

## 9. Known limits at this version
- Admin accounts have no second factor yet. Restrict `/admin` by IP and use strong, unique passwords.
- No strict Content-Security-Policy yet (see `SecurityHeaders`).
- No email notifications for support tickets.
- Webhook delivery is at least once; reseller systems must de-duplicate.
