# Testing the IHMS CMS Backend Locally with XAMPP

This guide explains how to run the PHP backend on your local computer using **XAMPP**.

---

## Step 1: Install XAMPP

1. Download XAMPP from: https://www.apachefriends.org
2. Install it (recommended path: `C:\xampp` on Windows).
3. Make sure these components are selected during installation:
   - Apache
   - MySQL (MariaDB)
   - PHP 8.1 or higher

---

## Step 2: Start Apache and MySQL

1. Open the **XAMPP Control Panel**.
2. Click **Start** next to **Apache**.
3. Click **Start** next to **MySQL**.
4. Both should show green.

---

## Step 3: Place the Backend Files

### Option A: Use the existing `backend/` folder in your project

Copy or move the entire `backend` folder from your project into the XAMPP web root:

```
C:\xampp\htdocs\ihms-cms-backend
```

So the structure becomes:

```
C:\xampp\htdocs\ihms-cms-backend\
├── .env.example
├── composer.json
├── config\
├── database\
├── public\          ← This is the document root
├── src\
└── uploads\
```

### Option B: Extract the zip I created

1. Extract `ihms-cms-backend-cpanel.zip`.
2. Move the extracted folder to:
   ```
   C:\xampp\htdocs\ihms-cms-backend
   ```

---

## Step 4: Create the Database

1. Open your browser and go to:
   ```
   http://localhost/phpmyadmin
   ```
2. Click **New** on the left sidebar.
3. Enter database name: `ihms_cms`
4. Click **Create**.
5. Select the new `ihms_cms` database.
6. Click the **SQL** tab at the top.
7. Open the file `backend/database/migration.sql` in a text editor.
8. Copy all the SQL and paste it into phpMyAdmin.
9. Click **Go**.

You should see success messages and the tables created.

---

## Step 5: Configure Environment Variables

1. In the backend folder, find `.env.example`.
2. Copy it and rename the copy to `.env`.
3. Open `.env` and make sure it matches your XAMPP setup:

```env
APP_NAME="IHMS CMS API"
APP_ENV=local
APP_DEBUG=true
APP_URL=http://localhost:4000

# XAMPP default database settings
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ihms_cms
DB_USERNAME=root
DB_PASSWORD=

JWT_SECRET=local-dev-secret-key-change-in-production
JWT_ISSUER=ihms-cms-api
JWT_EXPIRY=3600

CORS_ALLOWED_ORIGINS=http://localhost:3000,http://localhost:3001

UPLOAD_MAX_SIZE=5242880
UPLOAD_ALLOWED_TYPES=jpg,jpeg,png,gif,webp,svg,pdf,doc,docx
UPLOAD_PATH=uploads
```

> **Note:** In XAMPP, the default `root` user usually has **no password**.

---

## Step 6: Configure Apache Virtual Host (Recommended)

To make the backend available at `http://localhost:4000` like the frontend expects, set up a virtual host.

### For XAMPP on Windows:

1. Open Notepad as **Administrator**.
2. Open this file:
   ```
   C:\xampp\apache\conf\extra\httpd-vhosts.conf
   ```
3. Add this at the bottom:

```apache
<VirtualHost *:4000>
    DocumentRoot "C:/xampp/htdocs/ihms-cms-backend/public"
    ServerName localhost
    <Directory "C:/xampp/htdocs/ihms-cms-backend/public">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
```

4. Open this file as Administrator:
   ```
   C:\xampp\apache\conf\httpd.conf
   ```
5. Find this line and remove the `#` at the beginning if it exists:
   ```apache
   #LoadModule vhost_alias_module modules/mod_vhost_alias.so
   ```
   Change to:
   ```apache
   LoadModule vhost_alias_module modules/mod_vhost_alias.so
   ```
6. Also in `httpd.conf`, find:
   ```apache
   Listen 80
   ```
   Add below it:
   ```apache
   Listen 4000
   ```
