version: '3.8'

services:
  # 1. Frontend Gateway & Static File Server
  nginx:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: ems-wsl-nginx
    ports: ["80:80", "443:443"]
    volumes:
      - ./configs/nginx/conf.d:/etc/nginx/conf.d
    depends_on:
      app-server:
        condition: service_started

  # 2. Backend Service (Golang)
  app-server:
    build: 
      context: ./backend
      dockerfile: Dockerfile
    container_name: ems-wsl-backend
    restart: always
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8080/ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    environment:
      # --- 硬编码配置 (无需 .env) ---
      - GIN_MODE=debug # 开发环境使用 debug 模式
      - PORT=8080
      
      # 数据库连接
      - DB_DSN=host=postgres user=ems_user password=ems_pass dbname=ems_db port=5432 sslmode=disable TimeZone=Asia/Shanghai
      - TD_DSN=root:taosdata@http(tdengine:6041)/power_db
      - REDIS_ADDR=redis:6379
      
      # 独立变量
      - POSTGRES_USER=ems_user
      - POSTGRES_PASSWORD=ems_pass
      - POSTGRES_DB=ems_db
      - POSTGRES_HOST=postgres
      - POSTGRES_PORT=5432
      
      # 安全配置
      - JWT_SECRET=change_me_dev_secret
      - JWT_EXPIRE_HOURS=24
      - TZ=Asia/Shanghai
    volumes:
      - ./backend/logs:/app/logs
    ports:
      - "8080:8080"
    depends_on:
      - postgres
      - tdengine
      - redis

  # 3. Database: PostgreSQL
  postgres:
    image: postgres:15-alpine
    container_name: ems-wsl-postgres
    restart: always
    environment:
      POSTGRES_USER: ems_user
      POSTGRES_PASSWORD: ems_pass
      POSTGRES_DB: ems_db
      TZ: Asia/Shanghai
    volumes:
      - pg_data_wsl:/var/lib/postgresql/data
    ports:
      - "5433:5432" # 开发环境映射到 5433，避免与本地 PG 冲突

  # 4. Database: TDengine
  tdengine:
    image: tdengine/tdengine:3.3.0.0
    container_name: ems-wsl-tdengine
    hostname: tdengine
    privileged: true
    stop_grace_period: 2m
    ports: ["6030:6030", "6041:6041"]
    volumes:
      - ./data/taos/data:/var/lib/taos
      - ./data/taos/log:/var/log/taos
    environment:
      - TAOS_FIRST_EP=tdengine:6030
      - TAOS_FQDN=tdengine
      - TZ=Asia/Shanghai

  # 5. Cache: Redis
  redis:
    image: redis:7-alpine
    container_name: ems-wsl-redis
    ports: ["6379:6379"]
    volumes:
      - ./data/redis:/data
    environment:
      - TZ=Asia/Shanghai

  # 6. Message Broker: EMQX
  emqx:
    image: emqx/emqx:5.3.0
    container_name: ems-wsl-emqx
    ports: 
      - "1883:1883"  # MQTT TCP
      - "8083:8083"  # MQTT WebSocket
      - "18083:18083" # Dashboard
    environment:
      - EMQX_ALLOW_ANONYMOUS=true # 开发环境允许匿名
      - EMQX_DASHBOARD__DEFAULT_USERNAME=admin
      - EMQX_DASHBOARD__DEFAULT_PASSWORD=public
      - TZ=Asia/Shanghai

volumes:
  pg_data_wsl:
    driver: local
  taos_data:
    driver: local
