services:
  # PostgreSQL database for internal observability
  postgres:
    image: postgres:15-alpine
    environment:
      POSTGRES_USER: evenage
      POSTGRES_PASSWORD: evenage
      POSTGRES_DB: evenage
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U evenage"]
      interval: 5s
      timeout: 5s
      retries: 5

  # Redis for message bus
  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 5

  # MinIO for object storage (large artifacts/payloads)
  minio:
    image: minio/minio:latest
    ports:
      - "9000:9000"
      - "9001:9001"
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin123
    volumes:
      - minio_data:/data
    command: server /data --console-address ":9001"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 10s
      timeout: 5s
      retries: 5

  # EvenAge API server
  api:
    build:
      context: .
      dockerfile: Dockerfile
    working_dir: /app
    ports:
      - "8000:8000"
    environment:
      PYTHONPATH: "/app"
      DATABASE_URL: "postgresql://evenage:evenage@postgres:5432/evenage"
      REDIS_URL: "redis://redis:6379"
      MINIO_ENDPOINT: "minio:9000"
      MINIO_ACCESS_KEY: "minioadmin"
      MINIO_SECRET_KEY: "minioadmin123"
      MINIO_SECURE: "false"
      QUEUE_BACKEND: "redis"
      DB_BACKEND: "postgres"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      minio:
        condition: service_healthy
    command: python -m evenage.api.main

  # Dashboard (React + Vite)
  dashboard:
    image: nishantgupt786/dashboard:latest
    ports:
      - "5173:5173"
    environment:
      - VITE_API_URL=http://api:8000
      - VITE_DASH_USERS=${EVENAGE_DASH_USERS:-admin:admin}
    depends_on:
      - api

{% for agent in agents %}
  # {{ agent.name }} agent
  {{ agent.name }}:
    build:
      context: .
      dockerfile: Dockerfile
    working_dir: /app
    environment:
      PYTHONPATH: "/app"
      DATABASE_URL: "postgresql://evenage:evenage@postgres:5432/evenage"
      REDIS_URL: "redis://redis:6379"
      MINIO_ENDPOINT: "minio:9000"
      MINIO_ACCESS_KEY: "minioadmin"
      MINIO_SECRET_KEY: "minioadmin123"
      MINIO_SECURE: "false"
      QUEUE_BACKEND: "redis"
      DB_BACKEND: "postgres"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      minio:
        condition: service_healthy
    command: python -m workers.{{ agent.name }}_worker
    deploy:
      replicas: ${{ '{' }}{{ agent.name|upper }}_SCALE:-1{{ '}' }}

{% endfor %}

volumes:
  postgres_data:
  minio_data:
