# Deploying to cPanel

Three pieces deploy separately:
- **`server/`** → a Node.js app on cPanel (via "Setup Node.js App")
- **`admin/`** → a static site (plain HTML/JS/CSS after build) on a subdomain
- **`mobile/`** → not hosted on cPanel at all — it's rebuilt as an APK pointed at your live backend URL

Recommended domain layout: `api.yourdomain.com` (backend) + `admin.yourdomain.com` (admin panel). Using subdomains avoids path-based routing headaches with the admin panel's client-side router.

## 0. Prerequisites

- cPanel with **"Setup Node.js App"** (Node.js Selector / Passenger) — most shared hosts running CloudLinux have this. If you don't see it in cPanel, ask your host to enable it; without it there's no way to run the Express backend.
- A **MySQL database** via cPanel → MySQL Databases (or MySQL Wizard).
- Node.js **18+** available in the Node.js Selector (check the version dropdown when creating the app).
- SSH access is helpful but not required — everything below can be done through cPanel's UI + the Node.js app's built-in terminal.

## 1. Database

In cPanel → **MySQL Databases**:
1. Create a database, e.g. `youruser_orderbooking`.
2. Create a database user with a strong password, and add it to the database with **All Privileges**.
3. Note the full names — cPanel prefixes both with your account username, e.g. `youruser_orderbooking` / `youruser_obadmin`.

## 2. Backend (`server/`)

### Get the code onto the server
Easiest: cPanel → **Git Version Control** → "Create" → paste `https://github.com/sadiss/orderbooking.git`, set the repository path to something like `orderbooking`, deploy branch `main`. This clones the whole repo; you'll point the Node app at the `server` subfolder.

(No Git Version Control feature? Download the repo as a zip from GitHub and upload/extract via File Manager instead.)

### Create the Node.js app
cPanel → **Setup Node.js App** → Create Application:
- **Node.js version**: 18 or newer
- **Application mode**: Production
- **Application root**: `orderbooking/server` (wherever you cloned it, + `/server`)
- **Application URL**: `api.yourdomain.com`
- **Application startup file**: `dist/index.js`

Click Create. cPanel will show you an "Enter to the virtual environment" command — you'll use the app's **Run NPM Install** button and its terminal for the rest.

### Configure environment variables
In the Node.js app's **Environment Variables** section, add:
```
DATABASE_URL=mysql://youruser_obadmin:YOUR_PASSWORD@localhost:3306/youruser_orderbooking
JWT_SECRET=<generate a long random string — do NOT reuse the dev placeholder>
```
Don't set `PORT` — Passenger assigns and manages this itself; the app already reads `process.env.PORT` with a fallback, so it works either way.

### Install, build, and migrate
Use the "Run NPM Install" button in the Node.js app UI, or its terminal:
```
npm install
npm run build              # compiles TypeScript -> dist/
npx prisma migrate deploy  # creates tables in your MySQL database
npx prisma db seed --schema=prisma/schema.prisma  # optional: or just: npx tsx prisma/seed.ts
```
If `tsx` isn't available in production (it's a dev dependency), run the seed once via `node -e` or temporarily `npm install -D tsx` — or simplest, run seeding once from your local machine pointed at the production `DATABASE_URL` (see note below).

Then **Restart** the app from the Node.js app UI.

### Verify
Visit `https://api.yourdomain.com/health` — should return `{"ok":true}`. If it doesn't load, check the app's error log link in the same cPanel screen.

### Uploads directory
Product images are written to `server/uploads/products/`. Make sure that folder exists and is writable (cPanel File Manager → right-click → permissions, or it'll be created automatically on first upload since the code calls `fs.mkdirSync(..., { recursive: true })`).

## 3. Admin panel (`admin/`)

This is a static build — no Node.js app needed for it, just a web-hosted folder.

### Point it at your live backend
Before building, edit `admin/src/api/client.ts`:
```ts
export const API_BASE_URL = "https://api.yourdomain.com";
```
(Currently hardcoded to `http://localhost:4000` for local dev.)

### Build locally and upload
```
cd admin
npm install
npm run build        # outputs to admin/dist
```
Then upload the **contents** of `admin/dist` (not the folder itself) to the document root of `admin.yourdomain.com` — either via cPanel File Manager, FTP, or by creating the subdomain first (cPanel → Subdomains → create `admin.yourdomain.com`, which gives you a folder like `admin.yourdomain.com/` to upload into).

### Client-side routing fix
The admin panel uses React Router, so refreshing a page like `/products` needs the server to still serve `index.html`. Add a `.htaccess` in that same folder:
```apache
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>
```

### Enable HTTPS
cPanel → SSL/TLS Status → AutoSSL (or manually issue Let's Encrypt certs) for both `api.yourdomain.com` and `admin.yourdomain.com`. This isn't optional for the mobile app — Android blocks plain HTTP by default in production builds, so the backend **must** be served over HTTPS before you rebuild the APK.

## 4. Mobile app (`mobile/`)

Not deployed to cPanel — it's a separate Android build that talks to your now-live backend.

1. Edit `mobile/src/api/config.ts` to point at production instead of your LAN IP:
   ```ts
   export const API_BASE_URL = "https://api.yourdomain.com";
   ```
2. Build a real installable APK/AAB (Expo Go won't work for distributing to your order bookers — you need a standalone build):
   ```
   cd mobile
   npx eas login          # free Expo account
   npx eas build:configure
   npx eas build --platform android --profile preview   # produces an installable .apk
   ```
   EAS Build runs in the cloud and gives you a download link when done. For the Play Store instead of sideloading, use `--profile production` (AAB) and follow Expo's Play Store submission docs.
3. Distribute the APK to booker phones (direct download link, or install manually) — no cPanel involvement here.

## Order of operations

1. Database + backend live and responding to `/health`
2. Admin panel built against that backend, uploaded, HTTPS working
3. Log into the admin panel, confirm you can see seeded data
4. Mobile app rebuilt pointed at the same backend, installed on a test phone
5. Only then hand real devices to your order bookers

## Notes / gotchas

- **`sharp`** (used for product image resizing) installs a native binary — `npm install` on the actual server (not copied from your dev machine) handles this automatically as long as the server has outbound internet access during install.
- **Change `JWT_SECRET`** — do not deploy with the dev placeholder value from this repo.
- If your host's MySQL only listens for connections from `localhost` (typical on shared hosting), `DATABASE_URL` should use `localhost`, not a public host, and the Node app + MySQL need to be on the same account/server (true by default on cPanel).
- Re-running the seed script (`prisma/seed.ts`) is safe — it upserts and won't duplicate the admin/booker accounts or demo data, but you'll likely want to delete the demo products/stores once you've entered real data.
