# Deployment Guide — AgriBot App

This document describes how to prepare and deploy the AgriBot app to a cPanel subdomain (two deployment flavors).

Important: This project contains a backend Node server and a Vite React frontend. For a production deployment, you can either host the frontend as static files (recommended on standard shared hosting) or host the full Node backend (if your cPanel supports Node/Passenger).

---

## Quick prepare (build + package)

From project root, run:

```bash
# build backend (ts -> dist) and frontend (vite build)
npm run prepare:deploy
```

This does:
- `backend` TypeScript build -> `backend/dist`
- `frontend` build -> `frontend/dist`
- Copies `frontend/dist` -> `backend/public`

The folder `backend/public` is what you upload to a static host or that the backend serves when running on a host that supports Node.

---

## Option A — Static-only (recommended for cPanel shared hosting)

Use this when you only want the frontend UI on the cPanel subdomain. Host APIs separately (recommended: VPS, Render, Railway, Fly, etc.).

1. In cPanel, create a subdomain (e.g. `agri.yourdomain.com`). Note the subdomain document root (e.g. `public_html/agri`).
2. Run `npm run build:frontend` locally (or use `npm run build:deploy` then upload `backend/public` contents).
3. Upload the contents of `frontend/dist` (or `backend/public`) into the subdomain document root.
4. Ensure `index.html` is in the root and that `assets/` is present.
5. (Optional) Add the `.htaccess` file (see `backend/public/.htaccess`) to enable SPA routing on Apache.
6. Configure the frontend to call your backend API URL (set `VITE_API_URL` or adjust `frontend/src/config/api.ts`).

Notes:
- Backend APIs must be hosted separately and reachable by the frontend (CORS must be configured).
- WebSockets are not handled by static hosting — backend must be hosted somewhere that supports WebSocket.

---

## Option B — Full Node app on cPanel (if Node/Passenger supported)

Use cPanel _Setup Node.js App_ (Passenger) if your host supports Node apps.

1. Run `npm run prepare:deploy` locally to produce `backend/dist` and `backend/public`.
2. Upload the `backend` folder (including `dist`, `public`, and `package.json`) to the subdomain directory on the server (or clone the repo and run builds on the server if allowed).
3. In cPanel > Setup Node.js App:
   - Select the application root (the uploaded `backend` folder).
   - Set `Application startup file` to `dist/server.js`.
   - Set environment variables (see `backend/.env.example`) using the cPanel UI: `JWT_SECRET`, `NODE_ENV=production`, and any DB credentials if used.
   - Run `npm install --production` in the backend folder (or upload `node_modules` if required by host).
4. Start the app using cPanel. The app will be exposed on the assigned subdomain.

Notes & Caveats:
- Ensure your host supports WebSocket (Passenger generally supports it, but confirm with your provider).
- The server listens on `process.env.PORT || 3001` — cPanel sets the port automatically for Node apps.
- For production, replace the in-memory user store with a real database and set `JWT_SECRET` securely.

---

## .htaccess (Apache SPA fallback)

If hosting static files on Apache, include `.htaccess` in the frontend root (or `backend/public`) to ensure SPA routes fallback to `index.html`. See `backend/public/.htaccess` in repository.

---

## Environment variables (minimum)

Example (see `backend/.env.example`):

- `NODE_ENV=production`
- `PORT` (optional)
- `JWT_SECRET` (REQUIRED)

Optional / future:
- Database connection string (if you replace in-memory storage)
- `RTK_CONFIG` or hardware-specific secrets

---

## Checklist before going live

- [ ] `npm run prepare:deploy` completed successfully locally
- [ ] `backend/public` uploaded to cPanel (static) or `backend` uploaded (Node)
- [ ] Environment variables configured in cPanel
- [ ] CORS configured on backend if API on different origin
- [ ] WebSocket tested (if using real-time features)
- [ ] TLS/HTTPS enabled for the subdomain
- [ ] Secure `JWT_SECRET` set

---

If you want, I can add a short `DEPLOY.md` with step-by-step screenshots or create a small script that packages `backend` into a zip ready to upload to cPanel. Which would you prefer?