"""
Analyze MCP tools (plan/drift/verify) for Salesforce packs.
"""

from __future__ import annotations

from pathlib import Path
from typing import Any, Callable, Dict, List, Tuple

from g_gremlin import sfdc_dedupe_workflow
from g_gremlin import sfdc_metadata_pack
from g_gremlin import sfdc_pack

ToolTuple = Tuple[str, Callable[..., Dict[str, Any]], str]


def sfdc_metadata_pack_plan(pack_dir: str, target: str | None = None) -> Dict[str, Any]:
    return sfdc_metadata_pack.plan_pack(Path(pack_dir), org_alias=target).to_dict()


def sfdc_metadata_pack_drift(pack_dir: str, target: str | None = None) -> Dict[str, Any]:
    return sfdc_metadata_pack.compute_drift(Path(pack_dir), org_alias=target).to_dict()


def sfdc_metadata_pack_verify(pack_dir: str, target: str | None = None) -> Dict[str, Any]:
    return sfdc_metadata_pack.verify_pack(Path(pack_dir), org_alias=target).to_dict()


def sfdc_pack_plan(pack_dir: str, target: str | None = None) -> Dict[str, Any]:
    return sfdc_pack.plan_pack(Path(pack_dir), org_alias=target).to_dict()


def sfdc_dedupe_plan(
    sources: List[Dict[str, Any]],
    output_path: str,
    review_output_path: str | None = None,
    key_columns: List[str] | None = None,
    threshold: int | None = None,
    local: bool = False,
    survivor_policy: str = "crm_person",
    blocking_keys: List[str] | None = None,
    profile: str = "default",
    block_size_cap: int = 5000,
    limit_per_source: int | None = None,
    overwrite: bool = False,
    include_plan: bool = False,
) -> Dict[str, Any]:
    return sfdc_dedupe_workflow.build_dedupe_plan(
        sources=sources,
        output_path=output_path,
        review_output_path=review_output_path,
        key_columns=key_columns,
        threshold=threshold,
        local=local,
        survivor_policy=survivor_policy,
        blocking_keys=blocking_keys,
        profile=profile,
        block_size_cap=block_size_cap,
        limit_per_source=limit_per_source,
        overwrite=overwrite,
        include_plan=include_plan,
    )


def sfdc_dedupe_verify(
    receipt_path: str,
    target: str | None = None,
    plan_path: str | None = None,
) -> Dict[str, Any]:
    return sfdc_dedupe_workflow.verify_dedupe_run(
        receipt_path=receipt_path,
        target=target,
        plan_path=plan_path,
    )


def get_tools() -> List[ToolTuple]:
    return [
        ("sfdc.metadata_pack.plan", sfdc_metadata_pack_plan, "Plan metadata pack changes."),
        ("sfdc.metadata_pack.drift", sfdc_metadata_pack_drift, "Compare local vs remote metadata drift."),
        ("sfdc.metadata_pack.verify", sfdc_metadata_pack_verify, "Run metadata smoke assertions."),
        ("sfdc.pack.plan", sfdc_pack_plan, "Plan data pack updates."),
        ("sfdc.dedupe.plan", sfdc_dedupe_plan, "Build a Salesforce dedupe plan and return plan_hash."),
        ("sfdc.dedupe.verify", sfdc_dedupe_verify, "Verify a Salesforce dedupe receipt against live org state."),
    ]
