services:
  postgres:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-hll_vietnam}
      POSTGRES_USER: ${POSTGRES_USER:-hll_vietnam}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required}
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
      interval: 10s
      timeout: 5s
      retries: 12
    networks:
      - hll-internal
    restart: unless-stopped

  backend:
    build:
      context: ../../backend
    environment:
      HLL_BACKEND_DATABASE_URL: ${HLL_BACKEND_DATABASE_URL:?HLL_BACKEND_DATABASE_URL is required}
      HLL_BACKEND_HOST: ${HLL_BACKEND_HOST:-0.0.0.0}
      HLL_BACKEND_PORT: ${HLL_BACKEND_PORT:-8000}
      HLL_BACKEND_ALLOWED_ORIGINS: ${HLL_BACKEND_ALLOWED_ORIGINS:-https://comunidadhll.devzamode.es}
      HLL_BACKEND_LIVE_DATA_SOURCE: ${HLL_BACKEND_LIVE_DATA_SOURCE:-rcon}
      HLL_BACKEND_HISTORICAL_DATA_SOURCE: ${HLL_BACKEND_HISTORICAL_DATA_SOURCE:-rcon}
      HLL_BACKEND_RCON_TIMEOUT_SECONDS: ${HLL_BACKEND_RCON_TIMEOUT_SECONDS:-20}
      HLL_BACKEND_RCON_TARGETS: ${HLL_BACKEND_RCON_TARGETS:?HLL_BACKEND_RCON_TARGETS is required}
    expose:
      - "8000"
    depends_on:
      postgres:
        condition: service_healthy
    volumes:
      - backend-data:/app/data
    networks:
      - hll-internal
      - caddy
    restart: unless-stopped

  frontend:
    build:
      context: ../../frontend
    command:
      - sh
      - -c
      - |
        python - <<'PY'
        from pathlib import Path
        for path in Path('/srv/frontend').glob('*.html'):
            text = path.read_text(encoding='utf-8')
            text = text.replace('data-backend-base-url="http://127.0.0.1:8000"', 'data-backend-base-url=""')
            path.write_text(text, encoding='utf-8')
        PY
        python -m http.server 8080 --bind 0.0.0.0 --directory /srv/frontend
    expose:
      - "8080"
    depends_on:
      - backend
    networks:
      - caddy
    restart: unless-stopped

  historical-runner:
    profiles:
      - advanced
    build:
      context: ../../backend
    command: ["python", "-m", "app.historical_runner", "--hourly"]
    environment:
      HLL_BACKEND_DATABASE_URL: ${HLL_BACKEND_DATABASE_URL:?HLL_BACKEND_DATABASE_URL is required}
      HLL_BACKEND_LIVE_DATA_SOURCE: ${HLL_BACKEND_LIVE_DATA_SOURCE:-rcon}
      HLL_BACKEND_HISTORICAL_DATA_SOURCE: ${HLL_BACKEND_HISTORICAL_DATA_SOURCE:-rcon}
      HLL_BACKEND_RCON_TIMEOUT_SECONDS: ${HLL_BACKEND_RCON_TIMEOUT_SECONDS:-20}
      HLL_BACKEND_RCON_TARGETS: ${HLL_BACKEND_RCON_TARGETS:?HLL_BACKEND_RCON_TARGETS is required}
      HLL_HISTORICAL_REFRESH_INTERVAL_SECONDS: ${HLL_HISTORICAL_REFRESH_INTERVAL_SECONDS:-3600}
      HLL_HISTORICAL_REFRESH_MAX_RETRIES: ${HLL_HISTORICAL_REFRESH_MAX_RETRIES:-2}
      HLL_HISTORICAL_REFRESH_RETRY_DELAY_SECONDS: ${HLL_HISTORICAL_REFRESH_RETRY_DELAY_SECONDS:-15}
    depends_on:
      postgres:
        condition: service_healthy
      backend:
        condition: service_started
    volumes:
      - backend-data:/app/data
    networks:
      - hll-internal
    restart: unless-stopped

  rcon-historical-worker:
    profiles:
      - advanced
    build:
      context: ../../backend
    command: ["python", "-m", "app.rcon_historical_worker", "loop"]
    environment:
      HLL_BACKEND_DATABASE_URL: ${HLL_BACKEND_DATABASE_URL:?HLL_BACKEND_DATABASE_URL is required}
      HLL_BACKEND_LIVE_DATA_SOURCE: ${HLL_BACKEND_LIVE_DATA_SOURCE:-rcon}
      HLL_BACKEND_HISTORICAL_DATA_SOURCE: ${HLL_BACKEND_HISTORICAL_DATA_SOURCE:-rcon}
      HLL_BACKEND_RCON_TIMEOUT_SECONDS: ${HLL_BACKEND_RCON_TIMEOUT_SECONDS:-20}
      HLL_BACKEND_RCON_TARGETS: ${HLL_BACKEND_RCON_TARGETS:?HLL_BACKEND_RCON_TARGETS is required}
      HLL_RCON_HISTORICAL_CAPTURE_INTERVAL_SECONDS: ${HLL_RCON_HISTORICAL_CAPTURE_INTERVAL_SECONDS:-600}
      HLL_RCON_HISTORICAL_CAPTURE_MAX_RETRIES: ${HLL_RCON_HISTORICAL_CAPTURE_MAX_RETRIES:-2}
      HLL_RCON_HISTORICAL_CAPTURE_RETRY_DELAY_SECONDS: ${HLL_RCON_HISTORICAL_CAPTURE_RETRY_DELAY_SECONDS:-15}
      HLL_BACKEND_RCON_ADMIN_LOG_LOOKBACK_MINUTES: ${HLL_BACKEND_RCON_ADMIN_LOG_LOOKBACK_MINUTES:-10}
    depends_on:
      postgres:
        condition: service_healthy
      backend:
        condition: service_started
    volumes:
      - backend-data:/app/data
    networks:
      - hll-internal
    restart: unless-stopped

volumes:
  postgres-data:
  backend-data:

networks:
  hll-internal:
    driver: bridge
  caddy:
    external: true
    name: ${CADDY_NETWORK:-stack-caddy}
