version: '3.8'

services:
  redis:
    image: redis:7-alpine
    container_name: agent_bounty_redis
    restart: always
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 10
    networks:
      - agent-bounty-network

  db:
    image: postgres:14-alpine
    container_name: agent_bounty_db
    restart: always
    environment:
      POSTGRES_USER: agent
      POSTGRES_PASSWORD: agent_password_secure
      POSTGRES_DB: agent_bounty
    ports:
      - "5433:5432"
    volumes:
      - agent_bounty_pgdata:/var/lib/postgresql/data
    networks:
      - agent-bounty-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U agent"]
      interval: 5s
      timeout: 5s
      retries: 5

  web:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: agent_bounty_web
    restart: always
    ports:
      - "3000:3000"
    environment:
      # Use the docker internal network to connect to postgres
      - DATABASE_URL=postgresql://agent:agent_password_secure@db:5432/agent_bounty?schema=public
      - NODE_ENV=production
      - REDIS_URL=redis://redis:6379
      - API_KEY=${API_KEY:-super-secret-mcp-key}
      - E2B_API_KEY=${E2B_API_KEY:-""}
      - AUTO_WHITELIST_EXTERNAL_AGENTS=${AUTO_WHITELIST_EXTERNAL_AGENTS:-true}
      - ALLOW_MCP_CLAIM_WITHOUT_STRIPE=${ALLOW_MCP_CLAIM_WITHOUT_STRIPE:-false}
      - TELEGRAM_BOT_TOKEN=${TELEGRAM_BOT_TOKEN:-}
      - TELEGRAM_CHAT_ID=${TELEGRAM_CHAT_ID:-}
      - TRAFFIC_MONITOR_TOKEN=${TRAFFIC_MONITOR_TOKEN:-}
      - VIBEWORK_TRAFFIC_WEBHOOK_URL=${VIBEWORK_TRAFFIC_WEBHOOK_URL:-}
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    # We use a command override to run database push before starting next.js
    command: >
      sh -c "npx prisma@6.4.1 db push --schema=apps/web/prisma/schema.prisma --skip-generate && node apps/web/server.js"
    healthcheck:
      test: ["CMD-SHELL", "node -e \"const http=require('http');const req=http.get('http://127.0.0.1:3000/api/v1/health',(res)=>process.exit(res.statusCode===200?0:1));req.on('error',()=>process.exit(1));\""]
      interval: 10s
      timeout: 5s
      retries: 30
      start_period: 10s
    stop_grace_period: 30s
    networks:
      - agent-bounty-network

  scout-bot:
    build:
      context: .
      dockerfile: apps/scout-bot/Dockerfile
    container_name: agent_bounty_scout_bot
    restart: unless-stopped
    depends_on:
      web:
        condition: service_healthy
    environment:
      - NODE_ENV=production
      - VIBEWORK_API_URL=http://agent_bounty_web:3000/api
      - SCOUT_API_KEY=${SCOUT_API_KEY:-${API_KEY:-super-secret-mcp-key}}
      - SCOUT_AGENT_ID=scout_official_1
      # Optional: add more discovery repos via env (comma separated "owner/repo"), e.g. openai/swarm,significant-gravitas/autogpt
      - SCOUT_TARGET_REPOS=${SCOUT_TARGET_REPOS:-open-webui/open-webui,microsoft/vscode,vercel/next.js,langchain-ai/langgraph,facebook/react,microsoft/TypeScript,openai/openai-cookbook,astral-sh/ruff,sequelize/sequelize,pnpm/pnpm,prisma/prisma,openai/swarm,Significant-Gravitas/AutoGPT,microsoft/autogen,crewAIInc/crewAI}
      # Optional: comma-separated labels; leave unset for broad scan of all open issues
      # Use 'help wanted' to only target issues where maintainers are explicitly asking for external help. This prevents us from being seen as spam.
      - SCOUT_ISSUE_LABELS=${SCOUT_ISSUE_LABELS:-help wanted}
      # Keep compatibility with existing naming
      - SCOUT_ISSUE_LABEL=${SCOUT_ISSUE_LABEL:-}
      # Run once an hour to avoid flooding
      - SCOUT_CRON_EXPRESSION=${SCOUT_CRON_EXPRESSION:-0 * * * *}
      - SCOUT_ENABLED=${SCOUT_ENABLED:-true}
      # Allow commenting on all repos, since we are now strictly filtering by label and rate.
      - SCOUT_COMMENT_REPOS=${SCOUT_COMMENT_REPOS:-*}
      - SCOUT_PER_PAGE=${SCOUT_PER_PAGE:-30}
      # Only process at most 2 issues per repo per hour to stay under the radar
      - SCOUT_MAX_ISSUES_PER_SCAN=${SCOUT_MAX_ISSUES_PER_SCAN:-2}
      # GitHub token should be provided in deployment env for real posting.
      - GITHUB_TOKEN=${GITHUB_TOKEN:-}
    networks:
      - agent-bounty-network

networks:
  agent-bounty-network:
    driver: bridge
    name: agent-bounty-network

volumes:
  agent_bounty_pgdata:
