# Phosphat Report - Production Deployment Guide

This guide will help you deploy the Phosphat Report application on your Dockploy VPS using Docker Compose.

## Prerequisites

- Docker and Docker Compose installed on your VPS
- Domain name configured (optional but recommended)
- SSL certificate (handled by Traefik if using reverse proxy)

## Quick Start

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

2. **Configure environment variables**:
   ```bash
   cp .env.production .env
   nano .env  # Edit with your production values
   ```

3. **Deploy the application**:
   ```bash
   chmod +x deploy.sh
   ./deploy.sh deploy
   ```

## Environment Configuration

Edit the `.env` file with your production values:

### Required Settings
```env
# Change these values for security
SESSION_SECRET="your-super-secure-session-secret-min-32-chars"
SUPER_ADMIN_PASSWORD="YourSecurePassword123!"
SUPER_ADMIN_EMAIL="admin@yourcompany.com"

# Your domain (for Traefik labels)
DOMAIN=your-domain.com
```

### Optional Settings
```env
# Custom port (default: 3000)
APP_PORT=3000

# Storage paths
DATA_PATH=./data
BACKUP_PATH=./backups

# Email configuration (for password reset)
MAIL_HOST=smtp.gmail.com
MAIL_USERNAME=your-email@gmail.com
MAIL_PASSWORD=your-app-password
```

## Deployment Commands

The `deploy.sh` script provides several useful commands:

```bash
# Deploy application
./deploy.sh deploy

# Stop services
./deploy.sh stop

# Restart services
./deploy.sh restart

# View logs
./deploy.sh logs

# Create database backup
./deploy.sh backup

# Check status
./deploy.sh status
```

## Manual Deployment

If you prefer manual deployment:

```bash
# Create directories
mkdir -p data backups logs

# Build and start services
docker-compose up -d --build

# Check status
docker-compose ps
docker-compose logs app
```

## Dockploy Integration

For Dockploy deployment:

1. **Create a new application** in Dockploy
2. **Set the repository** URL
3. **Configure environment variables** in Dockploy UI
4. **Set build command**: `docker-compose build`
5. **Set start command**: `docker-compose up -d`

### Dockploy Environment Variables

Add these in the Dockploy environment variables section:

```
NODE_ENV=production
SESSION_SECRET=your-super-secure-session-secret
SUPER_ADMIN=superadmin
SUPER_ADMIN_EMAIL=admin@yourcompany.com
SUPER_ADMIN_PASSWORD=YourSecurePassword123!
DOMAIN=your-domain.com
```

## Database Management

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

# Automated backup (runs daily at 2 AM)
# Configured in docker-compose.yml backup service
```

### Restore
```bash
# Stop application
docker-compose stop app

# Restore database
cp backups/backup_YYYYMMDD_HHMMSS.db data/production.db

# Start application
docker-compose start app
```

## Monitoring

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

### Logs
```bash
# Application logs
docker-compose logs -f app

# All services logs
docker-compose logs -f
```

### Resource Usage
```bash
docker stats phosphat-report-app
```

## Reverse Proxy (Traefik)

The application includes Traefik labels for automatic SSL and routing. If you're using Traefik:

1. Ensure Traefik is running on your server
2. Set the `DOMAIN` environment variable
3. The application will be automatically available at `https://your-domain.com`

## Security Considerations

1. **Change default passwords** in `.env`
2. **Use strong session secret** (minimum 32 characters)
3. **Enable firewall** on your VPS
4. **Regular backups** are configured automatically
5. **Keep Docker images updated**

## Troubleshooting

### Application won't start
```bash
# Check logs
docker-compose logs app

# Check database permissions
ls -la data/

# Rebuild without cache
docker-compose build --no-cache
```

### Database issues
```bash
# Reset database (WARNING: This will delete all data)
docker-compose down
rm -f data/production.db
docker-compose up -d
```

### Performance issues
```bash
# Check resource usage
docker stats

# Increase memory limits in docker-compose.yml
# Optimize database queries
```

## Updates

To update the application:

```bash
# Pull latest code
git pull origin main

# Rebuild and restart
docker-compose up -d --build

# Check logs
docker-compose logs -f app
```

## Support

For issues and support:
1. Check the logs: `docker-compose logs app`
2. Verify environment variables
3. Check database connectivity
4. Review health endpoint: `/health`