# Deployment Guide for Phosphat Report App

This guide will help you deploy the Phosphat Report application on your VPS using the provided `compose.yml` file.

## Prerequisites

- Docker and Docker Compose installed on your VPS
- Git (to clone the repository)
- At least 1GB RAM and 10GB disk space

## Quick Deployment

1. **Clone the repository** to your VPS:
   ```bash
   git clone <your-repo-url>
   cd phosphat-report-app
   ```

2. **Deploy the application**:
   ```bash
   docker-compose -f compose.yml up -d --build
   ```

3. **Check the status**:
   ```bash
   docker-compose -f compose.yml ps
   ```

4. **Access your application**:
   - URL: `http://your-vps-ip:3000`
   - Default login: `superadmin` / `P@ssw0rd123!`

## Environment Variables (Hardcoded in compose.yml)

The following environment variables are already configured in the `compose.yml` file:

- **NODE_ENV**: `production`
- **DATABASE_URL**: `file:/app/data/production.db`
- **SESSION_SECRET**: `your-super-secure-session-secret-change-this-min-32-chars`
- **SUPER_ADMIN**: `superadmin`
- **SUPER_ADMIN_EMAIL**: `admin@yourcompany.com`
- **SUPER_ADMIN_PASSWORD**: `P@ssw0rd123!`
- **MAIL_HOST**: `smtp.gmail.com`
- **MAIL_PORT**: `587`
- **MAIL_USERNAME**: `your-email@gmail.com`
- **MAIL_PASSWORD**: `your-app-password`

## Services Included

### Main Application (`app`)
- **Port**: 3000
- **Database**: SQLite with persistent storage
- **Health Check**: Available at `/health` endpoint
- **Resource Limits**: 512MB RAM, 0.5 CPU

### Backup Service (`backup`)
- **Purpose**: Automatic daily database backups at 2 AM
- **Retention**: Keeps backups for 7 days
- **Location**: `/backup` volume

## Useful Commands

### View logs:
```bash
docker-compose -f compose.yml logs -f app
```

### Stop services:
```bash
docker-compose -f compose.yml down
```

### Restart services:
```bash
docker-compose -f compose.yml restart
```

### Manual backup:
```bash
docker-compose -f compose.yml exec app cp /app/data/production.db /app/data/backup_$(date +%Y%m%d_%H%M%S).db
```

### Check health:
```bash
curl http://localhost:3000/health
```

## Volumes

- **app_data**: Stores the SQLite database
- **app_logs**: Application logs
- **backup_data**: Database backups

## Security Notes

1. **Change default passwords** after first login
2. **Update email settings** in the application
3. **Configure firewall** to only allow necessary ports
4. **Use HTTPS** with a reverse proxy (nginx/traefik) for production

## Troubleshooting

### Application won't start:
```bash
# Check logs
docker-compose -f compose.yml logs app

# Rebuild without cache
docker-compose -f compose.yml build --no-cache app
```

### Database issues:
```bash
# Reset database (WARNING: This will delete all data)
docker-compose -f compose.yml down
docker volume rm $(docker volume ls -q | grep app_data)
docker-compose -f compose.yml up -d
```

### Port conflicts:
If port 3000 is already in use, edit the `compose.yml` file and change:
```yaml
ports:
  - "3001:3000"  # Change 3000 to any available port
```

## Updating the Application

1. **Pull latest changes**:
   ```bash
   git pull origin main
   ```

2. **Rebuild and restart**:
   ```bash
   docker-compose -f compose.yml up -d --build
   ```

## Support

For issues and support:
1. Check the application logs
2. Verify all services are running
3. Test the health endpoint
4. Check database connectivity

The application should be accessible at `http://your-vps-ip:3000` after successful deployment.