services:
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: chajia
    ports:
      # 宿主机 3306 常被本机 MySQL 占用，默认映射到 3307
      - "${MYSQL_HOST_PORT:-3307}:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 5s
      timeout: 5s
      retries: 10

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 10

  next-app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    environment:
      DATABASE_URL: mysql://root:${MYSQL_ROOT_PASSWORD}@mysql:3306/chajia
      REDIS_URL: redis://redis:6379
      JWT_SECRET: ${JWT_SECRET}
      HOST_SERVICE_TOKENS: ${HOST_SERVICE_TOKENS}
      NODE_ENV: production
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy

  rpa-worker:
    build:
      context: .
      dockerfile: Dockerfile.worker
    environment:
      DATABASE_URL: mysql://root:${MYSQL_ROOT_PASSWORD}@mysql:3306/chajia
      REDIS_URL: redis://redis:6379
      MOTHERSHIP_EMAIL: ${MOTHERSHIP_EMAIL}
      MOTHERSHIP_PASSWORD: ${MOTHERSHIP_PASSWORD}
      MOTHERSHIP_QUOTE_URLS: ${MOTHERSHIP_QUOTE_URLS}
      RPA_STORAGE_STATE_PATH: ${RPA_STORAGE_STATE_PATH:-.rpa/mothership-storage.json}
      RPA_MOCK_MODE: ${RPA_MOCK_MODE:-false}
      RPA_HEADLESS: ${RPA_HEADLESS:-true}
      RPA_USE_PATCHRIGHT: ${RPA_USE_PATCHRIGHT:-false}
      RPA_WORKER_ID: ${RPA_WORKER_ID:-}
      RPA_SELECTOR_PICKUP_STREET: ${RPA_SELECTOR_PICKUP_STREET:-}
      RPA_SELECTOR_DELIVERY_STREET: ${RPA_SELECTOR_DELIVERY_STREET:-}
      RPA_SELECTOR_ADDRESS_SUGGESTION: ${RPA_SELECTOR_ADDRESS_SUGGESTION:-}
      RPA_SELECTOR_SUBMIT: ${RPA_SELECTOR_SUBMIT:-}
      RPA_SELECTOR_STANDARD_TAB: ${RPA_SELECTOR_STANDARD_TAB:-}
      RPA_SELECTOR_GUARANTEED_TAB: ${RPA_SELECTOR_GUARANTEED_TAB:-}
      RPA_SELECTOR_LOWEST_OPTION: ${RPA_SELECTOR_LOWEST_OPTION:-}
      RPA_SELECTOR_FASTEST_OPTION: ${RPA_SELECTOR_FASTEST_OPTION:-}
      RPA_SELECTOR_PRICE: ${RPA_SELECTOR_PRICE:-}
    volumes:
      - rpa_state:/app/.rpa
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    shm_size: "1gb"

volumes:
  mysql_data:
  redis_data:
  rpa_state:
