# Deploying Spin-to-Win on cPanel (single Node app)

Both the Next.js frontend and the Express API run in **one Node process** on one
domain. `app.js` (project root) starts Express (all `/api/*` routes) and hands
every other request to Next.js. Because they share an origin, the browser calls
`/api/*` relatively — no CORS, no second URL.

## 1. Create the MySQL database (cPanel → MySQL® Databases)
Shared hosts don't allow `CREATE DATABASE` over SQL, so make it in the panel:
1. Create a database — real name becomes `cpuser_spinwheel`.
2. Create a user — real name becomes `cpuser_spinuser` — with a password.
3. Add the user to the database with **ALL PRIVILEGES**.

## 2. Upload the code
Put the project under a folder in your home dir, e.g. `~/spinwheel`, containing
`app.js`, `package.json`, `backend/`, and `frontend/`.

Do **not** upload any `node_modules` folders (there must be a single tree at the
app root — nested copies cause a duplicate-React build/runtime crash). The build
output `frontend/.next` is needed at runtime: either build on the server (step 5)
or build locally with `npm run build` and upload the resulting `frontend/.next`.

## 3. Create the Node.js app (cPanel → Setup Node.js App)
- **Node version:** 18.18+ or 20+
- **Application mode:** Production
- **Application root:** `spinwheel`
- **Application URL:** your domain/subdomain
- **Application startup file:** `app.js`

## 4. Set Environment Variables (in the same screen)
```
DB_HOST=localhost
DB_PORT=3306
DB_USER=cpuser_spinuser
DB_PASSWORD=your-password
DB_NAME=cpuser_spinwheel
DB_SKIP_CREATE=true
NEXT_PUBLIC_API_BASE=            # empty = same origin
# WhatsApp (optional; blank = simulation mode)
WHATSAPP_ACCESS_TOKEN=
WHATSAPP_PHONE_NUMBER_ID=
WHATSAPP_API_VERSION=v25.0
WHATSAPP_TEMPLATE_LANG=en_US
WHATSAPP_DEFAULT_COUNTRY_CODE=91
WHATSAPP_VERIFY_TOKEN=spin-verify
```
Do not set `PORT` — Passenger injects it.

## 5. Install & build
In the Node app screen click **Run NPM Install** (installs the root
`package.json` into a single `node_modules`). Then open the app's terminal
(“Enter to the virtual environment” command shown by cPanel) and run:
```
npm run build
```
`npm run build` runs `NEXT_PUBLIC_API_BASE= next build frontend`, producing a
same-origin production build. (If the host blocks outbound network, the Google
font download during build can fail — in that case build locally and upload
`frontend/.next`.)

## 6. Restart
Click **Restart** on the Node app. The tables are created automatically on first
boot. Visit your domain for the wheel and `/admin` for the dashboard.

## Notes
- Local dev is unchanged: `npm run install:all` then `npm run dev` (two processes
  on ports 3000/4000). The combined `npm run build` / `npm start` are for the
  single-tree deployment; don't run the combined build with nested `node_modules`
  present locally (duplicate React).
- WhatsApp webhook (if used): point Meta to `https://yourdomain/api/...` and use
  `WHATSAPP_VERIFY_TOKEN`.
