# Dokploy Deployment Guide

This guide will help you deploy the Phosphat Report application on Dokploy.

## Step 1: Prepare Your Repository

1. **Fix Git Issues** (if you encountered them):
   ```bash
   # Remove temporary Excel files
   git rm --cached "public/~$16-6 Petra.xlsx"
   
   # Configure Git line endings for Windows
   git config core.autocrlf true
   
   # Add and commit files
   git add .
   git commit -m "Initial commit for Dokploy deployment"
   git push origin main
   ```

## Step 2: Create Application in Dokploy

1. **Login to your Dokploy dashboard**
2. **Click "Create Application"**
3. **Choose "Docker Compose"**
4. **Fill in the details**:
   - **Name**: `phosphat-report` (or your preferred name)
   - **Repository URL**: Your Git repository URL
   - **Branch**: `main`
   - **Build Path**: `/` (root directory)

## Step 3: Configure Environment Variables

In the Dokploy environment variables section, add these variables:

### Required Variables
```
NODE_ENV=production
SESSION_SECRET=your-super-secure-session-secret-change-this-min-32-chars
SUPER_ADMIN=superadmin
SUPER_ADMIN_EMAIL=admin@yourcompany.com
SUPER_ADMIN_PASSWORD=YourSecurePassword123!
```

### Optional Variables
```
APP_PORT=3000
DOMAIN=your-domain.com
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_SECURE=false
MAIL_USERNAME=your-email@gmail.com
MAIL_PASSWORD=your-app-password
MAIL_FROM_NAME=Phosphat Report System
MAIL_FROM_EMAIL=noreply@yourcompany.com
LOG_LEVEL=info
```

## Step 4: Deploy

1. **Click "Deploy"** in Dokploy
2. **Monitor the build logs** for any errors
3. **Wait for deployment to complete**

## Step 5: Access Your Application

Once deployed, your application will be available at:
- **HTTP**: `http://your-server-ip:3000`
- **HTTPS**: `https://your-domain.com` (if domain is configured)

## Troubleshooting

### Common Issues

1. **Volume Mount Errors**:
   - The updated docker-compose.yml uses Docker volumes instead of bind mounts
   - This should resolve the "no such file or directory" error

2. **Build Failures**:
   - Check the build logs in Dokploy
   - Ensure all environment variables are set correctly
   - Verify your repository is accessible

3. **Database Issues**:
   - The database will be created automatically on first run
   - Check container logs if the application fails to start

### Checking Logs

In Dokploy:
1. Go to your application
2. Click on "Logs" tab
3. Select the container (app or backup)
4. View real-time logs

### Health Check

Once deployed, check if the application is healthy:
```bash
curl http://your-server-ip:3000/health
```

Should return:
```json
{
  "status": "ok",
  "timestamp": "2025-07-24T10:30:00.000Z",
  "uptime": 123.45,
  "environment": "production",
  "database": "connected"
}
```

## Database Management

### Backup
The backup service runs automatically daily at 2 AM. To manually backup:

1. **Access the container**:
   ```bash
   docker exec -it phosphat-report-app sh
   ```

2. **Create backup**:
   ```bash
   cp /app/data/production.db /app/data/backup_$(date +%Y%m%d_%H%M%S).db
   ```

### Restore
1. **Stop the application** in Dokploy
2. **Access the server** and restore the database file
3. **Restart the application**

## Updates

To update your application:

1. **Push changes** to your Git repository
2. **Click "Redeploy"** in Dokploy
3. **Monitor the deployment** logs

## Security Notes

1. **Change default passwords** in environment variables
2. **Use strong session secret** (minimum 32 characters)
3. **Configure proper domain** and SSL
4. **Regular backups** are handled automatically
5. **Monitor application logs** regularly

## Support

If you encounter issues:

1. **Check Dokploy logs** for build/runtime errors
2. **Verify environment variables** are set correctly
3. **Test health endpoint**: `/health`
4. **Check database connectivity** in logs
5. **Review Docker Compose configuration**

## File Structure for Dokploy

Your repository should have these key files:
```
├── docker-compose.yml          # Main compose file (Dokploy compatible)
├── docker-compose.dokploy.yml  # Alternative compose file
├── Dockerfile                  # Application container
├── .dockerignore              # Docker build exclusions
├── .env.dokploy               # Environment template
├── app/                       # Application code
├── prisma/                    # Database schema
└── public/                    # Static assets
```

The deployment is now optimized for Dokploy and should work without volume mounting issues.