# =============================================================================
# 企微IT智能服务台 — 本地开发环境 Docker Compose
# =============================================================================
# 目标:本地电脑(Windows + Docker Desktop)
# 用途:开发 + 测试,不依赖企微 OAuth,代码 volume mount 自动 reload
# 用法:
#   1. cp .env.example .env.dev (编辑填值,或直接用 .env.dev 模板)
#   2. docker compose -f docker-compose.dev.yml up -d
#   3. 前端 4 端各跑 pnpm dev(Vite proxy /api → backend:8000)
# 启动后:
#   - Backend:  http://localhost:8000  (Swagger: /docs)
#   - Postgres: localhost:5432
#   - Redis:    localhost:6379
# =============================================================================

services:
  # --------------------------------------------------------------------------
  # PostgreSQL 16 — 开发数据库
  # --------------------------------------------------------------------------
  postgres:
    image: postgres:16-alpine
    container_name: dev_wecom_postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-wecom}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-wecom_dev}
      POSTGRES_DB: ${POSTGRES_DB:-wecom_it_desk_dev}
    ports:
      - "5432:5432"          # 暴露到宿主机,方便用 Navicat/psql 连
    volumes:
      - postgres_dev_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-wecom}"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - dev-net

  # --------------------------------------------------------------------------
  # Redis 7 — 开发缓存
  # --------------------------------------------------------------------------
  redis:
    image: redis:7-alpine
    container_name: dev_wecom_redis
    restart: unless-stopped
    command: redis-server --appendonly yes --save 900 1 --save 300 10
    ports:
      - "6379:6379"
    volumes:
      - redis_dev_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - dev-net

  # --------------------------------------------------------------------------
  # Backend — 开发模式(代码 volume mount + uvicorn --reload)
  # --------------------------------------------------------------------------
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile.dev      # dev 版(无需 apt 装 gcc,快)
    image: wecom-it-desk-backend:dev
    container_name: dev_wecom_backend
    restart: unless-stopped
    env_file:
      - .env.dev
    environment:
      # 容器内用 service name(host 是 localhost,容器内是 postgres/redis)
      - DATABASE_URL=postgresql://${POSTGRES_USER:-wecom}:${POSTGRES_PASSWORD:-wecom_dev}@postgres:5432/${POSTGRES_DB:-wecom_it_desk_dev}
      - REDIS_URL=redis://redis:6379/0
      - DEV_MODE=true                 # 开启 Mock 企微 OAuth
      - CORS_ORIGINS=http://localhost:5173,http://localhost:5174,http://localhost:5175,http://localhost:5176
    ports:
      - "8000:8000"                   # 暴露到宿主机
    volumes:
      # 关键:volume mount 源码,改代码自动 reload
      - ./backend/app:/app/app
      - ./backend/alembic:/app/alembic
      - ./backend/scripts:/app/scripts
    command: >
      sh -c "alembic upgrade head &&
             uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - dev-net

volumes:
  postgres_dev_data:
  redis_dev_data:

networks:
  dev-net:
    driver: bridge
