"""MCP tool schema/handler registry를 제공한다."""

from __future__ import annotations


TOOL_REGISTRY: list[dict[str, object]] = [
    {"name": "sari_guide", "description": "Return quick usage guide in pack1 format", "inputSchema": {"type": "object", "properties": {}}, "handler_attr": "_sari_guide_tool"},
    {"name": "status", "description": "Return repository runtime/index status in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}}}, "handler_attr": "_status_tool"},
    {"name": "search", "description": "Search symbols/files in pack1 format", "inputSchema": {"type": "object", "required": ["repo", "query"], "properties": {"repo": {"type": "string"}, "query": {"type": "string"}, "limit": {"type": "integer", "minimum": 1}}}, "handler_attr": "_search_tool"},
    {"name": "doctor", "description": "Return runtime health checks in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}}}, "handler_attr": "_doctor_tool"},
    {"name": "rescan", "description": "Invalidate symbol cache in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}}}, "handler_attr": "_rescan_tool"},
    {"name": "repo_candidates", "description": "List repository candidates in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}}}, "handler_attr": "_repo_candidates_tool"},
    {"name": "read", "description": "Unified read interface in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "mode": {"type": "string"}, "target": {"type": "string"}, "path": {"type": "string"}, "offset": {"type": "integer", "minimum": 0}, "limit": {"type": "integer", "minimum": 1}, "content": {"type": "string"}, "against": {"type": "string"}, "tag": {"type": "string"}}}, "handler_attr": "_read_tool"},
    {"name": "dry_run_diff", "description": "Legacy diff preview wrapper in pack1 format", "inputSchema": {"type": "object", "required": ["repo", "path", "content"], "properties": {"repo": {"type": "string"}, "path": {"type": "string"}, "content": {"type": "string"}, "against": {"type": "string"}}}, "handler_attr": "_dry_run_diff_tool"},
    {"name": "scan_once", "description": "Scan repository files once and enqueue enrich jobs", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}}}, "handler_attr": "_scan_once_tool"},
    {"name": "list_files", "description": "List indexed files for repository in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "limit": {"type": "integer", "minimum": 1}, "prefix": {"type": "string"}}}, "handler_attr": "_list_files_tool"},
    {"name": "read_file", "description": "Read indexed file content in pack1 format", "inputSchema": {"type": "object", "required": ["repo", "relative_path"], "properties": {"repo": {"type": "string"}, "relative_path": {"type": "string"}, "offset": {"type": "integer", "minimum": 0}, "limit": {"type": "integer", "minimum": 1}}}, "handler_attr": "_read_file_tool"},
    {"name": "index_file", "description": "Incrementally index single file in pack1 format", "inputSchema": {"type": "object", "required": ["repo", "relative_path"], "properties": {"repo": {"type": "string"}, "relative_path": {"type": "string"}}}, "handler_attr": "_index_file_tool"},
    {"name": "list_symbols", "description": "List indexed symbols in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "query": {"type": "string"}, "limit": {"type": "integer", "minimum": 1}}}, "handler_attr": "_list_symbols_tool"},
    {"name": "read_symbol", "description": "Read symbol detail in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "name": {"type": "string"}, "symbol_id": {"type": "string"}, "sid": {"type": "string"}, "path": {"type": "string"}, "limit": {"type": "integer", "minimum": 1}}}, "handler_attr": "_read_symbol_tool"},
    {"name": "search_symbol", "description": "Search indexed symbols in pack1 format", "inputSchema": {"type": "object", "required": ["repo", "query"], "properties": {"repo": {"type": "string"}, "query": {"type": "string"}, "limit": {"type": "integer", "minimum": 1}, "path_prefix": {"type": "string"}}}, "handler_attr": "_search_symbol_tool"},
    {"name": "get_callers", "description": "Get caller edges for symbol in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "symbol": {"type": "string"}, "symbol_id": {"type": "string"}, "sid": {"type": "string"}, "limit": {"type": "integer", "minimum": 1}, "scope": {"type": "string"}}}, "handler_attr": "_get_callers_tool"},
    {"name": "get_implementations", "description": "Get implementation candidates for symbol in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "symbol": {"type": "string"}, "symbol_id": {"type": "string"}, "sid": {"type": "string"}, "limit": {"type": "integer", "minimum": 1}, "scope": {"type": "string"}}}, "handler_attr": "_get_implementations_tool"},
    {"name": "call_graph", "description": "Get call graph for symbol in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "symbol": {"type": "string"}, "symbol_id": {"type": "string"}, "sid": {"type": "string"}, "limit": {"type": "integer", "minimum": 1}, "scope": {"type": "string"}}}, "handler_attr": "_call_graph_tool"},
    {"name": "call_graph_health", "description": "Get call graph health summary in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "scope": {"type": "string"}}}, "handler_attr": "_call_graph_health_tool"},
    {"name": "knowledge", "description": "Query archived knowledge entries in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "query": {"type": "string"}, "limit": {"type": "integer", "minimum": 1}}}, "handler_attr": "_knowledge_tool"},
    {"name": "save_snippet", "description": "Save code snippet to local store in pack1 format", "inputSchema": {"type": "object", "required": ["repo", "path", "start_line", "end_line", "tag"], "properties": {"repo": {"type": "string"}, "path": {"type": "string"}, "start_line": {"type": "integer", "minimum": 1}, "end_line": {"type": "integer", "minimum": 1}, "tag": {"type": "string"}, "note": {"type": "string"}, "commit": {"type": "string"}}}, "handler_attr": "_save_snippet_tool"},
    {"name": "get_snippet", "description": "Query saved snippets in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "tag": {"type": "string"}, "query": {"type": "string"}, "limit": {"type": "integer", "minimum": 1}}}, "handler_attr": "_get_snippet_tool"},
    {"name": "archive_context", "description": "Archive context notes in pack1 format", "inputSchema": {"type": "object", "required": ["repo", "topic", "content"], "properties": {"repo": {"type": "string"}, "topic": {"type": "string"}, "content": {"type": "string"}, "tags": {"type": "array", "items": {"type": "string"}}, "related_files": {"type": "array", "items": {"type": "string"}}}}, "handler_attr": "_archive_context_tool"},
    {"name": "get_context", "description": "Get archived context notes in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "query": {"type": "string"}, "limit": {"type": "integer", "minimum": 1}}}, "handler_attr": "_get_context_tool"},
    {"name": "pipeline_policy_get", "description": "Get pipeline policy in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}}}, "handler_attr": "_pipeline_policy_get_tool"},
    {"name": "pipeline_policy_set", "description": "Set pipeline policy in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "deletion_hold": {"type": ["string", "boolean"]}, "l3_p95_threshold_ms": {"type": "integer", "minimum": 1}, "dead_ratio_threshold_bps": {"type": "integer", "minimum": 1}, "workers": {"type": "integer", "minimum": 1}, "watcher_queue_max": {"type": "integer", "minimum": 100}, "watcher_overflow_rescan_cooldown_sec": {"type": "integer", "minimum": 1}, "alert_window_sec": {"type": "integer", "minimum": 60}}}, "handler_attr": "_pipeline_policy_set_tool"},
    {"name": "pipeline_alert_status", "description": "Get pipeline alert snapshot in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}}}, "handler_attr": "_pipeline_alert_status_tool"},
    {"name": "pipeline_dead_list", "description": "List dead enrich jobs in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "limit": {"type": "integer", "minimum": 1}}}, "handler_attr": "_pipeline_dead_list_tool"},
    {"name": "pipeline_dead_requeue", "description": "Requeue dead enrich jobs in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "limit": {"type": "integer", "minimum": 1}, "all": {"type": ["boolean", "string"]}}}, "handler_attr": "_pipeline_dead_requeue_tool"},
    {"name": "pipeline_dead_purge", "description": "Purge dead enrich jobs in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "limit": {"type": "integer", "minimum": 1}, "all": {"type": ["boolean", "string"]}}}, "handler_attr": "_pipeline_dead_purge_tool"},
    {"name": "pipeline_auto_status", "description": "Get pipeline auto-control state in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}}}, "handler_attr": "_pipeline_auto_status_tool"},
    {"name": "pipeline_auto_set", "description": "Set pipeline auto-control enabled flag in pack1 format", "inputSchema": {"type": "object", "required": ["repo", "enabled"], "properties": {"repo": {"type": "string"}, "enabled": {"type": ["string", "boolean"]}}}, "handler_attr": "_pipeline_auto_set_tool"},
    {"name": "pipeline_auto_tick", "description": "Evaluate pipeline auto-control once in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}}}, "handler_attr": "_pipeline_auto_tick_tool"},
    {"name": "pipeline_quality_run", "description": "Run L3 quality evaluation in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "limit_files": {"type": "integer", "minimum": 1}, "profile": {"type": "string"}, "language_filter": {"type": ["string", "array"], "items": {"type": "string"}}}}, "handler_attr": "_pipeline_quality_run_tool"},
    {"name": "pipeline_quality_report", "description": "Return latest L3 quality summary in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}}}, "handler_attr": "_pipeline_quality_report_tool"},
    {"name": "pipeline_lsp_matrix_run", "description": "Run LSP readiness matrix and hard gate in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}, "required_languages": {"type": ["string", "array"], "items": {"type": "string"}}, "fail_on_unavailable": {"type": ["boolean", "string"]}, "strict_all_languages": {"type": ["boolean", "string"]}, "strict_symbol_gate": {"type": ["boolean", "string"]}}}, "handler_attr": "_pipeline_lsp_matrix_run_tool"},
    {"name": "pipeline_lsp_matrix_report", "description": "Return latest LSP readiness matrix report in pack1 format", "inputSchema": {"type": "object", "required": ["repo"], "properties": {"repo": {"type": "string"}}}, "handler_attr": "_pipeline_lsp_matrix_report_tool"},
]


def build_tool_handler_attr_map() -> dict[str, str]:
    """tool name -> McpServer handler attribute map을 반환한다."""
    return {str(item["name"]): str(item["handler_attr"]) for item in TOOL_REGISTRY}


def build_public_tool_schemas() -> list[dict[str, object]]:
    """tools/list용 공개 스키마 목록을 반환한다."""
    out: list[dict[str, object]] = []
    for item in TOOL_REGISTRY:
        out.append(
            {
                "name": item["name"],
                "description": item["description"],
                "inputSchema": item["inputSchema"],
            }
        )
    return out
