version: '3.8'

# Production docker-compose configuration
# This uses an external database and optimized settings

services:
  # FastAPI Backend
  api:
    build: ./backend
    restart: always
    environment:
      DB_HOST: ${DB_HOST}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
      DB_NAME: ${DB_NAME}
      DB_PORT: ${DB_PORT}
      SECRET_KEY: ${SECRET_KEY}
      ACCESS_TOKEN_EXPIRE_MINUTES: ${ACCESS_TOKEN_EXPIRE_MINUTES}
      API_V1_STR: ${API_V1_STR}
      PROJECT_NAME: ${PROJECT_NAME}
      MAIL_HOST: ${MAIL_HOST}
      MAIL_PORT: ${MAIL_PORT}
      MAIL_USERNAME: ${MAIL_USERNAME}
      MAIL_PASSWORD: ${MAIL_PASSWORD}
      MAIL_FROM: ${MAIL_FROM}
      MAIL_FROM_NAME: ${MAIL_FROM_NAME}
      BASE_URL: ${BASE_URL}
      REDIS_URL: ${REDIS_URL}
      TAG: ${TAG}
      TOP_BAR_BASE_COLOR: ${TOP_BAR_BASE_COLOR}
      ALLOW_PUBLIC_BOOKING: ${ALLOW_PUBLIC_BOOKING}
      ENVIRONMENT: production
      WORKERS: "4"
    ports:
      - "${API_PORT_EXTERNAL}:8000"
    volumes:
      - ./backend:/app
      - ./db-init:/db-init:ro  # Mount CSV data for seeding
    networks:
      - app_network
    extra_hosts:
      - "host.docker.internal:host-gateway"
    deploy:
      resources:
        limits:
          cpus: '2'
          memory: 2G
        reservations:
          cpus: '1'
          memory: 1G
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Redis for caching (optional)
  redis:
    image: redis:7-alpine
    restart: always
    networks:
      - app_network
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 512M

  # Nginx web server for public frontend
  web:
    image: nginx:alpine
    restart: always
    environment:
      BASE_URL: ${BASE_URL}
    command: >
      sh -c "echo 'window.PPR_CONFIG = { apiBase: \"'\$BASE_URL'/api/v1\" };' > /usr/share/nginx/html/config.js &&
             nginx -g 'daemon off;'"
    ports:
      - "${WEB_PORT_EXTERNAL}:80"
    volumes:
      - ./web:/usr/share/nginx/html
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - api
    networks:
      - app_network
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 256M

networks:
  app_network:
    driver: bridge
