"""Connector registry (build plan Section 9.1).

A catalog of available connector types. New connectors register here on import;
nothing else in the core needs to change.
"""

from __future__ import annotations

from legate.agent.builtin_tools import BUILTIN_REGISTRY
from legate.agent.tooling import ToolSpec
from legate.connectors.base import Connector
from legate.connectors.email import EmailConnector
from legate.connectors.github import GitHubConnector
from legate.connectors.google import GoogleConnector
from legate.connectors.http import HttpConnector
from legate.connectors.mcp import McpConnector
from legate.connectors.mysql import MySQLConnector
from legate.connectors.notion import NotionConnector
from legate.connectors.postgres import PostgresConnector
from legate.connectors.slack import SlackConnector
from legate.connectors.stripe import StripeConnector
from legate.connectors.webhook import WebhookConnector


def _build_registry() -> dict[str, Connector]:
    registry: dict[str, Connector] = {}
    for connector in (
        HttpConnector(),
        SlackConnector(),
        EmailConnector(),
        GitHubConnector(),
        NotionConnector(),
        WebhookConnector(),
        PostgresConnector(),
        StripeConnector(),
        MySQLConnector(),
        GoogleConnector(),
        McpConnector(),
    ):
        registry[connector.type] = connector
    return registry


#: type -> connector instance.
CONNECTOR_REGISTRY: dict[str, Connector] = _build_registry()


def get_connector(connector_type: str) -> Connector | None:
    return CONNECTOR_REGISTRY.get(connector_type)


def catalog_tool_specs() -> list[ToolSpec]:
    """Every tool offered across all connector types (deduplicated by name)."""
    seen: dict[str, ToolSpec] = {}
    for connector in CONNECTOR_REGISTRY.values():
        for spec in connector.list_tools():
            seen.setdefault(spec.name, spec)
    return list(seen.values())


def known_tool_names() -> set[str]:
    """Built-in tool names plus every connector tool name (for allow-list checks)."""
    return set(BUILTIN_REGISTRY.names()) | {spec.name for spec in catalog_tool_specs()}
