# Docker Compose configuration for hybridInference staging environment
#
# Usage:
#   Start:        docker compose -f infrastructure/docker/docker-compose.staging.yml up -d
#   With pgAdmin: docker compose -f infrastructure/docker/docker-compose.staging.yml --profile admin up -d
#   Stop all:     docker compose -f infrastructure/docker/docker-compose.staging.yml down
#
# All services start by default (no profiles required for core stack).

services:
  backend:
    build:
      context: ../..
      dockerfile: infrastructure/docker/Dockerfile.backend
    container_name: hybridinference-staging-backend
    restart: unless-stopped
    env_file: ../../.env
    environment:
      DB_HOST: postgres
      DB_PORT: "5432"
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - ../../config:/app/config:ro
      - ../../var/data:/app/var/data
    ports:
      - "127.0.0.1:${BACKEND_PORT:-8000}:8080"
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - hybridinference-staging

  frontend:
    build:
      context: ../..
      dockerfile: infrastructure/docker/Dockerfile.frontend
      args:
        NEXT_PUBLIC_API_BASE: ${NEXT_PUBLIC_API_BASE:-http://localhost:8000}
        NEXT_PUBLIC_DEPLOY_TARGET: ${NEXT_PUBLIC_DEPLOY_TARGET:-staging}
    container_name: hybridinference-staging-frontend
    restart: unless-stopped
    environment:
      NODE_ENV: production
      PORT: "3001"
    ports:
      - "127.0.0.1:${FRONTEND_PORT:-3002}:3001"
    extra_hosts:
      - "host.docker.internal:host-gateway"
    depends_on:
      backend:
        condition: service_healthy
    networks:
      - hybridinference-staging

  # ===== Database =====

  postgres:
    image: postgres:16
    container_name: hybridinference-staging-postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${DB_NAME:?DB_NAME must be set in .env}
      POSTGRES_USER: ${DB_USER:?DB_USER must be set in .env}
      POSTGRES_PASSWORD: ${DB_PASSWORD:?DB_PASSWORD must be set in .env}
      POSTGRES_INITDB_ARGS: "-E UTF8 --locale=C.UTF-8"
    ports:
      - "127.0.0.1:${DB_PORT:-5433}:5432"
    volumes:
      - postgres_staging_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER} -d ${DB_NAME}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    command: >
      postgres
      -c shared_buffers=128MB
      -c max_connections=100
      -c effective_cache_size=512MB
      -c maintenance_work_mem=32MB
      -c checkpoint_completion_target=0.9
      -c wal_buffers=8MB
      -c work_mem=4MB
    networks:
      - hybridinference-staging

  # ===== Observability =====

  prometheus:
    image: prom/prometheus:v3.6.0
    container_name: hybridinference-staging-prometheus
    restart: unless-stopped
    extra_hosts:
      # Allows host.docker.internal to resolve on Linux (Docker Desktop handles this automatically on Mac/Windows)
      - "host.docker.internal:host-gateway"
    command:
      - "--config.file=/etc/prometheus/prometheus.staging.yml"
      - "--storage.tsdb.path=/prometheus"
      - "--storage.tsdb.retention.time=7d"
      - "--web.enable-lifecycle"
      - "--web.console.libraries=/usr/share/prometheus/console_libraries"
      - "--web.console.templates=/usr/share/prometheus/consoles"
    ports:
      - "127.0.0.1:${PROMETHEUS_PORT:-9091}:9090"
    volumes:
      - ../prometheus/prometheus.staging.yml:/etc/prometheus/prometheus.staging.yml:ro
      - ../prometheus/rules:/etc/prometheus/rules:ro
      - prometheus_staging_data:/prometheus
    networks:
      - hybridinference-staging

  grafana:
    image: grafana/grafana:12.1.1
    container_name: hybridinference-staging-grafana
    restart: unless-stopped
    environment:
      GF_SECURITY_ADMIN_USER: ${GRAFANA_USER:-admin}
      GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_PASSWORD:-admin}
      DB_NAME: ${DB_NAME}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
    ports:
      - "127.0.0.1:${GRAFANA_PORT:-3001}:3000"
    volumes:
      - grafana_staging_data:/var/lib/grafana
      - ../grafana/provisioning:/etc/grafana/provisioning:ro
    depends_on:
      - prometheus
    networks:
      - hybridinference-staging

  # pgAdmin — optional web UI for inspecting the staging DB
  # Start with: --profile admin
  pgadmin:
    image: dpage/pgadmin4:8
    container_name: hybridinference-staging-pgadmin
    restart: unless-stopped
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL:-admin@staging.local}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD:-admin}
      PGADMIN_CONFIG_SERVER_MODE: "${PGADMIN_CONFIG_SERVER_MODE:-False}"
      PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED: "False"
      SCRIPT_NAME: /pgadmin
    ports:
      - "127.0.0.1:${PGADMIN_PORT:-5051}:80"
    volumes:
      - pgadmin_staging_data:/var/lib/pgadmin
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - hybridinference-staging
    profiles:
      - admin

volumes:
  postgres_staging_data:
    driver: local
  prometheus_staging_data:
    driver: local
  grafana_staging_data:
    driver: local
  pgadmin_staging_data:
    driver: local

networks:
  hybridinference-staging:
    driver: bridge
