# =============================================
# Thpark IoT Platform - Docker Compose
# =============================================
# Usage:
#   docker compose up -d          # Start all services
#   docker compose down           # Stop all services
#   docker compose down -v        # Stop and remove volumes (⚠️ data loss)
#   docker compose logs -f backend # Tail backend logs
#
# First run will auto-migrate DB and seed default admin.
# Default admin: admin@thpark.local / admin123456
# =============================================

services:
  # ---- PostgreSQL Database ----
  db:
    image: postgres:16-alpine
    container_name: thpark-db
    restart: unless-stopped
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: thpark_db_password
      POSTGRES_DB: thpark_db
      TZ: Asia/Shanghai
      PGTZ: Asia/Shanghai
    volumes:
      - thpark-postgres-data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres -d thpark_db"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    networks:
      - thpark-network

  # ---- Redis Cache ----
  redis:
    image: redis:7-alpine
    container_name: thpark-redis
    restart: unless-stopped
    command: redis-server --appendonly yes
    volumes:
      - thpark-redis-data:/data
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - thpark-network

  # ---- MinIO (S3 兼容对象存储, 用于合同/企业附件) ----
  # 控制台: http://localhost:9001  账号 minioadmin / minioadmin
  # S3 API:  http://localhost:9000
  minio:
    image: minio/minio:latest
    container_name: thpark-minio
    restart: unless-stopped
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin
    volumes:
      - thpark-minio-data:/data
    ports:
      - "9000:9000"
      - "9001:9001"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 15s
      timeout: 5s
      retries: 5
    networks:
      - thpark-network

  # 一次性容器：启动后自动创建 thpark bucket
  minio-init:
    image: minio/mc:latest
    container_name: thpark-minio-init
    depends_on:
      minio:
        condition: service_healthy
    entrypoint: >
      /bin/sh -c "
      mc alias set local http://minio:9000 minioadmin minioadmin &&
      mc mb --ignore-existing local/thpark &&
      echo '✅ MinIO bucket thpark ready'
      "
    networks:
      - thpark-network

  # ---- Go Backend ----
  backend:
    build:
      context: ./thpark_server
      dockerfile: Dockerfile
    container_name: thpark-backend
    restart: unless-stopped
    environment:
      APP_ENV: production
      # Database (overrides config file)
      THPARK_DATABASE_HOST: db
      THPARK_DATABASE_PORT: "5432"
      THPARK_DATABASE_PASSWORD: thpark_db_password
      # JWT (⚠️ CHANGE THIS IN PRODUCTION)
      THPARK_SERVER_JWT_SECRET: thpark-jwt-secret-change-me-in-production
      # CORS
      THPARK_SERVER_ALLOW_ORIGINS: "http://localhost,http://localhost:80"
      # Super admin password
      THPARK_SUPER_ADMIN_PASSWORD: admin123456
      # Redis (use service name as host)
      THPARK_REDIS_ADDRESS: redis:6379
      # 对象存储 (MinIO)
      THPARK_STORAGE_ENDPOINT: minio:9000
      THPARK_STORAGE_ACCESS_KEY: minioadmin
      THPARK_STORAGE_SECRET_KEY: minioadmin
      THPARK_STORAGE_BUCKET: thpark
      # 浏览器从宿主机访问需要换成对外可达的 endpoint
      THPARK_STORAGE_PUBLIC_ENDPOINT: localhost:9000
    ports:
      - "8090:8090"
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
      minio:
        condition: service_healthy
    networks:
      - thpark-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8090/health"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 15s

  # ---- React Admin Frontend (Nginx) ----
  frontend:
    build:
      context: ./thpark_admin
      dockerfile: Dockerfile
      args:
        VITE_API_BASE_URL: /api
    container_name: thpark-frontend
    restart: unless-stopped
    ports:
      - "80:80"
    depends_on:
      backend:
        condition: service_healthy
    networks:
      - thpark-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:80/"]
      interval: 30s
      timeout: 5s
      retries: 3

# ---- Volumes ----
volumes:
  thpark-postgres-data:
    driver: local
  thpark-redis-data:
    driver: local
  thpark-minio-data:
    driver: local

# ---- Networks ----
networks:
  thpark-network:
    driver: bridge
