#!/bin/bash

# 设置全局变量
PROJECT_NAME="hd-crane-gs"
WORK_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_PATH="${WORK_DIR}"

# 日志级别控制
LOG_LEVEL=${LOG_LEVEL:-INFO}

# 打印带时间戳的日志
log() {
    local message="$1"
    local level=${2:-INFO}
    
    # 根据日志级别决定是否输出
    case $LOG_LEVEL in
        DEBUG) ;;
        INFO) [[ $level == "DEBUG" ]] && return ;;
        WARN) [[ $level == "DEBUG" || $level == "INFO" ]] && return ;;
        ERROR) [[ $level != "ERROR" ]] && return ;;
    esac
    
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] [$level] $message"
}

# 检查系统依赖
check_permissions() {
    log "==========🔍 检查权限...==========" "INFO"
    # 检查脚本文件是否有执行权限
    if [ ! -x "$0" ]; then
        log "⚠️ 当前脚本没有执行权限，请使用 chmod +x $0 添加执行权限" "ERROR"
        exit 1
    else
        log "✅ 脚本已有执行权限" "INFO"
    fi

    log "==========🔍 检查系统依赖...==========" "INFO"
    local missing_deps=()
    
    for cmd in docker node npm pnpm; do
        if ! command -v $cmd &> /dev/null; then
            missing_deps+=($cmd)
            log "❌ $cmd 未安装" "ERROR"
        else
            log "🎉 $cmd 已安装 - $($cmd --version 2>/dev/null || $cmd -v)" "INFO"
        fi
    done
    
    if [ ${#missing_deps[@]} -ne 0 ]; then
        log "❌ 缺少依赖: ${missing_deps[*]}" "ERROR"
        exit 1
    fi
    
    log "✅ 所有依赖检查通过" "INFO"
}

# 停止项目容器
stop_project() {
    log "==========⏹️ 停止项目容器...==========" "INFO"
    log "📂 当前工作目录: $(pwd)" "INFO"
    if [ -f "docker-compose.yaml" ] || [ -f "docker-compose.yml" ]; then
            docker compose down || { log "❌ 停止容器失败" "ERROR"; exit 1; }
            log "✅ 项目容器已停止并删除" "INFO"
        else
            log "❌ docker-compose 文件未找到" "ERROR"
            exit 1
        fi
}


# 构建镜像&启动容器
start_containers() {
    log "==========🚀 构建镜像&启动容器...==========" "INFO"
    cd "${PROJECT_PATH}" || { log "❌ 无法进入项目目录：${PROJECT_PATH}" "ERROR"; exit 1; }
    docker compose build || { log "❌ 镜像构建失败" "ERROR"; exit 1; }
    log "✅  Docker镜像构建成功" "INFO"
    docker compose up -d --force-recreate || { log "❌ 容器启动失败" "ERROR"; exit 1; }
    sleep 5
    log "✅ 容器启动成功" "INFO"

    log "==========🗑️ 清理72小时前的旧镜像...==========" "INFO"
    # 只清理与项目相关的镜像
    local project_images=$(docker images | grep ${PROJECT_NAME} | awk '{print $3}' | wc -l)
    if [ $project_images -gt 0 ]; then
        local before_count=$(docker images | grep ${PROJECT_NAME} | wc -l)
        # 修改: 使用更准确的过滤方式清理镜像
        docker image prune -f --filter "until=72h" --filter "label=com.docker.compose.project=${PROJECT_NAME}" >/dev/null 2>&1
        local after_count=$(docker images | grep ${PROJECT_NAME} | wc -l)
        log "✅ 旧镜像清理完成，清理了 $((before_count - after_count)) 个镜像" "INFO"
    else
        log "⚠️  没有找到项目相关镜像，跳过清理" "WARN"
    fi
}

# 显示所有完整日志的函数
show_containers_logs() {
    log "==========📋 查看所有容器日志... ==========" "INFO"
    cd "${PROJECT_PATH}" || { log "❌ 无法进入项目目录：${PROJECT_PATH}" "ERROR"; exit 1; }
    
    # 显示容器状态
    log "📊 当前容器状态：" "INFO"
    docker compose ps --format "table {{.Service}}\t{{.Name}}\t{{.Status}}\t{{.Ports}}"
    
    log "📋 Nginx服务日志：" "INFO"
    echo "----------------------------------------"
    docker compose logs nginx
    echo "----------------------------------------"
    
    log "📋 MySQL服务日志：" "INFO"
    echo "----------------------------------------"
    docker compose logs mysql
    echo "----------------------------------------"
    
    log "📋 Redis服务日志：" "INFO"
    echo "----------------------------------------"
    docker compose logs redis
    echo "----------------------------------------"
    
    log "💡 实时日志查看命令：" "INFO"
    log "  - 查看所有服务实时日志：docker compose logs -f" "INFO"
    log "  - 查看单个服务实时日志：docker compose logs -f [服务名]" "INFO"
    log "  - 服务名：nginx, mysql, redis" "INFO"
}

# 信号处理
handle_interrupt() {
    log "==========⚠️ 收到中断信号，正在停止部署...==========" "WARN"
    # 如果在容器启动阶段中断，尝试停止容器
    if [ -d "${PROJECT_PATH}" ]; then
        cd "${PROJECT_PATH}"
        docker compose down >/dev/null 2>&1
    fi
    exit 130
}

# 主函数
main() {
    log "==========🚀 开始部署流程==========" "INFO"
    log "📂 脚本所在目录: ${WORK_DIR}" "INFO"
    log "🎯 项目完整路径: ${PROJECT_PATH}" "INFO"
    
    check_permissions
    stop_project
    start_containers
    show_containers_logs
    
    log "🎉 部署完成！以下是访问信息：
    📌 前端: 192.168.0.247/
    📌 后端接口: 192.168.0.247/api/v1/docs
    📌 登录信息: 账号 admin，密码 123456" "SUCCESS"
}

# 设置信号处理
trap handle_interrupt INT TERM


# 解析命令行参数
# 如果没有参数，则默认执行部署流程
if [ $# -eq 0 ]; then
    main "$@"
    exit 0
fi

while [[ $# -gt 0 ]]; do
    case $1 in
        --stop)
            stop_project
            exit 0
            ;;
        --start)
            main
            exit 0
            ;;
        --logs|-l)
            show_containers_logs
            exit 0
            ;;
        --help|-h)
            echo "使用说明："
            echo "  $0 [选项]"
            echo ""
            echo "选项："
            echo "  --stop      停止所有容器"
            echo "  --start     启动所有容器"
            echo "  --logs      查看所有容器日志"
            echo "  --help      显示此帮助信息"
            echo ""
            echo "默认执行完整部署流程："
            echo "  1. 检查权限"
            echo "  2. 停止现有容器"
            echo "  3. 更新代码"
            echo "  4. 构建前端"
            echo "  5. 启动容器"
            echo "  6. 显示日志"
            echo ""
            echo "日志查看命令："
            echo "  查看实时日志：docker compose logs -f [服务名]"
            echo "  服务名：backend, nginx, mysql, redis"
            exit 0
            ;;
        *)
            echo "未知参数: $1"
            exit 1
            ;;
    esac
done

main "$@"