pub mod items;
pub mod registry;
pub mod search;
pub mod system;

use crate::mcp::types::AppState;

/// ツール名で実装をディスパッチ。新規ツール追加時は registry::tool_list() にも定義を追加すること。
pub async fn dispatch_tool(
    state: &AppState,
    _method: &str,
    actual_method: &str,
    args: &serde_json::Map<String, serde_json::Value>,
) -> Option<serde_json::Value> {
    match actual_method {
        "get_item_by_id" => items::handle_get_item_by_id(state, args).await,
        "add_item_text" => items::handle_add_item_text(state, args).await,
        "search_text" | "lsa_search" => search::handle_search_text(state, actual_method, args).await
            .or_else(|| Some(serde_json::json!({
                "content": [{ "type": "text", "text": r#"{"items":[],"vector_search_used":false}"# }]
            }))),
        "update_item" => items::handle_update_item(state, args).await,
        "delete_item" => items::handle_delete_item(state, args).await,
        "list_documents" => items::handle_list_documents(state, args).await,
        "list_categories" => items::handle_list_categories(state).await,
        "get_document_count" => items::handle_get_document_count(state).await,
        "get_document" => items::handle_get_document(state, args).await,
        "delete_document" => items::handle_delete_document(state, args).await,
        "lsa_retrain" => system::handle_lsa_retrain(state).await,
        _ => Some(serde_json::json!({
            "content": [{ "type": "text", "text": format!("Unknown tool: {}", actual_method) }],
            "isError": true
        })),
    }
}
