#!/bin/bash
################################################################################
# 60_repair_capability_check.sh — 修复能力检测
# 功能: 检测修复脚本/工具是否就绪，不执行修复
# 参考: issue_handler.sh
# 日期: 2026-07-22
# 说明: 只检测不修复，验证修复工具链可用性
################################################################################

# 获取脚本所在目录并加载依赖
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LIB_DIR="${LIB_DIR:-/tmp/check_modules}"

# 加载配置文件和通用函数库
if [ -f "$LIB_DIR/lib/config.sh" ]; then
    source "$LIB_DIR/lib/config.sh"
else
    echo "ERROR: 配置文件不存在: $LIB_DIR/lib/config.sh"
    exit 1
fi

if [ -f "$LIB_DIR/lib/common.sh" ]; then
    source "$LIB_DIR/lib/common.sh"
else
    echo "ERROR: 通用函数库不存在: $LIB_DIR/lib/common.sh"
    exit 1
fi

# ==================== 平台类型识别 ====================

detect_platform_type() {
    [ -d "/data/services" ] && echo "new" || echo "old"
}

# ==================== 修复脚本检测 ====================

check_repair_scripts() {
    local scripts_ok="no"
    local scripts_found=0
    local scripts_list=""

    # 常见修复脚本路径
    local repair_script_paths=(
        "/opt/troubleshoot/issue_handler.sh"
        "/data/scripts/issue_handler.sh"
        "/home/scripts/issue_handler.sh"
        "/root/issue_handler.sh"
        "/usr/local/bin/issue_handler.sh"
    )

    for script in "${repair_script_paths[@]}"; do
        if [ -f "$script" ]; then
            scripts_found=$((scripts_found + 1))
            if [ -n "$scripts_list" ]; then
                scripts_list="${scripts_list},"
            fi
            scripts_list="${scripts_list}${script}"
        fi
    done

    if [ "$scripts_found" -gt 0 ]; then
        scripts_ok="yes"
    fi

    output_result "REPAIR_SCRIPT_EXISTS" "$scripts_ok"
    output_result "REPAIR_SCRIPT_COUNT" "$scripts_found"
    output_result "REPAIR_SCRIPT_LIST" "${scripts_list:-无}"

    if [ "$scripts_ok" = "yes" ]; then
        output_result "REPAIR_SCRIPT_LEVEL" "正常"
    else
        output_result "REPAIR_SCRIPT_LEVEL" "警告"
    fi
}

# ==================== DNS 修复能力检测 ====================

check_dns_repair_capability() {
    local capable="no"
    local detail=""

    # 检查 resolv.conf 是否可写
    if [ -f "/etc/resolv.conf" ]; then
        if [ -w "/etc/resolv.conf" ]; then
            capable="yes"
            detail="resolv.conf可写"
        else
            detail="resolv.conf只读"
        fi
    else
        detail="resolv.conf不存在"
    fi

    # 检查 NetworkManager
    if command -v nmcli >/dev/null 2>&1; then
        detail="${detail},NetworkManager可用"
    fi

    output_result "REPAIR_DNS_CAPABLE" "$capable"
    output_result "REPAIR_DNS_DETAIL" "${detail:-无工具}"
    output_result "REPAIR_DNS_LEVEL" "$([ "$capable" = "yes" ] && echo "正常" || echo "警告")"
}

# ==================== NTP 修复能力检测 ====================

check_ntp_repair_capability() {
    local capable="no"
    local detail=""

    # 检查 chrony
    if command -v chronyd >/dev/null 2>&1; then
        capable="yes"
        detail="chrony"
    elif command -v ntpd >/dev/null 2>&1; then
        capable="yes"
        detail="ntp"
    else
        detail="未安装NTP服务"
    fi

    # 检查配置文件可写性
    if [ -f "/etc/chrony.conf" ] && [ -w "/etc/chrony.conf" ]; then
        detail="${detail},chrony.conf可写"
    elif [ -f "/etc/ntp.conf" ] && [ -w "/etc/ntp.conf" ]; then
        detail="${detail},ntp.conf可写"
    fi

    output_result "REPAIR_NTP_CAPABLE" "$capable"
    output_result "REPAIR_NTP_DETAIL" "${detail:-无工具}"
    output_result "REPAIR_NTP_LEVEL" "$([ "$capable" = "yes" ] && echo "正常" || echo "警告")"
}

# ==================== 端口修复能力检测 ====================

