services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: phosphat-report-app
    restart: unless-stopped
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
      - PORT=3000
      - 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_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
      - ENCRYPTION_KEY=phosphat-report-default-key-32b
    volumes:
      - app_data:/app/data
      - app_logs:/app/logs
    networks:
      - app_network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3000/health", "||", "exit", "1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    deploy:
      resources:
        limits:
          memory: 512M
          cpus: '0.5'
        reservations:
          memory: 256M
          cpus: '0.25'

  backup:
    image: alpine:latest
    container_name: phosphat-report-backup
    restart: unless-stopped
    volumes:
      - app_data:/data:ro
      - backup_data:/backup
    command:
      - sh
      - -c
      - |
        apk add --no-cache dcron sqlite &&
        echo '0 2 * * * cp /data/production.db /backup/production_$$(date +%Y%m%d_%H%M%S).db && find /backup -name "production_*.db" -mtime +7 -delete' | crontab - &&
        crond -f
    networks:
      - app_network
    depends_on:
      - app

volumes:
  app_data:
    driver: local
  app_logs:
    driver: local
  backup_data:
    driver: local

networks:
  app_network:
    driver: bridge