services:
  backend:
    build:
      context: .
      dockerfile: Dockerfile.backend.prod
    container_name: ${PROJECT_NAME}_backend_prod
    restart: unless-stopped
    environment:
      - DATABASE_URL=postgresql+asyncpg://${DB_USER}:${DB_PASSWORD}@db:5432/${PROJECT_NAME}
      {% if use_redis %}- REDIS_URL=redis://redis:6379/0
      {% endif %}- ENVIRONMENT=production
      - SECRET_KEY=${SECRET_KEY}
      - ALLOWED_HOSTS=${ALLOWED_HOSTS}
    depends_on:
      db:
        condition: service_healthy
      {% if use_redis %}redis:
        condition: service_healthy
      {% endif %}
    networks:
      - backend
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    deploy:
      replicas: {{ backend_replicas }}
      resources:
        limits:
          cpus: '1.0'
          memory: 512M
        reservations:
          cpus: '0.5'
          memory: 256M
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  frontend:
    build:
      context: .
      dockerfile: Dockerfile.frontend.prod
    container_name: ${PROJECT_NAME}_frontend_prod
    restart: unless-stopped
    depends_on:
      - backend
    networks:
      - frontend
      - backend
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 128M
        reservations:
          cpus: '0.25'
          memory: 64M
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  db:
    image: postgres:16-alpine
    container_name: ${PROJECT_NAME}_db_prod
    restart: unless-stopped
    environment:
      - POSTGRES_DB=${PROJECT_NAME}
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./backups:/backups
    networks:
      - backend
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER}"]
      interval: 10s
      timeout: 5s
      retries: 5
    deploy:
      resources:
        limits:
          cpus: '1.0'
          memory: 1G
        reservations:
          cpus: '0.5'
          memory: 512M
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  {% if use_redis %}redis:
    image: redis:7-alpine
    container_name: ${PROJECT_NAME}_redis_prod
    restart: unless-stopped
    command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
    volumes:
      - redis_data:/data
    networks:
      - backend
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 256M
        reservations:
          cpus: '0.25'
          memory: 128M
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
  {% endif %}
  nginx:
    image: nginx:alpine
    container_name: ${PROJECT_NAME}_nginx_prod
    restart: unless-stopped
    ports:
      - "${HTTP_PORT:-80}:80"
      {% if ssl_enabled %}- "${HTTPS_PORT:-443}:443"
      {% endif %}
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      {% if ssl_enabled %}- ./nginx/ssl:/etc/nginx/ssl:ro
      {% endif %}- ./nginx/static:/usr/share/nginx/static:ro
    depends_on:
      - frontend
      - backend
    networks:
      - frontend
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 128M
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

volumes:
  postgres_data:
    driver: local
  {% if use_redis %}redis_data:
    driver: local
  {% endif %}
networks:
  frontend:
    driver: bridge
  backend:
    driver: bridge
