//! AR-11（方案A）：数据变更联动刷新。
//!
//! 工具执行成功后按映射 emit "df-data-changed" { entity, action }，
//! 前端 store listen 该事件按 entity 调 loadProjects/loadTasks/loadIdeas 刷新列表，
//! 取代工具执行后需手动刷新。
//!
//! 第四批从 audit/mod.rs 抽离,行为零变更。

use tauri::{AppHandle, Emitter};

/// AR-11（方案A）：工具名 → (entity, action) 映射，用于数据变更联动刷新。
///
/// 仅数据变更类工具返回 Some，其余工具返回 None（不 emit df-data-changed）。
/// 映射规则：
/// - create_project/create_idea/create_task → (project/idea/task, create)
/// - update_project/update_task/update_idea → (..., update)
/// - delete_project/delete_task/delete_idea/purge_project → (..., delete)
/// - restore_project → (project, update)（恢复等同状态变更，前端走 update 刷新）
/// - bind_directory → (project, update)（绑定目录属项目字段更新）
pub(super) fn data_change_for_tool(name: &str) -> Option<(&'static str, &'static str)> {
    let (entity, action) = match name {
        "create_project" => ("project", "create"),
        "create_task" => ("task", "create"),
        "create_idea" => ("idea", "create"),
        "update_project" => ("project", "update"),
        "update_task" => ("task", "update"),
        "update_idea" => ("idea", "update"),
        "delete_project" | "purge_project" => ("project", "delete"),
        "delete_task" => ("task", "delete"),
        "delete_idea" => ("idea", "delete"),
        "restore_project" => ("project", "update"),
        "bind_directory" => ("project", "update"),
        "write_file" | "patch_file" => ("file", "update"),
        _ => return None,
    };
    Some((entity, action))
}

/// AR-11（方案A）：工具执行成功后按映射 emit "df-data-changed" { entity, action }。
///
/// 前端 store listen 该事件，按 entity 调 loadProjects/loadTasks/loadIdeas 刷新列表，
/// 取代工具执行后需手动刷新。仅工具名命中 data_change_for_tool 映射才 emit，
/// 其余工具（run_workflow/list_tasks 等）不 emit。emit 失败不阻断（let _ =）。
pub(crate) fn emit_data_changed(app_handle: &AppHandle, tool_name: &str) {
    if let Some((entity, action)) = data_change_for_tool(tool_name) {
        let _ = app_handle.emit(
            "df-data-changed",
            serde_json::json!({ "entity": entity, "action": action }),
        );
    }
}