7. Save both files.
8. Restart Apache from the XAMPP Control Panel.

---

## Step 7: Test the API

### 1. Health Check

Open in browser:
```
http://localhost:4000/api/health
```

Expected response:
```json
{
  "status": "success",
  "data": {
    "status": "ok",
    "timestamp": "2026-07-23T09:00:00+01:00",
    "version": "1.0.0"
  }
}
```

### 2. Test Login

You can use Postman, Insomnia, or cURL.

**Using cURL:**

Open Command Prompt and run:

```bash
curl -X POST http://localhost:4000/api/auth/login ^
  -H "Content-Type: application/json" ^
  -d "{\"email\":\"admin@ihms.com\",\"password\":\"admin123\"}"
```

Expected response:
```json
{
  "status": "success",
  "message": "Login successful",
  "data": {
    "token": "eyJhbGciOiJIUzI1NiIs...",
    "admin": {
      "id": "...",
      "name": "Super Admin",
      "email": "admin@ihms.com",
      "role": "super_admin"
    }
  }
}
```

### 3. Test a Protected Route

Copy the token from the login response and use it:

```bash
curl -X GET http://localhost:4000/api/blogs ^
  -H "Authorization: Bearer YOUR_TOKEN_HERE"
```

Expected response:
```json
{
  "status": "success",
  "data": [],
  "meta": {
    "total": 0,
    "page": 1,
    "limit": 10,
    "totalPages": 0,
    "hasNext": false,
    "hasPrev": false
  }
}
```

---

## Step 8: Test from Your Next.js Frontend

1. In your frontend project, create or update `.env.local`:

```env
NEXT_PUBLIC_APP_API_URL=http://localhost:4000
```

2. Restart your Next.js dev server:

```bash
npm run dev
```

3. Open the frontend in the browser and try logging in.

---

## Alternative: Without Virtual Host (Simplest Method)

If you don't want to set up `localhost:4000`, you can access the backend directly through XAMPP's default port:

```
http://localhost/ihms-cms-backend/public/api/health
```

Then set your frontend env to:

```env
NEXT_PUBLIC_APP_API_URL=http://localhost/ihms-cms-backend/public
```

This works immediately without editing Apache config files.

---

## Common XAMPP Issues

| Issue | Solution |
|-------|----------|
| Apache won't start | Make sure port 80 or 4000 isn't used by another app (Skype, IIS, etc.) |
| MySQL won't start | Check if another MySQL service is already running |
| `404 Not Found` | Make sure `.htaccess` exists in `public/` and `mod_rewrite` is enabled |
| `500 Internal Server Error` | Check `C:\xampp\apache\logs\error.log` and set `APP_DEBUG=true` |
| Database connection failed | Verify DB username/password in `.env`. Default XAMPP root has no password |
| CORS errors | Add `http://localhost:3000` to `CORS_ALLOWED_ORIGINS` in `.env` |
| Upload fails | Make sure `uploads/` folder exists and is writable |

---

## Quick Reference: Folder Paths

| What | Path |
|------|------|
| XAMPP install | `C:\xampp` |
| Web root | `C:\xampp\htdocs` |
| Backend project | `C:\xampp\htdocs\ihms-cms-backend` |
| Public folder | `C:\xampp\htdocs\ihms-cms-backend\public` |
| Apache config | `C:\xampp\apache\conf\httpd.conf` |
| Virtual hosts | `C:\xampp\apache\conf\extra\httpd-vhosts.conf` |
| Apache error log | `C:\xampp\apache\logs\error.log` |
| phpMyAdmin | `http://localhost/phpmyadmin` |

---

## Default Login Credentials

After running the migration:

| Email | Password | Role |
|-------|----------|------|
| `admin@ihms.com` | `admin123` | `super_admin` |
| `editor@ihms.com` | `admin123` | `editor` |

> Change these passwords in production.
