version: "3.8"

services:
  # 1. PostgreSQL Database
  postgres:
    image: postgres:15-alpine
    container_name: ez-postgres
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
      POSTGRES_DB: ${POSTGRES_DB:-ezapi}
    ports:
      - "5433:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - ez-network

  # 1.1 Log PostgreSQL Database
  log-postgres:
    image: postgres:15-alpine
    container_name: ez-log-postgres
    environment:
      POSTGRES_USER: ${LOG_POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${LOG_POSTGRES_PASSWORD:-postgres}
      POSTGRES_DB: ${LOG_POSTGRES_DB:-ezapi_logs}
    ports:
      - "5435:5432"
    volumes:
      - log_postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - ez-network

  # 2. Redis Cache
  redis:
    image: redis:7-alpine
    container_name: ez-redis
    command: redis-server --appendonly yes
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - ez-network

  # 3. EZ-API (Control Plane)
  ez-api:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: ez-api
    ports:
      - "${EZ_API_PORT:-8080}:8080"
    environment:
      EZ_API_PORT: ${EZ_API_PORT:-8080}
      EZ_PG_DSN: ${EZ_PG_DSN:-host=postgres user=postgres password=postgres dbname=ezapi port=5432 sslmode=disable}
      EZ_LOG_PG_DSN: ${EZ_LOG_PG_DSN:-host=log-postgres user=postgres password=postgres dbname=ezapi_logs port=5432 sslmode=disable}
      EZ_REDIS_ADDR: ${EZ_REDIS_ADDR:-redis:6379}
      EZ_REDIS_PASSWORD: ${EZ_REDIS_PASSWORD}
      EZ_REDIS_DB: ${EZ_REDIS_DB:-0}
      EZ_ADMIN_TOKEN: ${EZ_ADMIN_TOKEN:-admin123}
      EZ_INTERNAL_STATS_TOKEN: ${EZ_INTERNAL_STATS_TOKEN:-}
      EZ_CORS_ALLOW_ORIGINS: ${EZ_CORS_ALLOW_ORIGINS:-*}
      EZ_LOG_RETENTION_DAYS: ${EZ_LOG_RETENTION_DAYS:-30}
      EZ_LOG_MAX_RECORDS: ${EZ_LOG_MAX_RECORDS:-1000000}
      EZ_LOG_PARTITIONING: ${EZ_LOG_PARTITIONING:-off}
      EZ_SWAGGER_HOST: ${EZ_SWAGGER_HOST:-}
    depends_on:
      postgres:
        condition: service_healthy
      log-postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - ez-network

  # 4. Balancer (Data Plane / Gateway)
  balancer:
    build:
      context: ../balancer
      dockerfile: Dockerfile
    container_name: ez-balancer
    ports:
      - "${EZ_BALANCER_PORT:-8081}:8081"
    environment:
      EZ_BALANCER_PORT: ${EZ_BALANCER_PORT:-8081}
      EZ_REDIS_ADDR: ${EZ_REDIS_ADDR:-redis:6379}
      EZ_REDIS_PASSWORD: ${EZ_REDIS_PASSWORD}
      EZ_REDIS_DB: ${EZ_REDIS_DB:-0}
      EZ_BALANCER_LOG_SINK_ENABLED: ${EZ_BALANCER_LOG_SINK_ENABLED:-false}
      EZ_BALANCER_LOG_SINK_BASE_URL: ${EZ_BALANCER_LOG_SINK_BASE_URL:-http://ez-api:8080}
      EZ_BALANCER_LOG_SINK_TIMEOUT_SECONDS: ${EZ_BALANCER_LOG_SINK_TIMEOUT_SECONDS:-3}
      EZ_BALANCER_STATS_FLUSH_ENABLED: ${EZ_BALANCER_STATS_FLUSH_ENABLED:-false}
      EZ_BALANCER_STATS_FLUSH_BASE_URL: ${EZ_BALANCER_STATS_FLUSH_BASE_URL:-http://ez-api:8080}
      EZ_BALANCER_STATS_FLUSH_TOKEN: ${EZ_BALANCER_STATS_FLUSH_TOKEN:-}
      EZ_BALANCER_STATS_FLUSH_INTERVAL_SECONDS: ${EZ_BALANCER_STATS_FLUSH_INTERVAL_SECONDS:-300}
      EZ_BALANCER_STATS_FLUSH_BATCH_SIZE: ${EZ_BALANCER_STATS_FLUSH_BATCH_SIZE:-200}
      EZ_BALANCER_STATS_FLUSH_TIMEOUT_SECONDS: ${EZ_BALANCER_STATS_FLUSH_TIMEOUT_SECONDS:-5}
    depends_on:
      redis:
        condition: service_healthy
    networks:
      - ez-network

volumes:
  postgres_data:
  log_postgres_data:
  redis_data:

networks:
  ez-network:
    driver: bridge
