# ISB Case Management System - CPanel Deployment Guide

This guide will help you deploy the ISB Case Management System to a CPanel hosting environment with MySQL database.

## Prerequisites

1. CPanel hosting with Node.js support (Node.js 18.x or later)
2. MySQL database access
3. FTP or File Manager access
4. Domain or subdomain configured

---

## Quick Deployment Steps

### Step 1: Create MySQL Database in CPanel

1. Log in to CPanel
2. Go to **MySQL Databases**
3. Create a new database (e.g., `isb_cases`)
4. Create a new MySQL user with a strong password
5. Add the user to the database with **ALL PRIVILEGES**
6. Note down your credentials

### Step 2: Initialize Database Tables

1. Go to **phpMyAdmin** in CPanel
2. Select your database
3. Click on the **Import** tab
4. Upload and execute `database-init.sql`

### Step 3: Prepare Deployment Files

The `cpanel-deployment` folder contains MySQL-specific server files. Before uploading:

1. **Copy the MySQL-specific files** from `cpanel-deployment/server/` and `cpanel-deployment/shared/` to replace the PostgreSQL versions:

```
cpanel-deployment/
├── server/
│   ├── db.ts          <- MySQL database connection
│   └── auth.ts        <- MySQL session store
├── shared/
│   ├── schema.ts      <- MySQL schema definitions
│   └── models/
│       └── auth.ts    <- MySQL auth models
```

2. **Build the application locally:**

```bash
# First, replace the server files with MySQL versions
cp cpanel-deployment/server/db.ts server/db.ts
cp cpanel-deployment/server/auth.ts server/auth.ts
cp cpanel-deployment/shared/schema.ts shared/schema.ts
cp cpanel-deployment/shared/models/auth.ts shared/models/auth.ts

# Then build
npm install
npm run build
```

### Step 4: Upload Files to CPanel

Upload to your application directory:

```
/your-app-folder/
├── dist/
│   ├── index.cjs        (Server bundle)
│   └── public/          (Frontend assets)
├── package.json
├── package-lock.json
├── .env                 (Create from .env.example)
└── app.js              (Startup file - included)
```

### Step 5: Create Environment File

Create `.env` in your application root:

```env
NODE_ENV=production
PORT=3000

# Database Configuration
DB_HOST=localhost
DB_PORT=3306
DB_USER=your_mysql_username
DB_PASSWORD=your_mysql_password
DB_NAME=your_database_name

# Session Secret (change this!)
SESSION_SECRET=your-very-long-random-secret-key-at-least-32-chars
```

### Step 6: Configure Node.js App in CPanel

1. Go to **Setup Node.js App** in CPanel
2. Click **Create Application**
3. Configure:
   - **Node.js Version:** 18.x or 20.x
   - **Application Mode:** Production
   - **Application Root:** Your app folder
   - **Application URL:** Your domain/subdomain
   - **Application Startup File:** `app.js`
4. Click **Create**

### Step 7: Install Dependencies

Via CPanel Terminal:
```bash
cd ~/your-app-folder
source /path/to/virtualenv/bin/activate
npm install --production
```

Or click **Run NPM Install** in the Node.js App interface.

### Step 8: Start the Application

1. Return to **Setup Node.js App**
2. Click **Restart**

### Step 9: Login

Navigate to your application URL and login:
- **Username:** `admin`
- **Password:** `admin123`

**IMPORTANT:** Change this password immediately!

---

## File Structure Explained

### Development (Replit) vs Production (CPanel)

| Component | Development (PostgreSQL) | Production (MySQL) |
|-----------|-------------------------|-------------------|
| Database | PostgreSQL via DATABASE_URL | MySQL via DB_* vars |
| Session Store | connect-pg-simple | express-mysql-session |
| Schema | PostgreSQL types (pgTable) | MySQL types (mysqlTable) |
| Arrays | Native array columns | JSON columns |
| Enums | pgEnum | mysqlEnum |

### What to Upload

```
Required Files:
├── dist/                    # Built application
│   ├── index.cjs           # Server bundle
│   └── public/             # Frontend files
├── package.json            # Dependencies
├── package-lock.json
├── app.js                  # CPanel startup file
├── .env                    # Environment config
└── node_modules/           # After npm install
```

---

## Database Schema

The MySQL database uses the following tables:

- `sessions` - User sessions
- `users` - User accounts
- `user_profiles` - Role and badge info
- `cases` - Case records
- `case_notes` - Notes on cases
- `case_tasks` - Investigation tasks
- `case_reports` - Report submissions
- `report_templates` - Report templates
- `report_attachments` - File attachments
- `audit_logs` - Activity logging

---

## Environment Variables

| Variable | Required | Description |
|----------|----------|-------------|
| NODE_ENV | Yes | Set to `production` |
| PORT | No | Server port (default: 3000) |
| DB_HOST | Yes* | MySQL host (usually `localhost`) |
| DB_PORT | No | MySQL port (default: 3306) |
| DB_USER | Yes* | MySQL username |
| DB_PASSWORD | Yes* | MySQL password |
| DB_NAME | Yes* | Database name |
| DATABASE_URL | Yes* | Alternative: full MySQL URL |
| SESSION_SECRET | Yes | Random string for sessions |

*Either use `DATABASE_URL` or the individual `DB_*` variables.

---

## Troubleshooting

### App Not Starting
- Check Node.js app logs in CPanel
- Verify `.env` file exists with correct values
- Ensure `app.js` points to `./dist/index.cjs`

### Database Connection Failed
- Verify MySQL credentials
- Check database exists and user has privileges
- Ensure host is `localhost` (not `127.0.0.1`)

### 500 Internal Server Error
- Check error logs in CPanel
- Verify `SESSION_SECRET` is set
- Ensure database tables were created

### Login Not Working
- Verify `sessions` table exists
- Check if admin user was created
- Try running `create-admin.sql` again

---

## Security Checklist

- [ ] Changed default admin password
- [ ] Set strong SESSION_SECRET
- [ ] Enabled HTTPS
- [ ] Database user has minimal privileges
- [ ] `.env` file is not publicly accessible
- [ ] Regular backups configured

---

## Default Credentials

**Username:** admin  
**Password:** admin123

**CHANGE IMMEDIATELY AFTER FIRST LOGIN!**

---

## Support

For issues with CPanel deployment, consult:
- Your hosting provider's documentation
- CPanel Node.js documentation: https://blog.cpanel.com/how-to-host-a-node-js-application-with-cpanel/
