namespace AxCopilot.Services.Agent;

public enum ToolRiskLevel { Low, Medium, High }

public static class ToolRiskMapper
{
    // Maps tool names to risk levels
    // High: file_write, file_edit, script_create, process_run, git (if destructive), sql, zip, http
    // Medium: file_read on sensitive paths, format_convert, template_render, batch_run
    // Low: file_read, glob, grep, date_time, math, base64, hash, diff, env_info, file_info, multi_read

    private static readonly Dictionary<string, ToolRiskLevel> _map = new(StringComparer.OrdinalIgnoreCase)
    {
        // High risk - modifying/executing
        ["file_write"] = ToolRiskLevel.High,
        ["file_edit"] = ToolRiskLevel.High,
        ["script_create"] = ToolRiskLevel.High,
        ["process_run"] = ToolRiskLevel.High,
        ["git"] = ToolRiskLevel.High,
        ["sql"] = ToolRiskLevel.High,
        ["zip"] = ToolRiskLevel.Medium,
        ["http"] = ToolRiskLevel.Medium,
        ["file_manage"] = ToolRiskLevel.High,
        ["build_run"] = ToolRiskLevel.High,
        ["test_loop"] = ToolRiskLevel.Medium,
        ["lsp"] = ToolRiskLevel.Low,
        ["delegate"] = ToolRiskLevel.Medium,
        // Medium risk
        ["format_convert"] = ToolRiskLevel.Medium,
        ["template_render"] = ToolRiskLevel.Medium,
        ["batch_run"] = ToolRiskLevel.High,
        ["html_create"] = ToolRiskLevel.Medium,
        ["excel_create"] = ToolRiskLevel.Medium,
        ["docx_create"] = ToolRiskLevel.Medium,
        ["pptx_create"] = ToolRiskLevel.Medium,
        ["csv_create"] = ToolRiskLevel.Medium,
        ["markdown_create"] = ToolRiskLevel.Medium,
        ["chart_create"] = ToolRiskLevel.Medium,
        ["document_assemble"] = ToolRiskLevel.Medium,
        // Low risk - read only
        ["file_read"] = ToolRiskLevel.Low,
        ["glob"] = ToolRiskLevel.Low,
        ["grep"] = ToolRiskLevel.Low,
        ["date_time"] = ToolRiskLevel.Low,
        ["math"] = ToolRiskLevel.Low,
        ["base64"] = ToolRiskLevel.Low,
        ["hash"] = ToolRiskLevel.Low,
        ["diff"] = ToolRiskLevel.Low,
        ["env_info"] = ToolRiskLevel.Low,
        ["file_info"] = ToolRiskLevel.Low,
        ["multi_read"] = ToolRiskLevel.Low,
        ["code_search"] = ToolRiskLevel.Low,
        ["folder_map"] = ToolRiskLevel.Low,
        ["document_reader"] = ToolRiskLevel.Low,
        ["document_plan"] = ToolRiskLevel.Low,
        ["document_review"] = ToolRiskLevel.Low,
        ["regex"] = ToolRiskLevel.Low,
        ["json"] = ToolRiskLevel.Low,
        ["xml"] = ToolRiskLevel.Low,
        ["encoding"] = ToolRiskLevel.Low,
        ["clipboard"] = ToolRiskLevel.Low,
        ["notify"] = ToolRiskLevel.Low,
        ["user_ask"] = ToolRiskLevel.Low,
        ["memory"] = ToolRiskLevel.Low,
        ["skill_manager"] = ToolRiskLevel.Low,
        ["image_analyze"] = ToolRiskLevel.Low,
        ["text_summarize"] = ToolRiskLevel.Low,
        ["data_pivot"] = ToolRiskLevel.Low,
        ["task_tracker"] = ToolRiskLevel.Low,
        ["suggest_actions"] = ToolRiskLevel.Low,
        ["diff_preview"] = ToolRiskLevel.Low,
        ["checkpoint"] = ToolRiskLevel.Low,
        ["playbook"] = ToolRiskLevel.Low,
        ["snippet_runner"] = ToolRiskLevel.Medium,
        ["open_external"] = ToolRiskLevel.Medium,
    };

    public static ToolRiskLevel GetRisk(string toolName)
    {
        if (_map.TryGetValue(toolName, out var risk)) return risk;
        // MCP tools — treat as Medium by default
        if (toolName.StartsWith("mcp__", StringComparison.OrdinalIgnoreCase)) return ToolRiskLevel.Medium;
        return ToolRiskLevel.Low;
    }

    public static string GetRiskLabel(ToolRiskLevel risk) => risk switch
    {
        ToolRiskLevel.High => "⚠ 높음",
        ToolRiskLevel.Medium => "• 보통",
        _ => "· 낮음"
    };

    public static bool RequiresConfirmation(string toolName, string permissionMode)
    {
        var risk = GetRisk(toolName);
        return permissionMode switch
        {
            "bypassPermissions" => false,
            "acceptEdits" => risk == ToolRiskLevel.High && toolName != "file_write" && toolName != "file_edit",
            "plan" => risk >= ToolRiskLevel.Medium,
            _ => risk == ToolRiskLevel.High
        };
    }
}
