services:
  postgres:
    image: postgres:16-alpine
    container_name: pac-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: pac
      POSTGRES_PASSWORD: pac
      POSTGRES_DB: pac
    ports:
      - "5532:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U pac -d pac"]
      interval: 5s
      timeout: 5s
      retries: 10

  # 本地 DW 镜像 —— 把瑞尔 DW(dw_group.fact_*_out)全量复制到本地,
  # cold-import 连本地即可,不依赖远程 DW 可用性。仅本地开发用(含真实 PII,勿外传)。
  clickhouse:
    image: clickhouse/clickhouse-server:24.3-alpine
    container_name: pac-clickhouse
    restart: unless-stopped
    environment:
      CLICKHOUSE_USER: default
      CLICKHOUSE_PASSWORD: pac
      CLICKHOUSE_DB: dw_group
    ports:
      - "8123:8123"   # HTTP(PAC @clickhouse/client 连这个)
      - "9100:9000"   # native(避开常见 9000 占用;remote() 复制用)
    ulimits:
      nofile:
        soft: 262144
        hard: 262144
    volumes:
      - clickhouse_data:/var/lib/clickhouse
    # 本地开发关闭 healthcheck —— alpine wget 走 localhost IPv6 失败
    #   ("Connection refused" 循环) → 每 5s spawn 失败 wget 持续占 1 核 CPU(实测 110%)。
    #   生产用时改回:test: ["CMD-SHELL", "wget --spider -q http://127.0.0.1:8123/ping || exit 1"]
    healthcheck:
      disable: true

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

  pac-service:
    build:
      context: .
      dockerfile: apps/pac-service/Dockerfile
      target: dev
    container_name: pac-service
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      NODE_ENV: development
      PORT: 3101
      DATABASE_URL: postgresql://pac:pac@postgres:5432/pac?schema=public
      REDIS_URL: redis://redis:6379
      JWT_SECRET: dev-secret-change-me-in-production-min-32-chars
      JWT_REFRESH_SECRET: dev-refresh-secret-change-me-in-prod-min-32
      JWT_EXPIRES_IN: 2h
      JWT_REFRESH_EXPIRES_IN: 7d
      AI_GATEWAY_URL: https://ai-gateway.example.com
      AI_GATEWAY_API_KEY: dev-placeholder
      CORS_ORIGINS: http://localhost:3100
      EXCHANGE_CODE_TTL_SECONDS: 60
    ports:
      - "3101:3101"
    volumes:
      - ./apps/pac-service:/app/apps/pac-service
      - ./packages:/app/packages
      - /app/node_modules
      - /app/apps/pac-service/node_modules

  pac-web:
    build:
      context: .
      dockerfile: apps/pac-web/Dockerfile
      target: dev
    container_name: pac-web
    restart: unless-stopped
    depends_on:
      - pac-service
    environment:
      NODE_ENV: development
      NEXT_PUBLIC_API_BASE_URL: http://localhost:3101
      PORT: 3100
    ports:
      - "3100:3100"
    volumes:
      - ./apps/pac-web:/app/apps/pac-web
      - ./packages:/app/packages
      - /app/node_modules
      - /app/apps/pac-web/node_modules
      - /app/apps/pac-web/.next

volumes:
  postgres_data:
  redis_data:
  clickhouse_data:
