version: "3.9"

services:
  # ── API Server ──────────────────────────────────────────
  api:
    build: .
    container_name: ai_engineering_api
    command: python main.py server
    ports:
      - "8000:8000"
    environment:
      - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
      - OPENAI_API_KEY=${OPENAI_API_KEY}
      - GOOGLE_API_KEY=${GOOGLE_API_KEY}
      - DATABASE_URL=postgresql+asyncpg://postgres:${POSTGRES_PASSWORD:-password}@db:5432/ai_engineering
      - REDIS_URL=redis://redis:6379/0
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/1
      - DEFAULT_EXECUTOR=${DEFAULT_EXECUTOR:-claude}
    volumes:
      - ./sandboxes:/app/sandboxes
      - ./reports:/app/reports
      - ./memory:/app/memory
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    restart: unless-stopped

  # ── Celery Worker ───────────────────────────────────────
  worker:
    build: .
    container_name: ai_engineering_worker
    command: celery -A app.queue.celery_app worker --loglevel=info -Q engineering,bugfix,review -c 3
    environment:
      - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
      - OPENAI_API_KEY=${OPENAI_API_KEY}
      - GOOGLE_API_KEY=${GOOGLE_API_KEY}
      - DATABASE_URL=postgresql+asyncpg://postgres:${POSTGRES_PASSWORD:-password}@db:5432/ai_engineering
      - REDIS_URL=redis://redis:6379/0
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/1
    volumes:
      - ./sandboxes:/app/sandboxes
      - ./reports:/app/reports
      - ./memory:/app/memory
      - /var/run/docker.sock:/var/run/docker.sock  # For OpenHands executor
    depends_on:
      - redis
      - db
    restart: unless-stopped

  # ── Celery Flower (Monitor) ─────────────────────────────
  flower:
    build: .
    container_name: ai_engineering_flower
    command: celery -A app.queue.celery_app flower --port=5555
    ports:
      - "5555:5555"
    environment:
      - CELERY_BROKER_URL=redis://redis:6379/0
    depends_on:
      - redis
    restart: unless-stopped

  # ── PostgreSQL ──────────────────────────────────────────
  db:
    image: postgres:16-alpine
    container_name: ai_engineering_db
    environment:
      POSTGRES_DB: ai_engineering
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-password}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped

  # ── Redis ───────────────────────────────────────────────
  redis:
    image: redis:7-alpine
    container_name: ai_engineering_redis
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped

volumes:
  postgres_data:
