version: "3.8"

services:
  mongoclaw:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:8000"
    environment:
      - MONGOCLAW_ENVIRONMENT=development
      - MONGOCLAW_MONGODB_URI=mongodb://mongodb:27017/mongoclaw
      - MONGOCLAW_REDIS_URL=redis://redis:6379/0
      - MONGOCLAW_AI_DEFAULT_PROVIDER=openai
      - MONGOCLAW_AI_DEFAULT_MODEL=gpt-4o-mini
      # Add your API keys here or use .env file
      # - MONGOCLAW_AI_OPENAI_API_KEY=your-key
    volumes:
      - ./configs:/app/configs:ro
    depends_on:
      mongodb:
        condition: service_healthy
      redis:
        condition: service_healthy
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s

  mongodb:
    image: mongo:7.0
    ports:
      - "27017:27017"
    volumes:
      - mongodb_data:/data/db
      - ./deploy/mongodb/init-replica.js:/docker-entrypoint-initdb.d/init-replica.js:ro
    command: ["--replSet", "rs0", "--bind_ip_all"]
    healthcheck:
      test: |
        mongosh --eval "try { rs.status().ok } catch(e) { rs.initiate({_id:'rs0',members:[{_id:0,host:'localhost:27017'}]}).ok }" --quiet
      interval: 10s
      timeout: 10s
      retries: 5
      start_period: 30s
    restart: unless-stopped

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    command: ["redis-server", "--appendonly", "yes"]
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped

  # Optional: Redis Commander for debugging
  redis-commander:
    image: rediscommander/redis-commander:latest
    ports:
      - "8081:8081"
    environment:
      - REDIS_HOSTS=local:redis:6379
    depends_on:
      - redis
    profiles:
      - debug

  # Optional: Mongo Express for debugging
  mongo-express:
    image: mongo-express:latest
    ports:
      - "8082:8081"
    environment:
      - ME_CONFIG_MONGODB_URL=mongodb://mongodb:27017/
      - ME_CONFIG_BASICAUTH=false
    depends_on:
      - mongodb
    profiles:
      - debug

  # Optional: Prometheus for metrics
  prometheus:
    image: prom/prometheus:latest
    ports:
      - "9090:9090"
    volumes:
      - ./deploy/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - prometheus_data:/prometheus
    command:
      - "--config.file=/etc/prometheus/prometheus.yml"
      - "--storage.tsdb.path=/prometheus"
    profiles:
      - monitoring
    restart: unless-stopped

  # Optional: Grafana for dashboards
  grafana:
    image: grafana/grafana:latest
    ports:
      - "3000:3000"
    volumes:
      - grafana_data:/var/lib/grafana
      - ./deploy/grafana/provisioning:/etc/grafana/provisioning:ro
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin
      - GF_USERS_ALLOW_SIGN_UP=false
    depends_on:
      - prometheus
    profiles:
      - monitoring
    restart: unless-stopped

volumes:
  mongodb_data:
  redis_data:
  prometheus_data:
  grafana_data:

networks:
  default:
    name: mongoclaw-network
