# ToolNaka → Vercel + Turso — Deployment Runbook

Everything is prepared in the code. You only need to create two free accounts and paste 2 credentials. Total time: ~20 minutes.

---

## Part A — Create the Turso database (5 min)

1. Go to <https://platform.turso.tech> and sign up (free — no credit card; GitHub login works).
2. Create a database: name `toolnaka`, location `nrt1` (Chennai — closest to India) or `bom1` (Mumbai).
3. From the database page copy:
   - **Database URL** (looks like `libsql://toolnaka-<yourorg>.turso.io`)
   - Create a **token** (Database → Tokens → "Generate token") and copy it.
4. Send both values to me (or fill them into `vercel-env.txt` yourself), OR continue below yourself.

> Why Turso: it is SQLite-compatible, so the existing Prisma schema (37 tables), all queries, and the existing data migrate 1:1 with zero SQL rewrite. It has a free tier (9 GB, 500 databases) that doesn't sleep.

## Part B — Create the Vercel project (5 min)

1. Go to <https://vercel.com> and sign up with GitHub (free Hobby plan).
2. Create a **Personal Access Token**: Vercel dashboard → Settings → Tokens → Create. Send it to me, or continue yourself with the Vercel CLI.

## Part C — What I run once I have the two sets of credentials

```bash
# 1. Schema + data migration from local SQLite to Turso
set TURSO_DATABASE_URL=libsql://...
set TURSO_AUTH_TOKEN=...
node scripts/migrate-to-turso.mjs

# 2. Link and deploy
npx vercel login            # (only if you did NOT give a token)
npx vercel link
npx vercel env add DATABASE_URL      # "file:/tmp/dev.db" placeholder (client uses adapter)
npx vercel env add TURSO_DATABASE_URL
npx vercel env add TURSO_AUTH_TOKEN
npx vercel env add SECRET   # existing JWT secret
npx vercel env add SMTP_USER / SMTP_PASS / NEXT_PUBLIC_SITE_URL
npx vercel env add CRON_SECRET  # random string, guards the monthly-bill cron
npx vercel --prod
```

Build on Vercel uses `next build` (the `SKIP_STANDALONE_COPY=1` env is set automatically; the standalone-copy step is skipped there).

## Part D — Domain (toolsnaka.store)

Currently the domain points at the local machine via Cloudflare + cloudflared. After Vercel is live:

1. Vercel project → Settings → Domains → add `toolsnaka.store`.
2. In Cloudflare DNS: change the `toolsnaka.store` record from the tunnel CNAME to the record Vercel shows (typically `cname.vercel-dns.com`), and **set it to DNS-only (grey cloud)** so Vercel can issue its own SSL.
3. Keep the local setup running on a subdomain (e.g. `local.toolsnaka.store`) as a fallback if desired.

## What was changed in the code for this migration

| Change | File | Why |
|---|---|---|
| Dual-mode Prisma client (Turso adapter or local SQLite) | `src/lib/db.ts` | Same code runs locally and on Vercel |
| `driverAdapters` preview feature + adapter deps | `prisma/schema.prisma`, `package.json` | Prisma 6 + libSQL/Turso |
| Image uploads stored in DB (`Upload` table), not on disk | `part-requests` + `uploads` routes, schema | Vercel FS is read-only; `<img>` tags can't send auth headers anyway |
| SQLite PRAGMAs guarded | `src/lib/db.ts` | PRAGMA is a no-op/invalid on Turso |
| Monthly-bill cron as API route + `vercel.json` | `src/app/api/cron/monthly-bills/route.ts` | Vercel Hobby allows 1 cron/day → runs daily, internally only acts on the 1st (or with `?force=1`) |
| `postinstall: prisma generate`, `SKIP_STANDALONE_COPY` support | `package.json`, `scripts/copy-standalone.mjs` | Correct builds on both hosts |
| Rider/Delivery role fully removed (auth page, landing, router, store, backend allowlist) | 6 files | Delivery flow was already removed from the product |
| Demo-login buttons removed | `AuthPage.tsx` | Demo accounts were wiped; buttons would 401 |

## Verification checklist (I run this after deploy)

- [ ] `GET /api/health` → `{status:ok, db:ok}` from Turso
- [ ] Landing page 200, no rider/delivery card, no demo buttons
- [ ] Register a real factory account (OTP flow) → login works → token stored
- [ ] Login with kept admin → admin dashboard empty state (no mocks)
- [ ] Product list empty (fresh start) on Turso
- [ ] Part-request photo upload → image serves from `/api/uploads/<name>`
- [ ] Unauth API → 401; delivery routes → 404
- [ ] Custom domain resolves with valid SSL
