# Nitescape (Next.js 15 + Prisma) — PM2/VPS deployment

## Requirements
- Node.js (LTS recommended)
- MySQL (or a compatible managed MySQL service)

## Local development
```bash
cp env.example .env
npm ci
npm run dev
```

## Environment variables
See `env.example` for the full list.

### Required (core)
- `DATABASE_URL`
- `NEXT_PUBLIC_BASE_URL`
- `NEXTAUTH_URL`
- `NEXTAUTH_SECRET`
- `ADMIN_EMAIL` + `ADMIN_PASSWORD_HASH` (recommended) or `ADMIN_PASSWORD` (fallback)
- If your environment expands `$...` in `.env` (can happen with Next.js), prefer `ADMIN_PASSWORD_HASH_BASE64` instead of `ADMIN_PASSWORD_HASH`.
- `UPLOAD_DIR` (where uploaded images are stored on disk)

Generate a bcrypt hash:
```bash
node -e "console.log(require('bcryptjs').hashSync('your-password', 12))"
```

Generate a base64-encoded bcrypt hash (avoids `$` expansion issues):
```bash
node -e "const b=require('bcryptjs');const h=b.hashSync('your-password',12);console.log(Buffer.from(h).toString('base64'))"
```

## Database (production)
Run migrations on deploy:
```bash
npx prisma migrate deploy
```

Optional: seed data (only if you want the seed fixtures in prod):
```bash
npx prisma db seed
```

## Build & run (production)
```bash
npm ci
npm run build
npm run start
```

## PM2 deploy (recommended)
This repo includes an `ecosystem.config.cjs` for PM2.

First-time setup:
```bash
pm2 start ecosystem.config.cjs --env production
pm2 save
pm2 startup
```

Typical update flow:
```bash
git pull
npm ci
npx prisma migrate deploy
npm run build
pm2 reload nitescape
```

## Notes
- Uploaded files should be stored outside the repo and **writable** by the PM2 user (see `UPLOAD_DIR`).
- Generated sitemaps (`public/sitemap*.xml`) are created during `postbuild` and are not committed.
