# HotPlex Worker Gateway - Docker Compose
#
# Usage:
#   Development:
#     docker-compose up -d
#
#   Production:
#     docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
#

version: '3.8'

services:
  gateway:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        GIT_SHA: ${GIT_SHA:-unknown}
        BUILD_TIME: ${BUILD_TIME:-unknown}
    image: hotplex:latest
    container_name: hotplex
    restart: unless-stopped
    
    # Use init to handle zombie processes (critical for spawning workers)
    init: true

    ports:
      - "${HOTPLEX_PORT:-8888}:8888"   # WebSocket gateway
      - "${HOTPLEX_ADMIN_PORT:-9999}:9999"   # Admin API

    environment:
      # Required secrets
      - HOTPLEX_JWT_SECRET=${HOTPLEX_JWT_SECRET:?HOTPLEX_JWT_SECRET is required}
      - HOTPLEX_ADMIN_TOKEN_1=${HOTPLEX_ADMIN_TOKEN:-admin-token-dev}
      - HOTPLEX_ADMIN_TOKEN_2=${HOTPLEX_ADMIN_TOKEN_2:-}

      # Optional configuration
      - HOTPLEX_LOG_LEVEL=${HOTPLEX_LOG_LEVEL:-info}
      - OTEL_EXPORTER_OTLP_ENDPOINT=${OTEL_EXPORTER_OTLP_ENDPOINT:-}
      - TZ=${TZ:-Asia/Shanghai}
      - GOMEMLIMIT=6500MiB
      
      # Claude Code environment
      - CLAUDE_API_KEY=${CLAUDE_API_KEY:-}

    volumes:
      # Configuration
      - ./configs:/etc/hotplex:ro

      # Persistent data (SQLite database & sessions)
      - hotplex-data:/var/lib/hotplex/data
      - hotplex-worker-sessions:/home/hotplex/.claude

      # Logs
      - hotplex-logs:/var/log/hotplex

      # TLS certificates (optional)
      - ./tls:/etc/hotplex/tls:ro

    healthcheck:
      test: ["CMD", "curl", "-sf", "http://localhost:9999/admin/health/ready"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 15s

    networks:
      - hotplex-network

    # Security options
    security_opt:
      - no-new-privileges:true
    cap_drop:
      - ALL
    cap_add:
      - NET_BIND_SERVICE

    # Resource limits
    deploy:
      resources:
        limits:
          cpus: '4'
          memory: 8G
        reservations:
          cpus: '1'
          memory: 2G

  # Database backup sidecar
  backup:
    image: alpine:3.21
    container_name: hotplex-backup
    restart: unless-stopped
    depends_on:
      - gateway
    volumes:
      - hotplex-data:/data:ro
      - backup-storage:/backups
    environment:
      - BACKUP_INTERVAL=3600  # 1 hour
      - RETENTION_DAYS=30
    command: >
      sh -c "
        apk add --no-cache sqlite &&
        mkdir -p /backups &&
        while true; do
          TIMESTAMP=$$(date +%Y%m%d-%H%M%S) &&
          sqlite3 /data/hotplex.db \".backup '/backups/hotplex-$$TIMESTAMP.db'\" &&
          find /backups -name '*.db' -mtime +$$RETENTION_DAYS -delete &&
          echo \"Backup created: hotplex-$$TIMESTAMP.db at $$(date)\" &&
          sleep $$BACKUP_INTERVAL
        done
      "
    networks:
      - hotplex-network

  # Prometheus for metrics
  prometheus:
    image: prom/prometheus:latest
    container_name: hotplex-prometheus
    restart: unless-stopped
    profiles:
      - monitoring
    ports:
      - "9090:9090"
    volumes:
      - ./configs/monitoring/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - prometheus-data:/prometheus
    networks:
      - hotplex-network

  # Grafana for visualization
  grafana:
    image: grafana/grafana:latest
    container_name: hotplex-grafana
    restart: unless-stopped
    profiles:
      - monitoring
    ports:
      - "3000:3000"
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD:-admin}
    volumes:
      - grafana-data:/var/lib/grafana
    depends_on:
      - prometheus
    networks:
      - hotplex-network

volumes:
  hotplex-data:
  hotplex-worker-sessions:
  hotplex-logs:
  backup-storage:
  prometheus-data:
  grafana-data:

networks:
  hotplex-network:
    driver: bridge
