version: '3.8'

services:
  # Main Ralph Orchestrator service
  ralph:
    build:
      context: .
      dockerfile: Dockerfile
    image: ralph-orchestrator:local
    container_name: ralph-orchestrator
    environment:
      # AI Agent API Keys (set in .env file)
      - CLAUDE_API_KEY=${CLAUDE_API_KEY}
      - GEMINI_API_KEY=${GEMINI_API_KEY}
      - Q_API_KEY=${Q_API_KEY}
      
      # Ralph Configuration
      - RALPH_AGENT=${RALPH_AGENT:-auto}
      - RALPH_MAX_ITERATIONS=${RALPH_MAX_ITERATIONS:-100}
      - RALPH_MAX_RUNTIME=${RALPH_MAX_RUNTIME:-14400}
      - RALPH_MAX_TOKENS=${RALPH_MAX_TOKENS:-1000000}
      - RALPH_MAX_COST=${RALPH_MAX_COST:-50.0}
      - RALPH_CHECKPOINT_INTERVAL=${RALPH_CHECKPOINT_INTERVAL:-5}
      - RALPH_VERBOSE=${RALPH_VERBOSE:-false}
      - RALPH_ENABLE_METRICS=${RALPH_ENABLE_METRICS:-true}
      
      # Logging
      - LOG_LEVEL=${LOG_LEVEL:-INFO}
    volumes:
      # Mount workspace for task execution
      - ./workspace:/workspace
      # Mount prompts directory (read-only)
      - ./prompts:/prompts:ro
      # Mount agent state directory
      - ralph-agent:/app/.agent
      # Mount cache directory
      - ralph-cache:/app/.cache
      # Mount git directory if needed
      - ./.git:/workspace/.git
    networks:
      - ralph-network
    restart: unless-stopped
    command: 
      - "--agent=${RALPH_AGENT:-auto}"
      - "--prompt=/workspace/PROMPT.md"
      - "--verbose"
    depends_on:
      - redis
    healthcheck:
      test: ["CMD", "python", "-c", "import sys; sys.exit(0)"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Redis for caching and state management
  redis:
    image: redis:7-alpine
    container_name: ralph-redis
    command: redis-server --appendonly yes
    volumes:
      - redis-data:/data
    networks:
      - ralph-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # PostgreSQL for persistent storage (optional)
  postgres:
    image: postgres:15-alpine
    container_name: ralph-postgres
    environment:
      - POSTGRES_DB=ralph_orchestrator
      - POSTGRES_USER=ralph
      - POSTGRES_PASSWORD=${DB_PASSWORD:-ralph_secret}
    volumes:
      - postgres-data:/var/lib/postgresql/data
      - ./scripts/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    networks:
      - ralph-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ralph"]
      interval: 10s
      timeout: 5s
      retries: 5
    profiles:
      - with-db

  # Prometheus for metrics collection
  prometheus:
    image: prom/prometheus:latest
    container_name: ralph-prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'
      - '--web.console.templates=/usr/share/prometheus/consoles'
    volumes:
      - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - prometheus-data:/prometheus
    networks:
      - ralph-network
    ports:
      - "9090:9090"
    restart: unless-stopped
    profiles:
      - monitoring

  # Grafana for visualization
  grafana:
    image: grafana/grafana:latest
    container_name: ralph-grafana
    environment:
      - GF_SECURITY_ADMIN_USER=${GRAFANA_USER:-admin}
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD:-admin123}
      - GF_INSTALL_PLUGINS=redis-datasource
    volumes:
      - grafana-data:/var/lib/grafana
      - ./monitoring/grafana/dashboards:/etc/grafana/provisioning/dashboards:ro
      - ./monitoring/grafana/datasources:/etc/grafana/provisioning/datasources:ro
    networks:
      - ralph-network
    ports:
      - "3000:3000"
    restart: unless-stopped
    depends_on:
      - prometheus
    profiles:
      - monitoring

  # Documentation server (development)
  docs:
    image: squidfunk/mkdocs-material:latest
    container_name: ralph-docs
    volumes:
      - .:/docs
    networks:
      - ralph-network
    ports:
      - "8000:8000"
    command: serve --dev-addr=0.0.0.0:8000
    profiles:
      - development

# Volumes for persistent data
volumes:
  ralph-agent:
    driver: local
  ralph-cache:
    driver: local
  redis-data:
    driver: local
  postgres-data:
    driver: local
  prometheus-data:
    driver: local
  grafana-data:
    driver: local

# Networks
networks:
  ralph-network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.28.0.0/16