version: '3.8'

services:
  # Redis 服务
  redis:
    image: redis:7-alpine
    container_name: webhook-ambassador-redis
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    command: redis-server --appendonly yes
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3

  # PostgreSQL 数据库 (可选，用于生产环境)
  postgres:
    image: postgres:15-alpine
    container_name: webhook-ambassador-postgres
    environment:
      POSTGRES_DB: webhook_ambassador
      POSTGRES_USER: webhook_user
      POSTGRES_PASSWORD: webhook_password
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U webhook_user -d webhook_ambassador"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Webhook Ambassador API 服务
  api:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: webhook-ambassador-api
    ports:
      - "8000:8000"
    environment:
      - REDIS_URL=redis://redis:6379/0
      - DATABASE_URL=postgresql://webhook_user:webhook_password@postgres:5432/webhook_ambassador
      - JENKINS_USERNAME=${JENKINS_USERNAME}
      - JENKINS_TOKEN=${JENKINS_TOKEN}
      - SECURITY_SECRET_KEY=${SECURITY_SECRET_KEY}
      - LOGGING_LEVEL=INFO
    volumes:
      - ./config:/app/config
      - ./logs:/app/logs
    depends_on:
      redis:
        condition: service_healthy
      postgres:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
    restart: unless-stopped

  # Celery Worker
  worker:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: webhook-ambassador-worker
    command: celery -A app.tasks.jenkins_tasks worker --loglevel=info --concurrency=4
    environment:
      - REDIS_URL=redis://redis:6379/0
      - DATABASE_URL=postgresql://webhook_user:webhook_password@postgres:5432/webhook_ambassador
      - JENKINS_USERNAME=${JENKINS_USERNAME}
      - JENKINS_TOKEN=${JENKINS_TOKEN}
      - SECURITY_SECRET_KEY=${SECURITY_SECRET_KEY}
      - LOGGING_LEVEL=INFO
    volumes:
      - ./config:/app/config
      - ./logs:/app/logs
    depends_on:
      redis:
        condition: service_healthy
      postgres:
        condition: service_healthy
    restart: unless-stopped

  # Celery Beat (定时任务调度器)
  beat:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: webhook-ambassador-beat
    command: celery -A app.tasks.jenkins_tasks beat --loglevel=info
    environment:
      - REDIS_URL=redis://redis:6379/0
      - DATABASE_URL=postgresql://webhook_user:webhook_password@postgres:5432/webhook_ambassador
      - JENKINS_USERNAME=${JENKINS_USERNAME}
      - JENKINS_TOKEN=${JENKINS_TOKEN}
      - SECURITY_SECRET_KEY=${SECURITY_SECRET_KEY}
      - LOGGING_LEVEL=INFO
    volumes:
      - ./config:/app/config
      - ./logs:/app/logs
    depends_on:
      redis:
        condition: service_healthy
      postgres:
        condition: service_healthy
    restart: unless-stopped

  # Flower (Celery 监控)
  flower:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: webhook-ambassador-flower
    command: celery -A app.tasks.jenkins_tasks flower --port=5555
    ports:
      - "5555:5555"
    environment:
      - REDIS_URL=redis://redis:6379/0
      - DATABASE_URL=postgresql://webhook_user:webhook_password@postgres:5432/webhook_ambassador
      - JENKINS_USERNAME=${JENKINS_USERNAME}
      - JENKINS_TOKEN=${JENKINS_TOKEN}
      - SECURITY_SECRET_KEY=${SECURITY_SECRET_KEY}
      - LOGGING_LEVEL=INFO
    depends_on:
      redis:
        condition: service_healthy
      postgres:
        condition: service_healthy
    restart: unless-stopped

  # Prometheus (监控)
  prometheus:
    image: prom/prometheus:latest
    container_name: webhook-ambassador-prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml
      - 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'
      - '--storage.tsdb.retention.time=200h'
      - '--web.enable-lifecycle'
    restart: unless-stopped

  # Grafana (监控面板)
  grafana:
    image: grafana/grafana:latest
    container_name: webhook-ambassador-grafana
    ports:
      - "3000:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin
    volumes:
      - grafana_data:/var/lib/grafana
      - ./monitoring/grafana/dashboards:/etc/grafana/provisioning/dashboards
      - ./monitoring/grafana/datasources:/etc/grafana/provisioning/datasources
    depends_on:
      - prometheus
    restart: unless-stopped

volumes:
  redis_data:
  postgres_data:
  prometheus_data:
  grafana_data: 