services:
  postgres:
    image: docker.m.daocloud.io/library/postgres:16-bookworm
    restart: unless-stopped
    environment:
      TZ: ${TZ:-Asia/Shanghai}
      POSTGRES_DB: ${POSTGRES_DB:-order_site}
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
    ports:
      - "${POSTGRES_PORT:-5432}:5432"
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "pg_isready -U ${POSTGRES_USER:-postgres} -d ${POSTGRES_DB:-order_site}",
        ]
      interval: 5s
      timeout: 5s
      retries: 20
    volumes:
      - postgres_dev_data:/var/lib/postgresql/data

  ocr-worker:
    image: ${OCR_IMAGE:-order_site-ocr-worker:latest}
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://127.0.0.1:8100/health || exit 1"]
      interval: 5s
      timeout: 5s
      retries: 20
      start_period: 15s
    environment:
      TZ: ${TZ:-Asia/Shanghai}
    ports:
      - "${OCR_PORT:-8100}:8100"

  backend:
    build:
      context: .
      dockerfile: deploy/docker/backend-dev.Dockerfile
    restart: unless-stopped
    env_file:
      - .env
    command: >-
      sh /app/scripts/start-dev-container.sh
    depends_on:
      postgres:
        condition: service_healthy
      ocr-worker:
        condition: service_healthy
    healthcheck:
      test:
        ["CMD-SHELL", "curl -fsS http://127.0.0.1:3000/health/ready || exit 1"]
      interval: 5s
      timeout: 5s
      retries: 20
      start_period: 20s
    environment:
      TZ: ${TZ:-Asia/Shanghai}
      PORT: ${BACKEND_PORT:-3000}
      DATABASE_URL: ${DATABASE_URL:-postgres://postgres:postgres@postgres:5432/order_site}
      DATABASE_SSL: ${DATABASE_SSL:-false}
      DATABASE_MAX_CONNECTIONS: ${DATABASE_MAX_CONNECTIONS:-10}
      DATA_ROOT: /app/data
      OCR_BASE_URL: ${OCR_BASE_URL:-http://ocr-worker:8100}
      CLAIM_BASE_URL: ${CLAIM_BASE_URL:-http://localhost/#/claim}
      TENCENT_REDEEM_PROOF_MODE: ${TENCENT_REDEEM_PROOF_MODE:-full}
      TENCENT_BROWSER_HEADLESS: ${TENCENT_BROWSER_HEADLESS:-true}
      TENCENT_BROWSER_DEVTOOLS: ${TENCENT_BROWSER_DEVTOOLS:-false}
      TENCENT_BROWSER_PREWARM: ${TENCENT_BROWSER_PREWARM:-true}
      TENCENT_BROWSER_KEEP_ALIVE: ${TENCENT_BROWSER_KEEP_ALIVE:-true}
      TENCENT_BROWSER_SLOW_MO: ${TENCENT_BROWSER_SLOW_MO:-0}
      TENCENT_SESSION_DEBUG: ${TENCENT_SESSION_DEBUG:-false}
      TENCENT_BROWSER_NOVNC_ENABLED: ${TENCENT_BROWSER_NOVNC_ENABLED:-true}
      TENCENT_BROWSER_NOVNC_PORT: ${TENCENT_BROWSER_NOVNC_PORT:-6080}
      TENCENT_BROWSER_VNC_PORT: ${TENCENT_BROWSER_VNC_PORT:-5900}
      TENCENT_BROWSER_VNC_RESOLUTION: ${TENCENT_BROWSER_VNC_RESOLUTION:-1600x900x24}
      ADMIN_SESSION_SECRET: ${ADMIN_SESSION_SECRET}
      ADMIN_DEFAULT_USERS_JSON: ${ADMIN_DEFAULT_USERS_JSON}
      AGISO_APP_SECRET: ${AGISO_APP_SECRET:-}
      AGISO_MESSAGING_ENABLED: ${AGISO_MESSAGING_ENABLED:-false}
    volumes:
      - ./apps/backend:/app
      - backend_node_modules:/app/node_modules
      - ./apps/backend/data:/app/data
    ports:
      - "${TENCENT_BROWSER_NOVNC_PORT:-6080}:6080"

  frontend:
    image: docker.m.daocloud.io/library/node:22-bookworm-slim
    working_dir: /app
    restart: unless-stopped
    command: >-
      sh -lc
      "[ -f node_modules/.install_done ] || (npm install --no-fund --no-audit && touch node_modules/.install_done);
      npm run dev -- --host 0.0.0.0 --port 5173"
    environment:
      TZ: ${TZ:-Asia/Shanghai}
      CHOKIDAR_USEPOLLING: "true"
      NPM_CONFIG_REGISTRY: https://registry.npmmirror.com
      VITE_API_TARGET: http://backend:3000
    volumes:
      - ./apps/frontend:/app
      - frontend_node_modules:/app/node_modules

  web:
    image: docker.m.daocloud.io/library/caddy:2-alpine
    restart: unless-stopped
    depends_on:
      backend:
        condition: service_healthy
      frontend:
        condition: service_started
    ports:
      - "80:80"
      - "443:443"
    environment:
      TZ: ${TZ:-Asia/Shanghai}
    volumes:
      - ./deploy/caddy/Caddyfile.dev:/etc/caddy/Caddyfile:ro

volumes:
  postgres_dev_data:
  backend_node_modules:
  frontend_node_modules:
