# Deployment Instructions for Production

## Current Issue
The CORS error occurs because the server at `https://muaziin.sa` is returning the wrong CORS headers. This means the server needs to be updated with the latest code changes.

## Quick Fix Steps

### 1. Update the `.env` file on your production server

SSH into your server and edit the `.env` file in the `backend` directory:

```bash
ssh your-server
cd /path/to/muzan/backend
nano .env  # or use vim/vi
```

Make sure it contains these lines:

```env
NODE_ENV=production
ADMIN_API_PORT=4000

# JWT Configuration
ADMIN_JWT_SECRET=your-super-secret-jwt-key
ADMIN_JWT_EXPIRES_IN=8h

# Database Configuration
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=your-db-user
DB_PASSWORD=your-db-password
DB_NAME=muzan_admin

# IMPORTANT: CORS Origins - Include ALL your domains
CORS_ORIGIN=https://muaziin.sa,https://www.muaziin.sa,https://muaziin.sa

# Public Base URL
PUBLIC_BASE_URL=https://muaziin.sa
```

**CRITICAL**: The `CORS_ORIGIN` line must include all three domains separated by commas with NO SPACES.

### 2. Upload the latest backend code

Upload these updated files to your server:
- `backend/src/config.js`
- `backend/src/app.js`
- Any other modified files

```bash
# From your local machine
scp backend/src/config.js user@server:/path/to/muzan/backend/src/
scp backend/src/app.js user@server:/path/to/muzan/backend/src/
```

Or use Git:

```bash
# On your server
cd /path/to/muzan
git pull origin main
```

### 3. Restart the backend server

**If using PM2:**

```bash
pm2 restart muzan-backend
# or
pm2 restart all

# Check status
pm2 status

# View logs to confirm it's working
pm2 logs muzan-backend
```

**If using systemd:**

```bash
sudo systemctl restart muzan-backend
sudo systemctl status muzan-backend
```

**If using Node directly:**

```bash
# Kill the old process
pkill -f node

# Start the new one
cd /path/to/muzan/backend
npm start
# or with clustering
npm run cluster
```

**If using Passenger (LiteSpeed/cPanel):**

```bash
# Touch restart.txt to restart
mkdir -p tmp
touch tmp/restart.txt

# Or restart through cPanel/LiteSpeed interface
```

### 4. Verify the fix

After restarting, check these URLs in your browser:

1. Open browser console (F12)
2. Visit `https://muaziin.sa`
3. Check the Network tab for API calls to `https://muaziin.sa/api/public/*`
4. The responses should now have the correct CORS headers:
   ```
   Access-Control-Allow-Origin: https://muaziin.sa
   ```

### 5. Test different origins

Also test:
- `https://www.muaziin.sa` (with www)
- `https://muaziin.sa` (direct API access)

All should work without CORS errors.

## Troubleshooting

### Still getting CORS errors?

1. **Check if server restarted:**
   ```bash
   pm2 status  # or your process manager
   ```

2. **Check the logs:**
   ```bash
   pm2 logs muzan-backend --lines 100
   ```

3. **Verify environment variables are loaded:**
   ```bash
   # Add this temporarily to backend/src/server.js
   console.log('CORS Origins:', config.corsOrigin)
   ```

4. **Clear browser cache:**
   - Press Ctrl+Shift+Delete (Windows) or Cmd+Shift+Delete (Mac)
   - Clear cached images and files
   - Or use Incognito/Private mode

5. **Check if .env file exists:**
   ```bash
   ls -la /path/to/muzan/backend/.env
   cat /path/to/muzan/backend/.env | grep CORS_ORIGIN
   ```

6. **Test CORS directly:**
   ```bash
   curl -H "Origin: https://muaziin.sa" \
        -H "Access-Control-Request-Method: GET" \
        -H "Access-Control-Request-Headers: Content-Type" \
        -X OPTIONS \
        https://muaziin.sa/api/public/works
   ```

   Should return headers including:
   ```
   Access-Control-Allow-Origin: https://muaziin.sa
   ```

### Getting 500 Internal Server Error?

This suggests a backend error. Check:

1. **Database connection:**
   ```bash
   mysql -u your-user -p muzan_admin -e "SELECT 1"
   ```

2. **Server logs:**
   ```bash
   pm2 logs muzan-backend --err --lines 50
   ```

3. **Node version:**
   ```bash
   node --version  # Should be >= 14.17.0
   ```

## Complete Deployment Checklist

- [ ] `.env` file has correct `CORS_ORIGIN` with all domains
- [ ] Latest code uploaded to server
- [ ] Dependencies installed (`npm install` in backend directory)
- [ ] Database is accessible
- [ ] Server restarted successfully
- [ ] No errors in server logs
- [ ] Website loads without CORS errors
- [ ] API calls succeed (check Network tab)
- [ ] Test on both `https://muaziin.sa` and `https://www.muaziin.sa`

## Contact

If issues persist, check:
- Server error logs
- Database connection
- Firewall settings
- SSL certificates validity
