#!/bin/bash

# Task Debug Script
# 用于打印任务的详细信息，包括所有环境变量
# 主要用于调试和排查问题

set -e

# 引入公共库
source "${SCRIPT_DIR:-.}/lib.sh"

# 打印分隔线
print_separator() {
    log_info "========================================"
}

# 打印标题
print_title() {
    print_separator
    echo "  $1"
    print_separator
}

# 打印键值对
print_kv() {
    printf "%-30s : %s\n" "$1" "$2"
}

# 主函数
main() {
    log_highlight "开始任务调试信息输出"
    
    # 1. 打印任务基本信息
    print_title "任务基本信息"
    print_kv "任务ID" "${TASK_ID:-未设置}"
    print_kv "任务名称" "${TASK_NAME:-未设置}"
    print_kv "任务类型" "${TASK_TYPE:-未设置}"
    print_kv "任务状态" "${TASK_STATUS:-未设置}"
    print_kv "任务描述" "${TASK_DESCRIPTION:-未设置}"
    print_kv "执行者" "${TASK_RUN_BY:-未设置}"
    print_kv "Agent 环境" "${TASK_AGENT_ENV:-未设置}"
    print_kv "调度的 Agent" "${TASK_SCHEDULED_AGENT:-未设置}"
    echo ""
    
    # 2. 打印标准环境变量
    print_title "标准环境变量"
    print_kv "工作目录" "${WORKSPACE:-未设置}"
    print_kv "脚本目录" "${SCRIPT_DIR:-未设置}"
    print_kv "用户" "${USER:-未设置}"
    print_kv "主机名" "${HOSTNAME:-未设置}"
    print_kv "PWD" "${PWD:-未设置}"
    print_kv "HOME" "${HOME:-未设置}"
    print_kv "SHELL" "${SHELL:-未设置}"
    echo ""
    
    # 3. 打印任务参数（PARAM_ 开头）
    print_title "任务参数 (PARAM_*)"
    local param_found=false
    while IFS='=' read -r name value; do
        if [[ $name == PARAM_* ]]; then
            param_found=true
            # 移除 PARAM_ 前缀显示
            local param_name="${name#PARAM_}"
            print_kv "$param_name" "$value"
        fi
    done < <(env | sort)
    
    if [ "$param_found" = false ]; then
        echo "  (无任务参数)"
    fi
    echo ""
    
    # 4. 打印任务定义（DEFINE_ 开头）
    print_title "任务定义 (DEFINE_*)"
    local define_found=false
    while IFS='=' read -r name value; do
        if [[ $name == DEFINE_* ]]; then
            define_found=true
            # 移除 DEFINE_ 前缀显示
            local define_name="${name#DEFINE_}"
            print_kv "$define_name" "$value"
        fi
    done < <(env | sort)
    
    if [ "$define_found" = false ]; then
        echo "  (无任务定义)"
    fi
    echo ""
    
    # 5. 打印所有环境变量（按字母排序）
    print_title "所有环境变量"
    env | sort | while IFS='=' read -r name value; do
        # 截断过长的值
        if [ ${#value} -gt 100 ]; then
            value="${value:0:100}... (truncated)"
        fi
        print_kv "$name" "$value"
    done
    echo ""
    
    # 6. 打印系统信息
    print_title "系统信息"
    print_kv "操作系统" "$(uname -s)"
    print_kv "内核版本" "$(uname -r)"
    print_kv "架构" "$(uname -m)"
    
    if command -v lsb_release &> /dev/null; then
        print_kv "发行版" "$(lsb_release -d | cut -f2-)"
    elif [ -f /etc/os-release ]; then
        print_kv "发行版" "$(grep PRETTY_NAME /etc/os-release | cut -d'"' -f2)"
    fi
    
    # 打印资源使用情况
    if command -v free &> /dev/null; then
        local mem_info=$(free -h | grep Mem | awk '{print $3 "/" $2 " (used/total)"}')
        print_kv "内存使用" "$mem_info"
    fi
    
    if command -v df &> /dev/null; then
        local disk_info=$(df -h . | tail -1 | awk '{print $3 "/" $2 " (" $5 " used)"}')
        print_kv "磁盘使用" "$disk_info"
    fi
    
    print_kv "CPU 核心数" "$(nproc 2>/dev/null || echo 'unknown')"
    print_kv "当前时间" "$(date '+%Y-%m-%d %H:%M:%S %Z')"
    echo ""
    
    # 7. 打印工作目录内容
    print_title "工作目录内容"
    if [ -d "${WORKSPACE}" ]; then
        print_kv "工作目录路径" "${WORKSPACE}"
        echo ""
        echo "文件列表:"
        ls -lh "${WORKSPACE}" 2>/dev/null | tail -n +2 | while read -r line; do
            echo "  $line"
        done
    else
        echo "  工作目录不存在: ${WORKSPACE:-未设置}"
    fi
    echo ""
    
    # 8. 打印网络信息
    print_title "网络信息"
    if command -v ip &> /dev/null; then
        echo "IP 地址:"
        ip addr show | grep -E "inet |inet6 " | awk '{print "  " $0}'
    elif command -v ifconfig &> /dev/null; then
        echo "IP 地址:"
        ifconfig | grep -E "inet |inet6 " | awk '{print "  " $0}'
    fi
    echo ""
    
    # 9. 打印 Docker 信息（如果可用）
    if command -v docker &> /dev/null; then
        print_title "Docker 信息"
        print_kv "Docker 版本" "$(docker --version 2>/dev/null | cut -d' ' -f3 | tr -d ',')"
        
        # 检查 Docker 是否运行
        if docker info &> /dev/null; then
            local running_containers=$(docker ps -q | wc -l)
            local all_containers=$(docker ps -aq | wc -l)
            print_kv "运行中的容器" "$running_containers"
            print_kv "总容器数" "$all_containers"
            
            local images_count=$(docker images -q | wc -l)
            print_kv "镜像数量" "$images_count"
        else
            echo "  Docker daemon 未运行"
        fi
        echo ""
    fi
    
    # 10. 打印进程信息
    print_title "进程信息"
    print_kv "当前进程 PID" "$$"
    print_kv "父进程 PID" "$PPID"
    
    if command -v ps &> /dev/null; then
        echo ""
        echo "当前进程树:"
        ps auxf 2>/dev/null | grep -E "(PID|$$)" | head -5 | while read -r line; do
            echo "  $line"
        done
    fi
    echo ""
    
    # 11. 环境变量统计
    print_title "环境变量统计"
    local total_env=$(env | wc -l)
    local param_count=$(env | grep -c "^PARAM_" || echo 0)
    local define_count=$(env | grep -c "^DEFINE_" || echo 0)
    local task_count=$(env | grep -c "^TASK_" || echo 0)
    
    print_kv "总环境变量数" "$total_env"
    print_kv "任务参数数 (PARAM_*)" "$param_count"
    print_kv "任务定义数 (DEFINE_*)" "$define_count"
    print_kv "任务信息数 (TASK_*)" "$task_count"
    echo ""
    
    print_separator
    log_success "任务调试信息输出完成"
    
    return 0
}

# 执行主函数
main "$@"
exit $?