# D2C OPs - Setup Guide

## Prerequisites
- Node.js 18+
- MySQL 8+
- npm

## Stack
- Frontend: Vite + React + TypeScript
- Backend: Express + TypeScript
- Database: MySQL
- Migration tool: Knex

## First-time setup

### 1. Install dependencies
From the project root:

```bash
npm run install:all
```

Or install each workspace manually:

```bash
cd server && npm install
cd ../client && npm install
cd ../database && npm install
```

### 2. Configure environment
```bash
cp server/.env.example server/.env
```

Edit `server/.env` with your real values:
- `DATABASE_URL`
- `JWT_SECRET`
- `CLIENT_URL`
- `PORT`
- `SMTP_HOST`
- `SMTP_PORT`
- `SMTP_USER`
- `SMTP_PASS`
- `APP_URL`
- `OPENAI_API_KEY` (optional if AI insights should call OpenAI)

Example database connection:

```env
DATABASE_URL=mysql://root:@localhost:3306/d2c_ops
```

### 3. Create the MySQL database
```sql
CREATE DATABASE d2c_ops;
```

### 4. Run migrations with Knex
From the project root:

```bash
npm run migrate
```

Or directly:

```bash
cd database
npx knex --knexfile knexfile.js migrate:latest
```

### 5. Seed development data
From the project root:

```bash
npm run seed
```

Or directly:

```bash
cd database
npx knex --knexfile knexfile.js seed:run
```

This creates:
- 8 processes: Ignite, Sunking, RDG, MobiHive, Momo, Sanlam, MBA, Muzanu
- 1 super admin: `admin@d2ctelcare.com` / `D2cApollo`
- 1 admin per process: `admin.<process>@d2ctelcare.com` / `1234567`
- Operators, managers, viewers, and clients for each process
- Sample incidents, service checks, automation rules, and on-call schedules

Examples:
- Operator: `ops.ignite@d2ctelcare.com` / `D2c123`
- Manager: `mgr.ignite@d2ctelcare.com` / `D2c123`
- Client: `client.ignite@d2ctelcare.com` / `D2c123`

### 6. Start development servers
From the project root:

```bash
npm run dev
```

Or run them separately:

```bash
# Terminal 1 - API
cd server && npm run dev

# Terminal 2 - Frontend
cd client && npm run dev
```

App runs at: `http://localhost:3000`  
API runs at: `http://localhost:4000`

## Production
- Set a strong `JWT_SECRET`
- Set a production `DATABASE_URL`
- Build frontend: `cd client && npm run build`
- Build backend: `cd server && npm run build`
- Serve `client/dist` through a reverse proxy like Nginx
- Run the backend with a process manager such as PM2
- Configure TLS in your reverse proxy

## Key defaults
| Setting | Value |
|---|---|
| API Port | `4000` |
| Client Port | `3000` |
| DB Engine | MySQL |
| DB Name | `d2c_ops` |
| Migration Tool | Knex |
| Admin Email | `admin@d2ctelcare.com` |