version: "3.8"

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        APT_CACHE_URL: ${APT_CACHE_URL:-}
    environment:
      - ENVIRONMENT=production
      - DEBUG=false
      - LOG_LEVEL=WARNING
      # Database configuration - must be provided externally
      - DATABASE_HOST=${DATABASE_HOST}
      - DATABASE_PORT=${DATABASE_PORT:-5432}
      - DATABASE_USER=${DATABASE_USER}
      - DATABASE_PASSWORD=${DATABASE_PASSWORD}
      - DATABASE_NAME=${DATABASE_NAME}
      - DATABASE_DRIVER=postgresql
      # Production-specific settings
      - CALMINER_EXPORT_MAX_ROWS=100000
      - CALMINER_IMPORT_MAX_ROWS=100000
      - CALMINER_EXPORT_METADATA=true
      - CALMINER_IMPORT_STAGING_TTL=3600
    ports:
      - "8003:8003"
    depends_on:
      postgres:
        condition: service_healthy
    restart: unless-stopped
    # Production health checks
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8003/health"]
      interval: 60s
      timeout: 30s
      retries: 5
      start_period: 60s
    # Resource limits for production
    deploy:
      resources:
        limits:
          cpus: "1.0"
          memory: 1G
        reservations:
          cpus: "0.5"
          memory: 512M

  postgres:
    environment:
      - POSTGRES_USER=${DATABASE_USER}
      - POSTGRES_PASSWORD=${DATABASE_PASSWORD}
      - POSTGRES_DB=${DATABASE_NAME}
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    restart: unless-stopped
    # Production postgres health check
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DATABASE_USER} -d ${DATABASE_NAME}"]
      interval: 60s
      timeout: 30s
      retries: 5
      start_period: 60s
    # Resource limits for postgres
    deploy:
      resources:
        limits:
          cpus: "1.0"
          memory: 2G
        reservations:
          cpus: "0.5"
          memory: 1G

volumes:
  postgres_data:
