version: '3.8'

services:
  # 🚀 Claude Relay Service
  claude-relay:
    build: .
    container_name: claude-relay-service
    restart: unless-stopped
    ports:
      - "${PORT:-3000}:3000"
    environment:
      - NODE_ENV=production
      - PORT=3000
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - ADMIN_USERNAME=${ADMIN_USERNAME:-}  # 可选：预设管理员用户名
      - ADMIN_PASSWORD=${ADMIN_PASSWORD:-}  # 可选：预设管理员密码
    volumes:
      - ./logs:/app/logs
      - ./data:/app/data
    depends_on:
      - redis
    networks:
      - claude-relay-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # 📊 Redis Database
  redis:
    image: redis:7-alpine
    container_name: claude-relay-redis
    restart: unless-stopped
    ports:
      - "${REDIS_PORT:-6379}:6379"
    volumes:
      - ./redis_data:/data
    command: redis-server --save 60 1 --appendonly yes --appendfsync everysec
    networks:
      - claude-relay-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3

  # 📈 Redis Monitoring (Optional)
  redis-commander:
    image: rediscommander/redis-commander:latest
    container_name: claude-relay-redis-web
    restart: unless-stopped
    ports:
      - "${REDIS_WEB_PORT:-8081}:8081"
    environment:
      - REDIS_HOSTS=local:redis:6379
    depends_on:
      - redis
    networks:
      - claude-relay-network
    profiles:
      - monitoring

  # 📊 Application Monitoring (Optional)
  prometheus:
    image: prom/prometheus:latest
    container_name: claude-relay-prometheus
    restart: unless-stopped
    ports:
      - "${PROMETHEUS_PORT:-9090}:9090"
    volumes:
      - ./config/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--web.enable-lifecycle'
    networks:
      - claude-relay-network
    profiles:
      - monitoring

  # 📈 Grafana Dashboard (Optional)
  grafana:
    image: grafana/grafana:latest
    container_name: claude-relay-grafana
    restart: unless-stopped
    ports:
      - "${GRAFANA_PORT:-3001}:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_ADMIN_PASSWORD:-admin123}
    volumes:
      - grafana_data:/var/lib/grafana
      - ./config/grafana:/etc/grafana/provisioning
    depends_on:
      - prometheus
    networks:
      - claude-relay-network
    profiles:
      - monitoring

volumes:
  prometheus_data:
    driver: local
  grafana_data:
    driver: local

networks:
  claude-relay-network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16