# 🚀 PharmaSFA Pro — Complete Production Setup Guide
## For: https://sfa.apansewa.com

> **Your Details (already configured in this package):**
> - **Website URL:** https://sfa.apansewa.com
> - **Database Name:** apansewa_sfa
> - **Database User:** apansewa_sfa
> - **Database Password:** Tools@720
>
> **No programming knowledge needed.** Just follow each step in order.
> **Total time: ~30 minutes**

---

## 📋 WHAT YOU NEED BEFORE STARTING

1. ✅ cPanel login access (https://apansewa.com/cpanel)
2. ✅ Database `apansewa_sfa` already created (you did this)
3. ✅ The deployment file `pharmasfa-pro-deploy.zip` (download from Preview Panel)

### How to download the deployment file:
- In the Preview Panel (right side of your screen), click **"Open in New Tab"**
- Add `/pharmasfa-pro-deploy.zip` to the URL in the address bar
- Press Enter — the file (32 MB) downloads to your computer

---

## STEP 1: Grant Database Permissions (2 minutes)

Your database user `apansewa_sfa` needs permission to read/write data.

1. **Log into cPanel** → https://apansewa.com/cpanel
2. Scroll down to the **"Databases"** section
3. Click **"MySQL Databases"**
4. Scroll down to **"Add User to Database"** section
5. Select:
   - **User:** `apansewa_sfa`
   - **Database:** `apansewa_sfa`
6. Click **"Add"**
7. On the next page:
   - Check the box at the top: **"ALL PRIVILEGES"** (this checks all boxes)
   - Click **"Make Changes"**
8. ✅ You see: "User apansewa_sfa was successfully added to the database"

---

## STEP 2: Open cPanel File Manager (1 minute)

1. In cPanel, scroll to the **"Files"** section
2. Click **"File Manager"**
3. A popup appears — select **"Web Root (public_html)"** → click **"Go"**
4. ✅ You're now in File Manager, seeing your `public_html` folder

---

## STEP 3: Create the App Folder (1 minute)

1. In File Manager, click the **"+ Folder"** button (top menu)
2. **New Folder Name:** type `sfa`
3. Click **"Create New Folder"**
4. ✅ You see a new folder named `sfa`
5. **Double-click the `sfa` folder** to open it
6. ✅ The address bar shows `/public_html/sfa/`

---

## STEP 4: Upload the Deployment ZIP (5 minutes)

1. Inside the `sfa` folder, click the **"Upload"** button (top menu)
2. A new tab opens for uploading
3. Click **"Select File"** or drag the `pharmasfa-pro-deploy.zip` file into the window
4. Wait for upload to complete (32 MB — takes 2-5 minutes)
5. ✅ You see a green checkmark or "Upload Complete" message
6. Click **"Go Back to /home/apansewa/public_html/sfa"** at the bottom
7. ✅ You see `pharmasfa-pro-deploy.zip` in the `sfa` folder

---

## STEP 5: Extract the ZIP File (2 minutes)

1. **Right-click** on `pharmasfa-pro-deploy.zip`
2. Click **"Extract"**
3. A popup appears — the path should be `/public_html/sfa/`
4. Click **"Extract File(s)"**
5. ✅ A list of extracted files appears
6. Click **"Close"**

7. You now see a folder named **`deploy-package`**
8. **Double-click `deploy-package`** to open it
9. You see files: `app` (folder), `.env`, `DEPLOY-GUIDE.md`, `schema.sql`, etc.

10. **Select ALL files** inside `deploy-package`:
    - Click the first item (`app` folder)
    - Hold **Shift** key → click the last item
    - All should be highlighted

11. **Right-click** any highlighted file → click **"Move"**
12. In the popup, change the path to: `/public_html/sfa/`
13. Click **"Move File(s)"**
14. ✅ All files moved to `/public_html/sfa/`

15. **Go up one level** (click `↑` or the `sfa` breadcrumb at top)
16. **Delete** the now-empty `deploy-package` folder:
    - Right-click `deploy-package` → **Delete** → **Confirm**
17. **Delete** the `pharmasfa-pro-deploy.zip` file too (no longer needed):
    - Right-click it → **Delete** → **Confirm**

18. ✅ Your `sfa` folder now contains: `app`, `.env`, `DEPLOY-GUIDE.md`, `schema.sql`, etc.

---

## STEP 6: Verify the .env File (2 minutes) ⭐ IMPORTANT

The `.env` file contains your database credentials — it's **already configured** for you.

1. **Enable hidden files** (if you don't see `.env`):
   - Click **"Settings"** (top right of File Manager)
   - Check **"Show Hidden Files (dotfiles)"**
   - Click **"Save"**

2. You should now see a file named **`.env`** in `/public_html/sfa/`

3. **Right-click** on `.env` → click **"Edit"**
4. A popup about encoding appears → click **"Edit"**
5. The file opens in a text editor. You should see:

```
DATABASE_URL="mysql://apansewa_sfa:Tools%40720@localhost:3306/apansewa_sfa"
NEXTAUTH_SECRET="k7mX9pQ2vR8wY3tF6nB1cL5jH4gZ0aD8sE2tU7iO3p"
NEXTAUTH_URL="https://sfa.apansewa.com"
NODE_ENV="production"
PORT=3000
```

6. **Verify these are correct** (they should be — no changes needed!)
7. Click **"Save Changes"** (top right) → click **"Close"**

> **Note:** The `%40` in the password is the URL-encoded version of `@`. Your real password is `Tools@720` — the encoding is just so the database connection string works correctly.

✅ Your `.env` file is configured correctly.

---

## STEP 7: Set Up the Node.js App in cPanel (5 minutes) ⭐ CRITICAL STEP

This is the step that makes your website live.

1. Go back to **cPanel Home** (click "Home" at top)
2. Scroll to the **"Software"** section
3. Click **"Setup Node.js App"**

   > ⚠️ **Don't see "Setup Node.js App"?**
   > Your hosting plan doesn't support Node.js. Contact your hosting provider and ask them to enable Node.js for your account, OR upgrade to a plan that supports it.

4. Click the **"Create Application"** button (top right)

5. Fill in the form **EXACTLY** like this:

   | Field | What to select/type |
   |-------|-------------------|
   | **Node.js version** | `18.20.8` (or the highest 18.x or 20.x available) |
   | **Application mode** | `Production` |
   | **Application root** | `sfa/app` |
   | **Application URL** | `sfa.apansewa.com` (select from dropdown) |
   | **Application startup file** | `app.js` ← **VERY IMPORTANT: type this exactly!** |
   | **Passenger log file** | Leave as default |

   > ⚠️ **CRITICAL:** The "Application startup file" MUST be **`app.js`** (NOT `server.js`). If you put `server.js`, you'll see "It works!" instead of your app.

6. Scroll down to **"Environment variables"** section
7. Click **"+ Add Variable"** and add these 4 variables (one at a time):

   | Variable name | Variable value |
   |---------------|----------------|
   | `DATABASE_URL` | `mysql://apansewa_sfa:Tools%40720@localhost:3306/apansewa_sfa` |
   | `NEXTAUTH_SECRET` | `k7mX9pQ2vR8wY3tF6nB1cL5jH4gZ0aD8sE2tU7iO3p` |
   | `NEXTAUTH_URL` | `https://sfa.apansewa.com` |
   | `NODE_ENV` | `production` |

8. Click **"Create"** (or "Save") at the bottom

9. ✅ You see: **"Application created successfully"** with a green "Running" indicator

---

## STEP 8: Install Dependencies (3 minutes)

1. On the "Setup Node.js App" page, find your app in the list
2. Click the **pencil icon (Edit)** next to your app
3. Scroll down to find the **"Run NPM Install"** button
4. Click **"Run NPM Install"**
5. Wait 1-2 minutes — you'll see output in the box below
6. ✅ You see something like: **"added 350 packages in 45s"**

> If you see errors here, take a screenshot and continue to Step 9 — sometimes it still works.

---

## STEP 9: Generate the Prisma Database Client (2 minutes) ⭐ CRITICAL

This step connects your app to the MySQL database. **Without it, the app won't read/write data.**

1. On the same "Setup Node.js App" edit page, find the **"Run script"** field
2. In the text box next to "Run script", type:
   ```
   npx prisma generate
   ```
3. Click the **"Run script"** button
4. Wait 30 seconds — you'll see output like:
   ```
   ✔ Generated Prisma Client (v6.19.2) to ./node_modules/@prisma/client
   ```
5. ✅ You see: **"Generated Prisma Client"**

> **If this fails**, open cPanel File Manager → go to `/public_html/sfa/app/` and check if the `prisma` folder (containing `schema.prisma`) is there. If missing, re-extract the zip file and move the `app` folder.

---

## STEP 10: Restart the App (30 seconds)

1. On the "Setup Node.js App" page, find your app
2. Click the **"Restart"** button (circular arrow icon)
3. Wait 10 seconds
4. ✅ You see **"Running"** with a green dot

---

## STEP 11: Test Your Website! 🎉 (2 minutes)

1. Open a new browser tab
2. Type your URL: **https://sfa.apansewa.com**
3. Press Enter

4. ✅ **You should see the PharmaSFA Pro login page** (green/teal design with a doctor icon)

> ⚠️ **If you see "It works!" instead:**
> - Go back to cPanel → Setup Node.js App → Edit your app
> - Check that "Application startup file" is `app.js` (not `server.js`)
> - Click "Restart"
> - Hard refresh your browser: Ctrl+Shift+R (Windows) or Cmd+Shift+R (Mac)

5. **Log in:**
   - Username: `admin`
   - Password: `admin@123`
   - Click **"Continue"**

6. MFA screen appears:
   - Type any 6 digits (like `123456`)
   - Click **"Verify & Login"**

7. ✅ **You see the Dashboard** with charts, KPIs, and menus!

8. **Verify the database is connected:**
   - In the left sidebar, click **"Database (MSfa_db)"** (under "System" group)
   - Scroll down — you should see your real database tables listed
   - At the top, the "Database Connection Configuration" card shows your `apansewa_sfa` database

9. **Verify real data is showing:**
   - Click **"Doctors"** in the left sidebar (under "Masters")
   - You should see 10 doctors (from the schema.sql you imported earlier)
   - Click **"Products"** — you should see 8 products
   - Click **"Employees"** — you should see 8 employees

🎉 **CONGRATULATIONS! Your PharmaSFA Pro is LIVE!**

---

## STEP 12: Enable SSL / HTTPS (5 minutes)

Make your site secure with the lock icon 🔒.

1. Go back to cPanel Home
2. Scroll to the **"Security"** section
3. Click **"SSL/TLS Status"**
4. Click **"Run AutoSSL"** (top right button)
5. Wait 2-5 minutes — it generates free SSL certificates
6. ✅ You see green locks next to your domains
7. Visit **https://sfa.apansewa.com** — you should see a **🔒 lock icon** in the browser address bar

---

## STEP 13: Change the Admin Password (3 minutes) ⭐ IMPORTANT FOR SECURITY

The default password `admin@123` is publicly known. Change it now.

1. Go to cPanel → **"phpMyAdmin"** (in the Databases section)
2. In the left sidebar, click on **`apansewa_sfa`**
3. You see a list of tables — click on the **`users`** table
4. You see the `admin` user — click **"Edit"** (pencil icon) next to it
5. Find the row named **`password_hash`**
6. The current value is a long string starting with `$2b$12$...`
7. **Generate a new password hash:**
   - Open a new browser tab → go to: https://bcrypt-generator.com/
   - Type your new password (e.g., `MySecurePass2025!`)
   - Click **"Hash"** or **"Generate"**
   - Copy the result (starts with `$2b$12$...`)

8. **Paste the new hash** into the `password_hash` field in phpMyAdmin
9. Click **"Go"** (or "Save") at the bottom
10. ✅ Password updated!

Now log out and log back in with your new password to test it.

---

## 🆘 TROUBLESHOOTING — Common Problems & Solutions

### Problem 1: "It works!" message instead of your app
**Cause:** cPanel is looking for `app.js` but can't find it, or the startup file is set to `server.js`
**Fix:**
1. cPanel → File Manager → go to `/public_html/sfa/app/`
2. Verify the file `app.js` exists (if not, re-upload from the deployment zip)
3. cPanel → Setup Node.js App → Edit your app
4. Set "Application startup file" to `app.js`
5. Click "Restart"
6. Hard refresh your browser (Ctrl+Shift+R)

### Problem 2: "This site can't be reached" or "502 Bad Gateway"
**Cause:** The Node.js app crashed or isn't running
**Fix:**
1. cPanel → Setup Node.js App
2. Check if your app shows "Running" (green dot). If red, click "Restart"
3. Wait 30 seconds, try again

### Problem 3: Website loads but shows "demo mode" (no real data)
**Cause:** Database connection failed — the app is using fallback mock data
**Fix:**
1. Visit https://sfa.apansewa.com/api/health in your browser
2. If it says `"db":"disconnected"`, your `DATABASE_URL` is wrong
3. Check cPanel → File Manager → `/public_html/sfa/.env`:
   - The DATABASE_URL should be: `mysql://apansewa_sfa:Tools%40720@localhost:3306/apansewa_sfa`
   - **The `%40` is correct** — don't change it to `@`
4. Verify database permissions (Step 1)
5. Restart the app

### Problem 4: "Cannot find module '@prisma/client'" error
**Cause:** Prisma client not generated for your server's OS
**Fix:**
1. cPanel → Setup Node.js App → Edit your app
2. Click "Run NPM Install" again
3. Then in "Run script" box, type: `npx prisma generate` → click "Run script"
4. Click "Restart"

### Problem 5: "Loading chunk app/page failed" error in browser
**Cause:** Browser cached old files
**Fix:**
- Hard refresh: **Ctrl + Shift + R** (Windows) or **Cmd + Shift + R** (Mac)
- Or try a private/incognito window
- Or clear browser cache completely

### Problem 6: Charts don't show / blank page
**Cause:** JavaScript error in the browser
**Fix:**
1. Press F12 to open browser DevTools
2. Click "Console" tab — look for red error messages
3. Most common fix: restart the app in cPanel (Setup Node.js App → Restart)

### Problem 7: "Cannot find module 'next'" error
**Cause:** Dependencies not installed
**Fix:**
1. cPanel → Setup Node.js App → Edit your app
2. Click "Run NPM Install"
3. Wait for it to complete
4. Click "Restart"

### Problem 8: Can't find "Setup Node.js App" in cPanel
**Cause:** Your hosting plan doesn't support Node.js
**Fix:**
- Contact your hosting provider: "How do I run a Node.js app on my account?"
- Or switch to a Node-friendly host (Vercel, Render, Railway — all have free tiers)

---

## 📞 QUICK REFERENCE CARD

| Item | Your Value |
|------|-----------|
| **Website URL** | https://sfa.apansewa.com |
| **Database Name** | apansewa_sfa |
| **Database User** | apansewa_sfa |
| **Database Password** | Tools@720 |
| **App Folder** | /public_html/sfa/app |
| **Startup File** | app.js |
| **Application Root** | sfa/app |
| **Default Login** | admin / admin@123 (change in Step 13!) |
| **Health Check URL** | https://sfa.apansewa.com/api/health |
| **DATABASE_URL** | mysql://apansewa_sfa:Tools%40720@localhost:3306/apansewa_sfa |

---

## ✅ COMPLETE STEP SUMMARY

| Step | What you do | Time | Status |
|------|-------------|------|--------|
| 1 | Grant database permissions | 2 min | ☐ |
| 2 | Open File Manager | 1 min | ☐ |
| 3 | Create `sfa` folder | 1 min | ☐ |
| 4 | Upload `pharmasfa-pro-deploy.zip` | 5 min | ☐ |
| 5 | Extract the zip | 2 min | ☐ |
| 6 | Verify `.env` file | 2 min | ☐ |
| 7 | Set up Node.js App (startup: app.js) | 5 min | ☐ |
| 8 | Run NPM Install | 3 min | ☐ |
| 9 | Run `npx prisma generate` | 2 min | ☐ |
| 10 | Restart the app | 30 sec | ☐ |
| 11 | Test your website! 🎉 | 2 min | ☐ |
| 12 | Enable SSL (HTTPS) | 5 min | ☐ |
| 13 | Change admin password | 3 min | ☐ |

**Total time: ~35 minutes**

---

## 🎯 MOST IMPORTANT THINGS TO REMEMBER

1. **Startup file = `app.js`** (NOT server.js) — this is the #1 reason for "It works!" errors
2. **Application root = `sfa/app`** — points to the folder with app.js
3. **Password encoding = `Tools%40720`** — the @ must be %40 in the DATABASE_URL
4. **Always click "Restart"** after making any change in cPanel
5. **Hard refresh browser** (Ctrl+Shift+R) if you see cached old pages

---

**Good luck! Your PharmaSFA Pro will be live at https://sfa.apansewa.com** 🚀
