version: '3.8'

# 开发环境完整部署配置
services:
  
  # ================== 基础服务 ==================
  
  # PostgreSQL 数据库
  postgres:
    image: postgres:16-alpine
    container_name: ul-postgres
    environment:
      POSTGRES_DB: urban_lifeline
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres123
      TZ: Asia/Shanghai
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./urbanLifelineServ/.bin/database/postgres/sql:/docker-entrypoint-initdb.d
    networks:
      - ul-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Redis
  redis:
    image: redis:7-alpine
    container_name: ul-redis
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    networks:
      - ul-network
    command: redis-server --appendonly yes

  # Nacos 注册中心
  nacos:
    image: nacos/nacos-server:v2.3.0
    container_name: ul-nacos
    environment:
      MODE: standalone
      SPRING_DATASOURCE_PLATFORM: mysql
      PREFER_HOST_MODE: hostname
      JVM_XMS: 512m
      JVM_XMX: 512m
      JVM_XMN: 256m
    ports:
      - "8848:8848"
      - "9848:9848"
    volumes:
      - nacos_data:/home/nacos/data
    networks:
      - ul-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8848/nacos"]
      interval: 10s
      timeout: 5s
      retries: 10

  # ================== 后端服务 ==================

  # Gateway 网关服务
  gateway:
    build:
      context: ./urbanLifelineServ/gateway
      dockerfile: Dockerfile.dev
    container_name: ul-gateway
    environment:
      SPRING_PROFILES_ACTIVE: dev
      NACOS_SERVER_ADDR: nacos:8848
      POSTGRES_HOST: postgres
      POSTGRES_PORT: 5432
      REDIS_HOST: redis
      REDIS_PORT: 6379
    ports:
      - "8080:8080"
    volumes:
      - ./urbanLifelineServ/gateway:/app
      - maven_cache:/root/.m2
    networks:
      - ul-network
    depends_on:
      postgres:
        condition: service_healthy
      nacos:
        condition: service_healthy
    command: mvn spring-boot:run

  # 认证服务
  auth-service:
    build:
      context: ./urbanLifelineServ/auth
      dockerfile: Dockerfile.dev
    container_name: ul-auth
    environment:
      SPRING_PROFILES_ACTIVE: dev
      NACOS_SERVER_ADDR: nacos:8848
      POSTGRES_HOST: postgres
      REDIS_HOST: redis
    volumes:
      - ./urbanLifelineServ/auth:/app
      - maven_cache:/root/.m2
    networks:
      - ul-network
    depends_on:
      - postgres
      - nacos

  # 系统服务
  system-service:
    build:
      context: ./urbanLifelineServ/system
      dockerfile: Dockerfile.dev
    container_name: ul-system
    environment:
      SPRING_PROFILES_ACTIVE: dev
      NACOS_SERVER_ADDR: nacos:8848
      POSTGRES_HOST: postgres
    volumes:
      - ./urbanLifelineServ/system:/app
      - maven_cache:/root/.m2
    networks:
      - ul-network
    depends_on:
      - postgres
      - nacos

  # ================== 前端服务 ==================

  # 共享包服务（Module Federation Remote）
  shared:
    build:
      context: ./urbanLifelineWeb
      dockerfile: packages/shared/Dockerfile.dev
    container_name: ul-shared
    ports:
      - "5000:5000"
    volumes:
      - ./urbanLifelineWeb/packages/shared:/app
      - /app/node_modules
      - pnpm_store:/root/.local/share/pnpm/store
    networks:
      - ul-network
    environment:
      - VITE_PORT=5000
      - CHOKIDAR_USEPOLLING=true  # 支持 Docker 内文件监听
    command: pnpm dev

  # 主应用（Portal）
  portal:
    build:
      context: ./urbanLifelineWeb
      dockerfile: packages/portal/Dockerfile.dev
    container_name: ul-portal
    ports:
      - "3000:3000"
    volumes:
      - ./urbanLifelineWeb/packages/portal:/app
      - ./urbanLifelineWeb/packages/shared:/shared
      - /app/node_modules
      - pnpm_store:/root/.local/share/pnpm/store
    networks:
      - ul-network
    environment:
      - VITE_PORT=3000
      - VITE_API_BASE_URL=http://nginx/api
      - VITE_SHARED_REMOTE=http://nginx/shared
      - CHOKIDAR_USEPOLLING=true
    depends_on:
      - shared
    command: pnpm dev

  # 招投标应用
  app-bidding:
    build:
      context: ./urbanLifelineWeb
      dockerfile: packages/app-bidding/Dockerfile.dev
    container_name: ul-app-bidding
    ports:
      - "3001:3001"
    volumes:
      - ./urbanLifelineWeb/packages/app-bidding:/app
      - ./urbanLifelineWeb/packages/shared:/shared
      - /app/node_modules
      - pnpm_store:/root/.local/share/pnpm/store
    networks:
      - ul-network
    environment:
      - VITE_PORT=3001
      - VITE_API_BASE_URL=http://nginx/api
      - VITE_SHARED_REMOTE=http://nginx/shared
      - CHOKIDAR_USEPOLLING=true
    depends_on:
      - shared
    command: pnpm dev

  # 智能客服应用
  app-customer-service:
    build:
      context: ./urbanLifelineWeb
      dockerfile: packages/app-customer-service/Dockerfile.dev
    container_name: ul-app-cs
    ports:
      - "3002:3002"
    volumes:
      - ./urbanLifelineWeb/packages/app-customer-service:/app
      - ./urbanLifelineWeb/packages/shared:/shared
      - /app/node_modules
      - pnpm_store:/root/.local/share/pnpm/store
    networks:
      - ul-network
    environment:
      - VITE_PORT=3002
      - VITE_API_BASE_URL=http://nginx/api
      - VITE_SHARED_REMOTE=http://nginx/shared
      - CHOKIDAR_USEPOLLING=true
    depends_on:
      - shared
    command: pnpm dev

  # ================== 统一网关 ==================

  # Nginx 统一入口
  nginx:
    image: nginx:alpine
    container_name: ul-nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./docker/nginx/nginx.dev.conf:/etc/nginx/nginx.conf:ro
      - ./docker/nginx/conf.d:/etc/nginx/conf.d:ro
      - ./docker/nginx/ssl:/etc/nginx/ssl:ro
    networks:
      - ul-network
    depends_on:
      - gateway
      - portal
      - app-bidding
      - app-customer-service
      - shared
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost/health"]
      interval: 10s
      timeout: 5s
      retries: 3

networks:
  ul-network:
    driver: bridge

volumes:
  postgres_data:
  redis_data:
  nacos_data:
  maven_cache:
  pnpm_store:
