version: '3.8'

services:
  # 1. Frontend Gateway & Static File Server
  nginx:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: ems-nginx
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./configs/nginx/conf.d:/etc/nginx/conf.d
      - ./logs/nginx:/var/log/nginx
    depends_on:
      app-server:
        condition: service_started
    networks:
      - ems-net

  # 2. Backend Service (Golang)
  app-server:
    build: 
      context: ./backend
      dockerfile: Dockerfile
    container_name: ems-backend
    restart: always
    environment:
      # --- 应用配置 ---
      - GIN_MODE=release
      - TZ=Asia/Shanghai
      - PORT=8080
      
      # --- 数据库连接串 ---
      # PostgreSQL 连接 (对应下方 postgres 服务配置)
      - DB_DSN=host=postgres user=ems_user password=ems_pass dbname=ems_db port=5432 sslmode=disable TimeZone=Asia/Shanghai
      # TDengine 连接 (REST API 端口 6041)
      - TD_DSN=root:taosdata@http(tdengine:6041)/power_db
      # Redis 连接
      - REDIS_ADDR=redis:6379
      
      # --- 独立变量 (供程序读取) ---
      - POSTGRES_USER=ems_user
      - POSTGRES_PASSWORD=ems_pass
      - POSTGRES_DB=ems_db
      - POSTGRES_HOST=postgres
      - POSTGRES_PORT=5432
      
      # --- JWT 认证配置 ---
      - JWT_SECRET=your_production_secret_key_change_this
      - JWT_EXPIRE_HOURS=24
      
    volumes:
      - ./logs/backend:/app/logs
    depends_on:
      - postgres
      - tdengine
      - redis
    networks:
      - ems-net

  # 3. Database: PostgreSQL
  postgres:
    image: postgres:15-alpine
    container_name: ems-postgres
    restart: always
    environment:
      POSTGRES_USER: ems_user
      POSTGRES_PASSWORD: ems_pass
      POSTGRES_DB: ems_db
      TZ: Asia/Shanghai
    volumes:
      - pg_data:/var/lib/postgresql/data
    # 仅允许本机连接 (安全)
    ports:
      - "127.0.0.1:5432:5432"
    networks:
      - ems-net

  # 4. Database: TDengine
  tdengine:
    image: tdengine/tdengine:3.3.0.0
    container_name: ems-tdengine
    hostname: tdengine
    restart: always
    privileged: true
    environment:
      - TAOS_FIRST_EP=tdengine:6030
      - TAOS_FQDN=tdengine
      - TZ=Asia/Shanghai
    volumes:
      - ./data/taos/data:/var/lib/taos
      - ./data/taos/log:/var/log/taos
    # 仅允许本机连接
    ports:
      - "127.0.0.1:6030:6030"
      - "127.0.0.1:6041:6041"
    networks:
      - ems-net

  # 5. Cache: Redis
  redis:
    image: redis:7-alpine
    container_name: ems-redis
    restart: always
    volumes:
      - ./data/redis:/data
    environment:
      - TZ=Asia/Shanghai
    networks:
      - ems-net

  # 6. Message Broker: EMQX
  emqx:
    image: emqx/emqx:5.3.0
    container_name: ems-emqx
    restart: always
    ports: 
      - "1883:1883"   # MQTT TCP
      - "8083:8083"   # MQTT WebSocket
      - "18083:18083" # Dashboard
    volumes:
      - ./data/emqx:/opt/emqx/data
    environment:
      - EMQX_ALLOW_ANONYMOUS=false # 禁止匿名连接
      - EMQX_DASHBOARD__DEFAULT_USERNAME=admin
      - EMQX_DASHBOARD__DEFAULT_PASSWORD=public
      - TZ=Asia/Shanghai
    networks:
      - ems-net

networks:
  ems-net:
    driver: bridge

volumes:
  pg_data:
    driver: local
