# Deployment

**Status:** Production readiness guide in [20-production-readiness.md](./20-production-readiness.md) (Task 018). This file remains the deployment overview.

## Environments

| Environment | Purpose | URL pattern |
|-------------|---------|-------------|
| Local | Development | `http://localhost:3000` |
| Staging | Pre-production testing | `https://staging-growth.anwal.sa` (example) |
| Production | Live internal use | `https://growth.anwal.sa` (example) |

## Prerequisites

- Node.js 20 LTS
- MongoDB 6+ (Atlas recommended)
- SMTP and IMAP mailboxes provisioned
- Env vars set per [10-environment-variables.md](./10-environment-variables.md)

## Build and run

```bash
npm ci
npm run build
npm run start
```

Development:

```bash
npm run dev
```

Requires `.env.local` with core variables — see [10-environment-variables.md](./10-environment-variables.md).

```bash
cp .env.example .env.local
# Edit .env.local with real values, then:
npm run dev
```

## Hosting options

| Option | Notes |
|--------|-------|
| Vercel | Simple Next.js deploy; ensure MongoDB and long-running IMAP jobs supported (cron/external worker) |
| Docker on VPS | Full control for IMAP sync workers and Redis |
| Azure / AWS | Enterprise preference if Anwal standardizes there |

**Note:** IMAP sync and campaign send workers may require a **separate worker process** or scheduled cron if serverless timeouts are insufficient.

## Environment configuration

1. Copy `.env.example` to platform secret store
2. Set all required variables for enabled features
3. Verify `NEXTAUTH_URL` and `APP_URL` match public URL
4. Run health check: `GET /api/health`

## Database

- Create MongoDB database and application user with read/write only on `anwal_growth`
- Enable backups (Atlas continuous backup or scheduled dump)
- Sync indexes: `npx tsx scripts/sync-indexes.ts` (see [20-production-readiness.md](./20-production-readiness.md))

## CI/CD (planned)

- Lint and typecheck on PR
- Build on merge to main
- Deploy staging automatically; production manual approval

## Post-deploy checklist

See full runbook: [20-production-readiness.md](./20-production-readiness.md) and [21-smoke-test-checklist.md](./21-smoke-test-checklist.md).

- [ ] Health endpoint returns 200 with `database: connected`
- [ ] Login works with production NextAuth config
- [ ] MongoDB connection from app host
- [ ] Test SMTP send to internal address
- [ ] IMAP sync test (if enabled)
- [ ] HTTPS and secure cookies verified

## Rollback

- Redeploy previous build artifact
- Database migrations are backward-compatible or have rollback scripts

## Monitoring

- Uptime on `/api/health`
- Error tracking (Sentry or equivalent — Task 023)
- Disk and memory if self-hosted

## Related

- [11-security.md](./11-security.md)
- [20-production-readiness.md](./20-production-readiness.md)
- [21-smoke-test-checklist.md](./21-smoke-test-checklist.md)
