version: "3.8"

services:
  # ==================== MySQL 数据库 ====================
  mysql:
    image: mysql:8.0
    container_name: plat-auto-test-mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-Ubains@13579}
      MYSQL_DATABASE: ${MYSQL_DATABASE:-plat_auto_test}
      MYSQL_USER: ${MYSQL_USER:-platapp}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD:-PlatApp2026}
      TZ: Asia/Shanghai
    volumes:
      # MySQL 数据持久化
      - /data/third_party/plat-auto-test/mysql/data:/var/lib/mysql
      # 初始化脚本（仅首次启动执行）
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    ports:
      - "3307:3306"   # 映射到宿主机 3307，避免与本地 MySQL 冲突
    networks:
      - plat-auto-test-net
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD:-Ubains@13579}"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s
    command:
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
      - --default-authentication-plugin=mysql_native_password
      - --max_connections=200

  # ==================== 应用容器（前后端合一） ====================
  app:
    build:
      context: ../backend
      dockerfile: ../deploy/Dockerfile
    container_name: plat-auto-test-app
    restart: unless-stopped
    # 强制 asyncio 事件循环（禁用 uvloop），修复 Playwright sync API 死锁。
    # 覆盖镜像 CMD，无需重建镜像即可生效。详见 Dockerfile 注释。
    command: ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80", "--loop", "asyncio"]
    depends_on:
      mysql:
        condition: service_healthy
    environment:
      - DATABASE_URL=mysql+aiomysql://${MYSQL_USER:-platapp}:${MYSQL_PASSWORD:-PlatApp2026}@mysql:3306/${MYSQL_DATABASE:-plat_auto_test}?charset=utf8mb4
      - PLAYWRIGHT_HEADLESS=true
      - PLAYWRIGHT_TIMEOUT=30000
      - LOG_LEVEL=INFO
      - TZ=Asia/Shanghai
    volumes:
      # 后端源码（更新后端只需重启容器，无需重建镜像）
      - /data/third_party/plat-auto-test/backend:/app
      # 前端构建产物（更新前端无需重启，替换文件即热生效）
      - /data/third_party/plat-auto-test/frontend/dist:/app/frontend/dist
      # 应用数据（截图、报告）
      - /data/third_party/plat-auto-test/data:/app/data
      # 日志
      - /data/third_party/plat-auto-test/logs:/app/logs
    ports:
      - "80:80"
    networks:
      - plat-auto-test-net
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:80/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

networks:
  plat-auto-test-net:
    driver: bridge