# Deploy to cPanel — Long Island Pickleball Pros Automation

## Requirements

- cPanel with **Setup Node.js App** (Node.js Selector)
- **Node.js 22 or higher** (required for built-in SQLite)
- SSH or File Manager access
- Domain or subdomain (e.g. `automation.yourdomain.com`)

---

## Step 1 — Build the deploy package (on your computer)

```powershell
cd "C:\Users\AY Treader\Desktop\Wix Automation"
npm install
npm run build
```

This creates:
- `dist/lipbp-deploy.zip` ← upload this to cPanel
- `dist/lipbp-automation/` ← or upload this folder

**Do NOT upload `node_modules`** — cPanel installs it for you.

---

## Step 2 — Upload to cPanel

1. Login to **cPanel**
2. Open **File Manager**
3. Go to your home folder (e.g. `/home/username/`)
4. Create folder: `lipbp-automation`
5. Upload **`dist/lipbp-deploy.zip`**
6. Right-click → **Extract**
7. Delete the zip after extracting

Your files should look like:
```
/home/username/lipbp-automation/
  server.js          ← startup file
  package.json
  src/
  public/
  data/
```

---

## Step 3 — Create Node.js App in cPanel

1. cPanel → **Setup Node.js App** (or **Application Manager**)
2. Click **Create Application**
3. Fill in:

| Setting | Value |
|---|---|
| Node.js version | **22.x** or latest available |
| Application mode | **Production** |
| Application root | `lipbp-automation` |
| Application URL | `automation.yourdomain.com` (or subdomain you want) |
| Application startup file | **`server.js`** |
| Passenger log file | (leave default) |

4. Click **Create**

---

## Step 4 — Install dependencies

In the Node.js App panel:

1. Click **Run NPM Install**
2. Wait until it completes (installs packages from `package.json`)

Or via SSH:
```bash
cd ~/lipbp-automation
npm install --production
node src/db/init.js
```

---

## Step 5 — Set environment variables (cPanel)

In **Setup Node.js App** → your app → **Environment Variables**, add:

```
NODE_ENV=production
BASE_URL=https://automation.yourdomain.com
TEST_MODE=false
ADMIN_EMAIL=frank@longislandpickleballpros.com
ADMIN_PASSWORD=YourStrongPassword123
JWT_SECRET=long-random-secret-string
RESEND_API_KEY=re_your_key
FROM_EMAIL=onboarding@resend.dev
EMBED_SITE_KEY=lipbp_live_key
```

Copy all values from `.env.production.example` and update with your real URLs and keys.

> cPanel sets `PORT` automatically — do not hardcode it.

---

## Step 6 — Start / Restart the app

1. In Node.js App panel → click **Restart**
2. Visit your URL: `https://automation.yourdomain.com/health`
3. Should return: `{"status":"ok"}`

### Live URLs

| Page | URL |
|---|---|
| Customer form | `https://automation.yourdomain.com/form` |
| Admin login | `https://automation.yourdomain.com/login` |
| Health check | `https://automation.yourdomain.com/health` |

---

## Step 7 — Embed form on Wix

Wix Editor → Add (+) → **Embed Code** → paste:

```html
<script src="https://automation.yourdomain.com/embed/form.js"></script>
<div data-lipbp-form></div>
```

Replace `automation.yourdomain.com` with your real domain.

---

## Step 8 — Make data folder writable

The app stores the SQLite database in `data/`.

In File Manager:
1. Right-click `lipbp-automation/data`
2. **Change Permissions** → `755` or `775`

---

## Troubleshooting

| Problem | Fix |
|---|---|
| App won't start | Check Node.js version is 22+; startup file must be `server.js` |
| 503 error | Click Restart in Node.js App; check error log in cPanel |
| Database error | Ensure `data/` folder exists and is writable |
| Form works but no email | Set `TEST_MODE=false` and add `RESEND_API_KEY` |
| SSL / HTTPS | Enable AutoSSL in cPanel for your subdomain |
| Wrong URLs in emails | Set `BASE_URL` to your full https domain |

---

## Update after changes

1. Edit code locally
2. Run `npm run build`
3. Upload changed files to cPanel (or re-upload zip)
4. **Run NPM Install** if `package.json` changed
5. **Restart** the Node.js app

---

## Security checklist before go-live

- [ ] Change `ADMIN_PASSWORD` from default
- [ ] Set strong `JWT_SECRET`
- [ ] Set `TEST_MODE=false`
- [ ] Enable HTTPS (SSL)
- [ ] Do not expose `.env` file publicly
- [ ] Add Resend API key for real emails