check_port_repair_capability() {
    local capable="no"
    local detail=""

    # 检查防火墙工具
    if command -v firewall-cmd >/dev/null 2>&1; then
        capable="yes"
        detail="firewalld"
    elif command -v iptables >/dev/null 2>&1; then
        capable="yes"
        detail="iptables"
    else
        detail="无防火墙工具"
    fi

    output_result "REPAIR_PORT_CAPABLE" "$capable"
    output_result "REPAIR_PORT_DETAIL" "${detail:-无工具}"
    output_result "REPAIR_PORT_LEVEL" "$([ "$capable" = "yes" ] && echo "正常" || echo "警告")"
}

# ==================== 权限修复能力检测 ====================

check_permission_repair_capability() {
    local capable="no"
    local detail=""

    # 检查 chmod/chown
    if command -v chmod >/dev/null 2>&1 && command -v chown >/dev/null 2>&1; then
        capable="yes"
        detail="chmod+chown可用"
    else
        detail="权限工具缺失"
    fi

    # 检查关键目录是否可写
    local platform_type
    platform_type=$(detect_platform_type)

    if [ "$platform_type" = "new" ]; then
        if [ -w "/data/services/api" ] 2>/dev/null; then
            detail="${detail},/data/services可写"
        else
            detail="${detail},/data/services不可写"
        fi
    else
        if [ -w "/var/www/java" ] 2>/dev/null; then
            detail="${detail},/var/www/java可写"
        else
            detail="${detail},/var/www/java不可写"
        fi
    fi

    output_result "REPAIR_PERM_CAPABLE" "$capable"
    output_result "REPAIR_PERM_DETAIL" "${detail:-无工具}"
    output_result "REPAIR_PERM_LEVEL" "$([ "$capable" = "yes" ] && echo "正常" || echo "警告")"
}

# ==================== Docker 修复能力检测 ====================

check_docker_repair_capability() {
    local capable="no"
    local detail=""

    if command -v docker >/dev/null 2>&1; then
        capable="yes"
        # 检查 docker 权限
        if docker ps >/dev/null 2>&1; then
            detail="docker可用"
        else
            detail="docker无权限"
            capable="no"
        fi
    else
        detail="docker未安装"
    fi

    output_result "REPAIR_DOCKER_CAPABLE" "$capable"
    output_result "REPAIR_DOCKER_DETAIL" "${detail:-无工具}"
    output_result "REPAIR_DOCKER_LEVEL" "$([ "$capable" = "yes" ] && echo "正常" || echo "警告")"
}

# ==================== 服务重启能力检测 ====================

check_service_restart_capability() {
    local capable="no"
    local detail=""

    if command -v systemctl >/dev/null 2>&1; then
        capable="yes"
        detail="systemctl"
    elif command -v service >/dev/null 2>&1; then
        capable="yes"
        detail="service"
    else
        detail="无服务管理工具"
    fi

    output_result "REPAIR_SERVICE_CAPABLE" "$capable"
    output_result "REPAIR_SERVICE_DETAIL" "${detail:-无工具}"
    output_result "REPAIR_SERVICE_LEVEL" "$([ "$capable" = "yes" ] && echo "正常" || echo "警告")"
}

# ==================== 日志清理能力检测 ====================

check_log_cleanup_capability() {
    local capable="no"
    local detail=""

    # 检查日志清理工具
    local tools_found=0

    if command -v logrotate >/dev/null 2>&1; then
        tools_found=$((tools_found + 1))
        detail="logrotate"
    fi

    if command -v lsof >/dev/null 2>&1; then
        tools_found=$((tools_found + 1))
        detail="${detail}+lsof"
    fi

    if command -v find >/dev/null 2>&1; then
        tools_found=$((tools_found + 1))
        detail="${detail}+find"
    fi

    if [ "$tools_found" -ge 2 ]; then
        capable="yes"
    fi

    output_result "REPAIR_LOG_CLEAN_CAPABLE" "$capable"
    output_result "REPAIR_LOG_CLEAN_DETAIL" "${detail:-工具不足}"
    output_result "REPAIR_LOG_CLEAN_LEVEL" "$([ "$capable" = "yes" ] && echo "正常" || echo "警告")"
}

# ==================== 主检测流程 ====================

main() {
    log_info "开始修复能力检测..."

    set +e

    # 1. 修复脚本检测
    check_repair_scripts

    # 2. DNS 修复能力
    check_dns_repair_capability

    # 3. NTP 修复能力
    check_ntp_repair_capability

    # 4. 端口修复能力
    check_port_repair_capability

    # 5. 权限修复能力
    check_permission_repair_capability

    # 6. Docker 修复能力
    check_docker_repair_capability

    # 7. 服务重启能力
    check_service_restart_capability

    # 8. 日志清理能力
    check_log_cleanup_capability

    # 9. 综合状态
    output_result "REPAIR_STATUS" "已检测"
    output_result "REPAIR_STATUS_LEVEL" "正常"

    log_info "修复能力检测完成"
}

main