# ==================================================
# OCDP Backend - Docker Compose 统一配置
# ==================================================
# 使用方式:
#
#   1. 只启动依赖服务 (PostgreSQL) - 开发模式
#      docker compose up -d
#      然后在本地运行: go run cmd/api/main.go
#
#   2. 启动完整服务 (PostgreSQL + Backend)
#      docker compose --profile backend up -d
#
#   3. 启动 Mock 模式 (无需数据库)
#      docker compose --profile mock up -d
#
#   4. 启动数据库管理工具 (可选)
#      docker compose --profile tools up -d
#
# 停止服务:
#   docker compose down
#
# 查看日志:
#   docker compose logs -f
# ==================================================

services:
  # ==================================================
  # PostgreSQL 数据库 (默认启动)
  # ==================================================
  postgres:
    image: postgres:17-alpine
    container_name: ocdp-postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-ocdp}
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
      POSTGRES_INITDB_ARGS: "--encoding=UTF8 --lc-collate=C --lc-ctype=C"
    ports:
      - "${POSTGRES_PORT:-15432}:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ${INIT_DB_SQL_PATH:-./scripts/init-db.sql}:/docker-entrypoint-initdb.d/01-init.sql:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-postgres} -d ${POSTGRES_DB:-ocdp}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    networks:
      - ocdp-network

  # ==================================================
  # Backend - 真实模式 (连接 PostgreSQL)
  # 使用: docker compose --profile backend up -d
  # ==================================================
  backend:
    build:
      context: ${BACKEND_BUILD_CONTEXT:-.}
      dockerfile: ${BACKEND_BUILD_DOCKERFILE:-Dockerfile}
      args:
        GOPROXY: ${GOPROXY:-https://goproxy.cn,direct}
        GOSUMDB: ${GOSUMDB:-sum.golang.google.cn}
    image: ocdp-backend:latest
    container_name: ocdp-backend
    restart: unless-stopped
    env_file:
      - path: ../.env
        required: false
        format: raw
    environment:
      ADAPTER_MODE: ${ADAPTER_MODE:-production}
      PORT: 8080
      JWT_SECRET: ${JWT_SECRET:-change-me-in-production}
      ENCRYPTION_KEY: ${ENCRYPTION_KEY:-change-me-32-bytes-long-key-here}
      DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@postgres:5432/${POSTGRES_DB:-ocdp}?sslmode=disable
    ports:
      - "${BACKEND_PORT:-18081}:8080"
    volumes:
      - ./config:/app/config:ro
      - ./data:/app/data
    healthcheck:
      test: ["CMD", "curl", "-f", "http://127.0.0.1:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 15s
    networks:
      - ocdp-network
    depends_on:
      postgres:
        condition: service_healthy
    profiles:
      - backend

  # ==================================================
  # Backend - Mock 模式 (无需数据库，适合快速测试)
  # 使用: docker compose --profile mock up -d
  # ==================================================
  backend-mock:
    build:
      context: ${BACKEND_BUILD_CONTEXT:-.}
      dockerfile: ${BACKEND_MOCK_BUILD_DOCKERFILE:-Dockerfile.mock}
      args:
        GOPROXY: ${GOPROXY:-https://goproxy.cn,direct}
        GOSUMDB: ${GOSUMDB:-sum.golang.google.cn}
    container_name: ocdp-backend-mock
    restart: unless-stopped
    environment:
      ADAPTER_MODE: mock
      PORT: 8080
      JWT_SECRET: ${JWT_SECRET:-test-jwt-secret-key}
      ENCRYPTION_KEY: ${ENCRYPTION_KEY:-test-encryption-key-32-bytes-long}
    ports:
      - "${BACKEND_PORT:-18081}:8080"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://127.0.0.1:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 5s
    networks:
      - ocdp-network
    profiles:
      - mock

  # ==================================================
  # pgAdmin - 数据库管理界面 (可选)
  # 使用: docker compose --profile tools up -d
  # ==================================================
  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: ocdp-pgadmin
    restart: unless-stopped
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL:-admin@ocdp.local}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD:-change-me}
      PGADMIN_CONFIG_SERVER_MODE: "False"
      PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED: "False"
    ports:
      - "${PGADMIN_PORT:-5050}:80"
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    networks:
      - ocdp-network
    depends_on:
      postgres:
        condition: service_healthy
    profiles:
      - tools

# ==================================================
# 网络配置
# ==================================================
networks:
  ocdp-network:
    driver: bridge
    name: ocdp-network

# ==================================================
# 数据卷配置
# ==================================================
volumes:
  postgres_data:
    name: ocdp-postgres-data
  pgadmin_data:
    name: ocdp-pgadmin-data
