# Deployment Guide for Medklinix Application

This guide outlines the steps to deploy the Client, Server, and the Background Processor (Worker) for the Medklinix System.

## 🏗️ Architecture Overview

The application consists of three main components that need to be deployed:

1.  **Client**: A Next.js frontend application.
2.  **Server**: A Node.js/Express backend API.
3.  **Processor (Worker)**: A background worker for handling asynchronous tasks like sending emails and notifications.

## 📋 Prerequisites

Before deployment, ensure the following services are installed and running:

-   **Node.js** (v18 or higher)
-   **MongoDB** (v6.0 or higher) - For data storage.
-   **Redis** (v6.0 or higher) - Required for the background processor (BullMQ) to manage job queues.

---

## ⚙️ Environment Configuration

### 1. Server & Processor Configuration
Create a `.env` file in the `server/` directory with the following variables:

```env
# Server Configuration
PORT=9899
NODE_ENV=production

# Database Configuration
MONGODB_URI=mongodb://localhost:27017/clinic-management

# Redis Configuration (Required for Processor)
REDIS_HOST=localhost
REDIS_PORT=6379
# REDIS_PASSWORD=your_redis_password  # Uncomment if your Redis instance requires authentication

# Email Service (Resend)
RESEND_API_KEY=your_resend_api_key
EMAIL_FROM=noreply@yourdomain.com

# Stripe Configuration
STRIPE_SECRET_KEY=your_stripe_secret_key
STRIPE_WEBHOOK_SECRET=your_stripe_webhook_secret

# Frontend URL (For CORS and links)
FRONTEND_URL=http://your-frontend-domain.com
```

### 2. Client Configuration
Create a `.env` file in the `client/` directory:

```env
NEXT_PUBLIC_API_URL=http://your-backend-domain.com:9899
```

---

## 🚀 Deployment Steps

### Step 1: Deploy the Server (API)

The server handles API requests and schedules cron jobs.

1.  Navigate to the server directory:
    ```bash
    cd server
    ```

2.  Install dependencies:
    ```bash
    npm install
    ```

3.  Build the TypeScript code:
    ```bash
    npm run build
    ```

4.  Start the server:
    ```bash
    npm start
    ```
    *The server will start on the port defined in `PORT` (default 9899).*

### Step 2: Deploy the Processor (Worker)

The processor is a separate process that consumes jobs from the Redis queue. It shares the same codebase as the server but runs a different entry point.

1.  Navigate to the server directory (if not already there):
    ```bash
    cd server
    ```
    *(Note: The processor uses the same `server` directory and dependencies as the API).*

2.  Ensure dependencies are installed and code is built (same as Step 1).
    ```bash
    npm install
    npm run build
    ```

3.  Start the worker process:
    ```bash
    # Option A: Using the npm script (Development/Easy)
    npm run worker

    # Option B: Running the compiled code (Production Recommended)
    node dist/worker.js
    ```
    
    **Note:** In a production environment, you should run this as a background service (see [Process Management](#-process-management-with-pm2)).

### Step 3: Deploy the Client (Frontend)

1.  Navigate to the client directory:
    ```bash
    cd client
    ```

2.  Install dependencies:
    ```bash
    npm install
    ```

3.  Build the Next.js application:
    ```bash
    npm run build
    ```

4.  Start the client server:
    ```bash
    npm start
    ```
    *The client will start on port 3000 by default.*

---

## 🛠 Process Management with PM2 (Recommended)

For production, it is highly recommended to use **PM2** to manage your processes. PM2 ensures your apps stay online, automatically restart on failure, and can be managed easily.

1.  Install PM2 globally:
    ```bash
    npm install -g pm2
    ```

2.  Start the **Server**:
    ```bash
    cd server
    pm2 start dist/index.js --name "clinic-api"
    ```

3.  Start the **Processor**:
    ```bash
    cd server
    # We use ts-node for the worker script as configured in package.json, 
    # but for production, it's better to run the compiled JS if available.
    # If a compiled worker file exists (e.g., dist/worker.js):
    # pm2 start dist/worker.js --name "clinic-worker"
    
    # If you rely on the npm script:
    pm2 start npm --name "clinic-worker" -- run worker
    ```

4.  Start the **Client**:
    ```bash
    cd client
    pm2 start npm --name "clinic-client" -- start
    ```

5.  Save the process list to restart on reboot:
    ```bash
    pm2 save
    pm2 startup
    ```

## ✅ Verification

To verify that everything is running correctly:

1.  **API**: Visit `http://your-backend-domain:9899/api/health` (if a health check exists) or check logs.
2.  **Client**: Visit `http://your-frontend-domain`.
3.  **Processor**: Check the logs of the worker process. You should see:
    ```
    [Worker] Notification Worker initialized
    ✓ Worker connected to MongoDB
    ```

## 🔍 Troubleshooting

-   **Redis Connection Error**: Ensure Redis is running and accessible. Check `REDIS_HOST` and `REDIS_PORT` in your `.env`. The processor will fail to start if it cannot connect to Redis.
-   **MongoDB Connection Error**: Ensure your connection string in `MONGODB_URI` is correct.
-   **CORS Issues**: Ensure the `FRONTEND_URL` in the server's `.env` matches the actual URL of your deployed client.
