"""
Read-only MCP tools for ZoomInfo enrichment workflows.
"""

from __future__ import annotations

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

from g_gremlin import zoominfo as zoominfo_helpers

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


def zoominfo_doctor() -> Dict[str, Any]:
    try:
        client = zoominfo_helpers.ZoomInfoClient()
    except Exception as exc:
        return {
            "connected": False,
            "auth_mode": None,
            "workspace_id": None,
            "org_id": None,
            "org_name": None,
            "token_expires_at": None,
            "error": str(exc),
        }

    config = getattr(client, "config", {}) or {}
    result: Dict[str, Any] = {
        "connected": False,
        "auth_mode": config.get("zoominfo_auth_mode") or config.get("auth_mode"),
        "workspace_id": config.get("zoominfo_workspace_id") or config.get("workspace_id"),
        "org_id": config.get("zoominfo_org_id"),
        "org_name": config.get("zoominfo_org_name"),
        "token_expires_at": config.get("zoominfo_token_expires_at"),
    }
    try:
        token = client._get_access_token()  # noqa: SLF001 - intentional health check call
    except Exception as exc:
        result["error"] = str(exc)
        return result

    result["connected"] = bool(token)
    return result


def zoominfo_enrich_company(domain: str, fields: List[str] | None = None) -> Dict[str, Any]:
    domain_clean = str(domain or "").strip()
    if not domain_clean:
        raise ValueError("domain is required for zoominfo.enrich_company.")

    company = zoominfo_helpers.ZoomInfoClient().enrich_company(domain=domain_clean, fields=fields)
    return {
        "found": bool(company),
        "company": company,
    }


def get_tools() -> List[ToolTuple]:
    return [
        ("zoominfo.doctor", zoominfo_doctor, "Validate ZoomInfo auth/token health and return connection status."),
        ("zoominfo.enrich_company", zoominfo_enrich_company, "Enrich a company profile by domain via ZoomInfo."),
    ]
