# Deployment Guide for cPanel (Node.js Backend)

This guide explains how to deploy the **Potobot AI Stylist** backend to a cPanel hosting environment.

## Prerequisites
- cPanel access with **Setup Node.js App** feature enabled.
- Access to File Manager or FTP.

## Step 1: Prepare Files for Upload
1. Create a zip archive of your project folder EXCLUDING `node_modules`.
   - Include: `server/`, `app.js`, `package.json`, `uploads/` (if it has starter content like sets), `.env.local` (rename to `.env` later).
   - **Do not include:** `node_modules`, `.git`.

## Step 2: Upload to cPanel
1. Log in to cPanel.
2. Go to **File Manager**.
3. Create a new folder for your backend (e.g., `potobot-backend`) outside of `public_html` if possible for security, or inside if required by your host.
4. Upload your zip file and extract it.

## Step 3: Configure Node.js App
1. Go to **Setup Node.js App** in cPanel.
2. Click **Create Application**.
3. **Node.js Version:** Select a compatible version (v18 or v20 recommended).
4. **Application Mode:** `Production`.
5. **Application Root:** The path to the folder you just created (e.g., `potobot-backend`).
6. **Application URL:** The public URL where this API will be accessible (e.g., `api.yourdomain.com`).
7. **Application Startup File:** `app.js` (This wrapper file imports the actual server logic).
8. Click **Create**.

## Step 4: Install Dependencies
1. Once created, the button **Run NPM Install** should appear (if `package.json` is detected). Click it.
2. If the button doesn't work or appear, you can SSH into your server, navigate to the folder, and run `npm install`.

## Step 5: Environment Variables
1. cPanel doesn't always read `.env` files automatically in the same way locally.
2. In the **Setup Node.js App** page, look for **Environment Variables**.
3. Add your key:
   - Name: `GEMINI_API_KEY`
   - Value: `your_actual_api_key_here`

## Step 6: Verify and Fix Paths
Since cPanel environments can handle paths differently:
1. Ensure your `uploads` folder has write permissions (chmod 755 or 777 depending on host).
2. The current code expects `uploads/` to be in the project root.

## Step 7: Accessing the API
- Your API will be available at the URL you configured (e.g., `https://api.yourdomain.com/health`).
- Update your Frontend to point to this new URL instead of `http://localhost:3001`.

## Important Note on Ports
- cPanel (via Passenger) handles port binding automatically.
- The `app.js` / `server.js` tries to listen on port 3001. Passenger intercepts this.
- If your app fails to start, modify `server/server.js` to use `process.env.PORT`:
  ```javascript
  const PORT = process.env.PORT || 3001;
  ```
- (Your server.js has been updated to use strictly 3001, so double check if Passenger overrides it or if you need to respect the environment port).

**Recommendation:** I will update `server.js` now to respect `process.env.PORT` to be safe for cPanel.
