#!/bin/bash

# 参数解析
BUILD_API=false
BUILD_TASK=false
NO_CACHE=false
FORCE_RECREATE=false

while [ $# -gt 0 ]; do
  case "$1" in
    --api) BUILD_API=true ;;
    --task) BUILD_TASK=true ;;
    --all) BUILD_API=true; BUILD_TASK=true ;;
    --no-cache) NO_CACHE=true ;;
    --force) FORCE_RECREATE=true ;;
    *) echo "未知参数: $1"; exit 1 ;;
  esac
  shift
done

# 默认构建全部
if [ "$BUILD_API" = false ] && [ "$BUILD_TASK" = false ]; then
  BUILD_API=true
  BUILD_TASK=true
fi

# 构建参数
BUILD_ARGS=""
if [ "$NO_CACHE" = true ]; then
  BUILD_ARGS="$BUILD_ARGS --no-cache"
fi

echo "开始部署..."

# 确保目录存在
echo "创建日志目录并设置权限"
mkdir -p /www/wwwroot/nunu/storage/logs

# 创建按天分割的日志目录结构
current_date=$(date +"%Y-%m-%d")

echo "===== 创建日志目录 (当前日期: ${current_date}) ====="
mkdir -p /www/wwwroot/nunu/storage/logs/

# 创建日志文件并设置正确的权限
touch "/www/wwwroot/nunu/storage/logs/api-${current_date}.log"
touch "/www/wwwroot/nunu/storage/logs/task-${current_date}.log"
touch "/www/wwwroot/nunu/storage/logs/server.log"

# 设置权限
chmod -R 777 /www/wwwroot/nunu/storage

# 输出目录和文件状态以便调试
echo "日志目录内容和权限:"
ls -la /www/wwwroot/nunu/storage/logs/

# 确保配置文件包含正确的日志配置
echo "创建/更新日志配置..."
cat > "/www/wwwroot/nunu/config/log_config.yml" << EOL
log:
  log_level: info
  encoding: json
  log_format: "2006-01-02"
  api_log_file: "./storage/logs/api-%s.log"
  task_log_file: "./storage/logs/task-%s.log"
  log_file_name: "./storage/logs/server.log"
  max_backups: 30
  max_age: 30
  max_size: 200
  compress: true
EOL

echo "配置文件已创建，确保它被正确加载"

# 创建Docker卷（用于缓存Go构建）
if ! docker volume inspect go-mod-cache > /dev/null 2>&1; then
  echo "创建Go模块缓存卷..."
  docker volume create go-mod-cache
fi

# 构建并部署API服务
if [ "$BUILD_API" = true ]; then
  echo "构建API服务..."
  docker build $BUILD_ARGS -t nunu-api:v1 \
    --build-arg APP_NAME=app \
    --build-arg APP_RELATIVE_PATH=./cmd/server/main.go \
    -f deploy/build/Dockerfile .

  if [ "$FORCE_RECREATE" = true ] || ! docker ps -q -f name=nunu-api > /dev/null 2>&1; then
    echo "停止并移除旧的API容器..."
    docker stop nunu-api 2>/dev/null || true
    docker rm nunu-api 2>/dev/null || true
    
    echo "启动新的API容器..."
    docker run -d --restart=always --name nunu-api \
      --env SERVICE_TYPE=api \
      --env APP_CONF=config/prod.yml \
      -p 8000:8000 \
      -v /www/wwwroot/nunu/storage:/data/app/storage \
      -v /www/wwwroot/nunu/config:/data/app/config \
      -v go-mod-cache:/go/pkg/mod \
      nunu-api:v1 \
      ./app -conf config/prod.yml
  else
    echo "API容器已在运行中，如需重新创建请使用 --force 参数"
  fi
fi

# 构建并部署Task服务
if [ "$BUILD_TASK" = true ]; then
  echo "构建Task服务..."
  docker build $BUILD_ARGS -t nunu-task:v1 \
    --build-arg APP_NAME=task \
    --build-arg APP_RELATIVE_PATH=./cmd/task/main.go \
    -f deploy/build/Dockerfile .

  if [ "$FORCE_RECREATE" = true ] || ! docker ps -q -f name=nunu-task > /dev/null 2>&1; then
    echo "停止并移除旧的Task容器..."
    docker stop nunu-task 2>/dev/null || true
    docker rm nunu-task 2>/dev/null || true

    echo "启动新的Task容器..."
    docker run -d --restart=always --name nunu-task \
      --env APP_CONF=config/prod.yml \
      -v /www/wwwroot/nunu/storage:/data/app/storage \
      -v /www/wwwroot/nunu/config:/data/app/config \
      -v go-mod-cache:/go/pkg/mod \
      nunu-task:v1 \
      ./task -conf config/prod.yml
  else
    echo "Task容器已在运行中，如需重新创建请使用 --force 参数"
  fi
fi

echo "\n部署完成！"
[ "$BUILD_API" = true ] && echo "API服务运行在端口: 8000"
echo "日志保存在: /www/wwwroot/nunu/storage/logs/server.log"
echo "\n使用方法:"
echo "  ./deploy.sh --api     # 仅构建和部署API服务"
echo "  ./deploy.sh --task    # 仅构建和部署Task服务"
echo "  ./deploy.sh --all     # 构建和部署所有服务（默认）"
echo "  ./deploy.sh --no-cache # 不使用Docker缓存构建"
echo "  ./deploy.sh --force   # 强制重新创建容器"

