#!/bin/bash

# 遇到错误立即退出
set -e

# 配置变量
REGISTRY="harbor.3prism.ai"
PROJECT="fundamental_analysis"
VERSION="latest" # 或者使用 $(date +%Y%m%d%H%M%S) 生成时间戳版本
NAMESPACE="$REGISTRY/$PROJECT"

# 颜色输出
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

echo -e "${GREEN}=== 开始构建并推送镜像到 $NAMESPACE ===${NC}"

# 定义服务列表及其 Dockerfile 路径
# 格式: "服务名:Dockerfile路径"
SERVICES=(
    "data-persistence-service:services/data-persistence-service/Dockerfile"
    "api-gateway:services/api-gateway/Dockerfile"
    "alphavantage-provider-service:services/alphavantage-provider-service/Dockerfile"
    "tushare-provider-service:services/tushare-provider-service/Dockerfile"
    "finnhub-provider-service:services/finnhub-provider-service/Dockerfile"
    "yfinance-provider-service:services/yfinance-provider-service/Dockerfile"
    "report-generator-service:services/report-generator-service/Dockerfile"
    "frontend:frontend/Dockerfile.prod"
)

# 总大小计数器
TOTAL_SIZE=0

for entry in "${SERVICES[@]}"; do
    KEY="${entry%%:*}"
    DOCKERFILE="${entry#*:}"
    IMAGE_NAME="$NAMESPACE/$KEY:$VERSION"
    
    echo -e "\n${YELLOW}>>> 正在构建 $KEY ...${NC}"
    echo "使用 Dockerfile: $DOCKERFILE"
    
    # 构建镜像
    # 注意：构建上下文始终为项目根目录 (.)
    docker build -t "$IMAGE_NAME" -f "$DOCKERFILE" .

    # 获取镜像大小 (MB)
    SIZE_BYTES=$(docker inspect "$IMAGE_NAME" --format='{{.Size}}')
    SIZE_MB=$(echo "scale=2; $SIZE_BYTES / 1024 / 1024" | bc)
    
    echo -e "${GREEN}√ $KEY 构建完成. 大小: ${SIZE_MB} MB${NC}"
    
    # 累加大小
    TOTAL_SIZE=$(echo "$TOTAL_SIZE + $SIZE_BYTES" | bc)
    
    # 检查单个镜像大小是否异常 (例如超过 500MB 对于 Rust 微服务来说通常是不正常的，除非包含大模型)
    if (( $(echo "$SIZE_MB > 500" | bc -l) )); then
        echo -e "${RED}警告: $KEY 镜像大小超过 500MB，请检查 Dockerfile 是否包含不必要的文件！${NC}"
        # 这里我们可以选择暂停询问用户，或者只是警告
    fi

    echo -e "${YELLOW}>>> 正在推送 $KEY 到 Harbor ...${NC}"
    docker push "$IMAGE_NAME"
done

TOTAL_SIZE_MB=$(echo "scale=2; $TOTAL_SIZE / 1024 / 1024" | bc)
echo -e "\n${GREEN}=== 所有镜像处理完成 ===${NC}"
echo -e "${GREEN}总大小: ${TOTAL_SIZE_MB} MB${NC}"

# 检查总大小是否超过 1GB (1024 MB)
if (( $(echo "$TOTAL_SIZE_MB > 1024" | bc -l) )); then
    echo -e "${RED}警告: 总镜像大小超过 1GB，请注意远程仓库的空间限制！${NC}"
else
    echo -e "${GREEN}总大小在 1GB 限制范围内。${NC}"
fi

# 生成服务器使用的 docker-compose.server.yml
echo -e "\n${YELLOW}>>> 正在生成服务器部署文件 docker-compose.server.yml ...${NC}"

cat > docker-compose.server.yml <<EOF
services:
  postgres-db:
    image: timescale/timescaledb:2.15.2-pg16
    container_name: fundamental-postgres
    command: -c shared_preload_libraries=timescaledb
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: fundamental
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres -d fundamental"]
      interval: 5s
      timeout: 5s
      retries: 10
    networks:
      - app-network
      
  nats:
    image: nats:2.9
    volumes:
      - nats_data:/data
    networks:
      - app-network

  data-persistence-service:
    image: $NAMESPACE/data-persistence-service:$VERSION
    container_name: data-persistence-service
    restart: unless-stopped
    environment:
      HOST: 0.0.0.0
      PORT: 3000
      DATABASE_URL: postgresql://postgres:postgres@postgres-db:5432/fundamental
      RUST_LOG: info
      RUST_BACKTRACE: "1"
    depends_on:
      postgres-db:
        condition: service_healthy
    networks:
      - app-network

  frontend:
    image: $NAMESPACE/frontend:$VERSION
    container_name: fundamental-frontend
    restart: unless-stopped
    environment:
      NEXT_PUBLIC_BACKEND_URL: http://api-gateway:4000/v1
      FRONTEND_INTERNAL_URL: http://fundamental-frontend:3000
      BACKEND_INTERNAL_URL: http://api-gateway:4000/v1
      NODE_ENV: production
    ports:
      - "3001:3000"
    depends_on:
      api-gateway:
        condition: service_healthy
    networks:
      - app-network

  api-gateway:
    image: $NAMESPACE/api-gateway:$VERSION
    container_name: api-gateway
    restart: unless-stopped
    environment:
      SERVER_PORT: 4000
      NATS_ADDR: nats://nats:4222
      DATA_PERSISTENCE_SERVICE_URL: http://data-persistence-service:3000
      PROVIDER_SERVICES: '["http://alphavantage-provider-service:8000", "http://tushare-provider-service:8001", "http://finnhub-provider-service:8002", "http://yfinance-provider-service:8003"]'
      RUST_LOG: info,axum=info
      RUST_BACKTRACE: "1"
    depends_on:
      - nats
      - data-persistence-service
      - alphavantage-provider-service
      - tushare-provider-service
      - finnhub-provider-service
      - yfinance-provider-service
    networks:
      - app-network
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://localhost:4000/health >/dev/null || exit 1"]
      interval: 5s
      timeout: 5s
      retries: 12

  alphavantage-provider-service:
    image: $NAMESPACE/alphavantage-provider-service:$VERSION
    container_name: alphavantage-provider-service
    restart: unless-stopped
    environment:
      SERVER_PORT: 8000
      NATS_ADDR: nats://nats:4222
      DATA_PERSISTENCE_SERVICE_URL: http://data-persistence-service:3000
      RUST_LOG: info,axum=info
      RUST_BACKTRACE: "1"
    depends_on:
      - nats
      - data-persistence-service
    networks:
      - app-network
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://localhost:8000/health >/dev/null || exit 1"]
      interval: 5s
      timeout: 5s
      retries: 12

  tushare-provider-service:
    image: $NAMESPACE/tushare-provider-service:$VERSION
    container_name: tushare-provider-service
    restart: unless-stopped
    environment:
      SERVER_PORT: 8001
      NATS_ADDR: nats://nats:4222
      DATA_PERSISTENCE_SERVICE_URL: http://data-persistence-service:3000
      TUSHARE_API_URL: http://api.waditu.com
      RUST_LOG: info,axum=info
      RUST_BACKTRACE: "1"
    depends_on:
      - nats
      - data-persistence-service
    networks:
      - app-network
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://localhost:8001/health >/dev/null || exit 1"]
      interval: 5s
      timeout: 5s
      retries: 12

  finnhub-provider-service:
    image: $NAMESPACE/finnhub-provider-service:$VERSION
    container_name: finnhub-provider-service
    restart: unless-stopped
    environment:
      SERVER_PORT: 8002
      NATS_ADDR: nats://nats:4222
      DATA_PERSISTENCE_SERVICE_URL: http://data-persistence-service:3000
      FINNHUB_API_URL: https://finnhub.io/api/v1
      RUST_LOG: info,axum=info
      RUST_BACKTRACE: "1"
    depends_on:
      - nats
      - data-persistence-service
    networks:
      - app-network
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://localhost:8002/health >/dev/null || exit 1"]
      interval: 5s
      timeout: 5s
      retries: 12

  yfinance-provider-service:
    image: $NAMESPACE/yfinance-provider-service:$VERSION
    container_name: yfinance-provider-service
    restart: unless-stopped
    environment:
      SERVER_PORT: 8003
      NATS_ADDR: nats://nats:4222
      DATA_PERSISTENCE_SERVICE_URL: http://data-persistence-service:3000
      RUST_LOG: info,axum=info
      RUST_BACKTRACE: "1"
    depends_on:
      - nats
      - data-persistence-service
    networks:
      - app-network
    dns:
      - 8.8.8.8
      - 8.8.4.4
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://localhost:8003/health >/dev/null || exit 1"]
      interval: 5s
      timeout: 5s
      retries: 12

  report-generator-service:
    image: $NAMESPACE/report-generator-service:$VERSION
    container_name: report-generator-service
    restart: unless-stopped
    environment:
      SERVER_PORT: 8004
      NATS_ADDR: nats://nats:4222
      DATA_PERSISTENCE_SERVICE_URL: http://data-persistence-service:3000
      RUST_LOG: info,axum=info
      RUST_BACKTRACE: "1"
    depends_on:
      - nats
      - data-persistence-service
    networks:
      - app-network
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://localhost:8004/health >/dev/null || exit 1"]
      interval: 5s
      timeout: 5s
      retries: 12

volumes:
  pgdata:
  nats_data:

networks:
  app-network:
EOF

echo -e "${GREEN}生成完成: docker-compose.server.yml${NC}"
echo -e "请将此文件复制到远程服务器，并执行: docker-compose -f docker-compose.server.yml up -d"

