version: '3.8'

services:
  # PostgreSQL Database
  db:
    image: postgres:15-alpine
    container_name: scraper-db
    environment:
      POSTGRES_DB: scraper
      POSTGRES_USER: scraper
      POSTGRES_PASSWORD: ${DB_PASSWORD:-scraper123}
    ports:
      - "5437:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./db/init:/docker-entrypoint-initdb.d:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U scraper"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - scraper-network

  # API Server
  api:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: scraper-api
    environment:
      - DATABASE_URL=postgresql://scraper:${DB_PASSWORD:-scraper123}@db:5432/scraper
      - API_BASE_URL=${API_BASE_URL:-http://localhost:8001}
      - PORT=8001
      - MAX_CONCURRENT_JOBS=${MAX_CONCURRENT_JOBS:-5}
      - CANARY_TEST_URL=${CANARY_TEST_URL:-https://www.google.com/maps/place/Soho+Factory/@54.6738155,25.2595844,17z/}
      - SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL:-}
      # Chromium/Xvfb configuration
      - DISPLAY=:99
      - CHROME_BIN=/usr/bin/chromium
      # Pipeline packages path
      - PYTHONPATH=/app:/app/packages/pipeline-core/src:/app/packages/reviewiq-pipeline/src
      # ReviewIQ pipeline database URL (same DB, pipeline schema)
      - REVIEWIQ_DATABASE_URL=postgresql://scraper:${DB_PASSWORD:-scraper123}@db:5432/scraper
      # ReviewIQ LLM API keys
      - REVIEWIQ_OPENAI_API_KEY=${OPENAI_API_KEY:-}
      - REVIEWIQ_ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY:-}
    volumes:
      - ./packages:/app/packages:ro
      - ./api:/app/api:ro
    ports:
      - "8001:8001"
      - "5900:5900"   # VNC port (for VNC client)
      - "6080:6080"   # noVNC web interface (browser access)
    depends_on:
      db:
        condition: service_healthy
    # Chrome requires shared memory for stability
    shm_size: 2gb
    # Chrome capabilities (needed for sandboxing)
    cap_add:
      - SYS_ADMIN
    # Security options for Chrome
    security_opt:
      - seccomp:unconfined
    networks:
      - scraper-network
    restart: unless-stopped

volumes:
  postgres_data:

networks:
  scraper-network:
    driver: bridge
