version: '3.8'

services:
  # Redis for session storage
  redis:
    image: redis:7-alpine
    container_name: playwright-proxy-redis
    ports:
      - "6379:6379"
    volumes:
      - redis-data:/data
    command: redis-server --appendonly yes
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3

  # Playwright Proxy Application
  proxy:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: playwright-proxy-app
    ports:
      - "3000:3000"  # Backend API
      - "3001:3001"  # Frontend UI
      - "9090:9090"  # Metrics
    environment:
      - NODE_ENV=production
      - PORT=3000
      - HOST=0.0.0.0
      - MAX_CONCURRENT_SESSIONS=200
      - BROWSER_TIMEOUT_MS=300000
      - IDLE_TIMEOUT_MS=600000
      - SESSION_SECRET=${SESSION_SECRET:-change-me-in-production}
      - JWT_SECRET=${JWT_SECRET:-change-me-in-production}
      - REDIS_URL=redis://redis:6379
      - LOG_LEVEL=info
      - METRICS_ENABLED=true
      - METRICS_PORT=9090
    depends_on:
      redis:
        condition: service_healthy
    volumes:
      - ./logs:/app/logs
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: '4'
          memory: 4G
        reservations:
          cpus: '2'
          memory: 2G

  # Prometheus for metrics collection (optional)
  prometheus:
    image: prom/prometheus:latest
    container_name: playwright-proxy-prometheus
    ports:
      - "9091:9090"
    volumes:
      - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus-data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
    restart: unless-stopped
    profiles:
      - monitoring

  # Grafana for metrics visualization (optional)
  grafana:
    image: grafana/grafana:latest
    container_name: playwright-proxy-grafana
    ports:
      - "3002:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD:-admin}
      - GF_USERS_ALLOW_SIGN_UP=false
    volumes:
      - grafana-data:/var/lib/grafana
      - ./monitoring/grafana-dashboards:/etc/grafana/provisioning/dashboards
    restart: unless-stopped
    depends_on:
      - prometheus
    profiles:
      - monitoring

volumes:
  redis-data:
  prometheus-data:
  grafana-data:
