version: '3.8'

services:
  # PostgreSQL Database
  postgres:
    image: postgres:16-alpine
    container_name: vip-coordinator-postgres
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-vip_coordinator}
      POSTGRES_USER: ${POSTGRES_USER:-vip_user}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD must be set}
    volumes:
      - vip-coordinator-postgres-data:/var/lib/postgresql/data
    networks:
      - vip-coordinator-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-vip_user}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    restart: unless-stopped
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # Redis Cache
  redis:
    image: redis:7-alpine
    container_name: vip-coordinator-redis
    volumes:
      - vip-coordinator-redis-data:/data
    networks:
      - vip-coordinator-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
      start_period: 5s
    restart: unless-stopped
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
    command: redis-server --appendonly yes

  # NestJS Backend API
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    image: vip-coordinator-backend:latest
    container_name: vip-coordinator-backend
    environment:
      # Database Configuration
      DATABASE_URL: postgresql://${POSTGRES_USER:-vip_user}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-vip_coordinator}

      # Redis Configuration
      REDIS_URL: redis://redis:6379

      # Auth0 Configuration
      AUTH0_DOMAIN: ${AUTH0_DOMAIN:?AUTH0_DOMAIN must be set}
      AUTH0_AUDIENCE: ${AUTH0_AUDIENCE:?AUTH0_AUDIENCE must be set}
      AUTH0_ISSUER: ${AUTH0_ISSUER:?AUTH0_ISSUER must be set}

      # Application Configuration
      NODE_ENV: production
      PORT: 3000

      # Optional: AviationStack API (for flight tracking)
      AVIATIONSTACK_API_KEY: ${AVIATIONSTACK_API_KEY:-}

      # Optional: Database seeding
      RUN_SEED: ${RUN_SEED:-false}
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - vip-coordinator-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3000/api/v1/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    restart: unless-stopped
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # React Frontend (Nginx)
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        # These are embedded at build time
        VITE_API_URL: ${VITE_API_URL:-http://localhost/api/v1}
        VITE_AUTH0_DOMAIN: ${AUTH0_DOMAIN:?AUTH0_DOMAIN must be set}
        VITE_AUTH0_CLIENT_ID: ${AUTH0_CLIENT_ID:?AUTH0_CLIENT_ID must be set}
        VITE_AUTH0_AUDIENCE: ${AUTH0_AUDIENCE:?AUTH0_AUDIENCE must be set}
    image: vip-coordinator-frontend:latest
    container_name: vip-coordinator-frontend
    ports:
      - "${FRONTEND_PORT:-80}:80"
    depends_on:
      backend:
        condition: service_healthy
    networks:
      - vip-coordinator-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost/health"]
      interval: 30s
      timeout: 3s
      retries: 3
      start_period: 5s
    restart: unless-stopped
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

# Named volumes for data persistence
volumes:
  vip-coordinator-postgres-data:
    name: vip-coordinator-postgres-data
  vip-coordinator-redis-data:
    name: vip-coordinator-redis-data

# Dedicated network for service communication
networks:
  vip-coordinator-network:
    name: vip-coordinator-network
    driver: bridge
