"""
Read-only MCP tools for Outreach sequencing workflows.
"""

from __future__ import annotations

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

from g_gremlin import outreach as outreach_helpers

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


def outreach_whoami() -> Dict[str, Any]:
    identity = outreach_helpers.OutreachClient().whoami()
    return {"identity": identity}


def outreach_probe_capabilities() -> Dict[str, Any]:
    checks = outreach_helpers.OutreachClient().probe_capabilities()
    active_scopes = outreach_helpers.get_active_scopes()
    return {
        "count": len(checks),
        "checks": [check.to_dict() for check in checks],
        "active_scopes": active_scopes,
    }


def outreach_list_sequences(
    show_all: bool = False,
    page_size: int = 50,
    max_results: int = 1000,
) -> Dict[str, Any]:
    sequences = outreach_helpers.OutreachClient().list_sequences(
        enabled_only=not bool(show_all),
        page_size=max(int(page_size), 1),
        max_results=max(int(max_results), 1),
    )
    rows = [sequence.to_dict() for sequence in sequences]
    return {"count": len(rows), "sequences": rows}


def outreach_find_prospect_by_email(email: str) -> Dict[str, Any]:
    email_clean = str(email or "").strip()
    if not email_clean:
        raise ValueError("email is required for outreach.find_prospect_by_email.")
    prospect = outreach_helpers.OutreachClient().find_prospect_by_email(email_clean)
    if not prospect:
        return {"found": False, "prospect": None}
    return {"found": True, "prospect": prospect.to_dict()}


def get_tools() -> List[ToolTuple]:
    return [
        ("outreach.whoami", outreach_whoami, "Return authenticated Outreach user/org identity."),
        ("outreach.probe_capabilities", outreach_probe_capabilities, "Probe Outreach endpoint capability access."),
        ("outreach.list_sequences", outreach_list_sequences, "List Outreach sequences."),
        (
            "outreach.find_prospect_by_email",
            outreach_find_prospect_by_email,
            "Find an Outreach prospect by email address.",
        ),
    ]

