"""Pure, storage-free selection of a typed configuration write handler.

The registry deliberately contains no SQL, payload, or 1C metadata decoding.
It is the first migration seam out of the monolithic adapter server.
"""

from __future__ import annotations

from write.contracts import WriteHandler
from write.handlers.form import HANDLER as FORM_HANDLER
from write.handlers.module import HANDLER as MODULE_HANDLER
from write.handlers.object_member import HANDLER as OBJECT_MEMBER_HANDLER
from write.handlers.object_property import HANDLER as OBJECT_PROPERTY_HANDLER
from write.handlers.scheduled_job import HANDLER as SCHEDULE_HANDLER


def select_write_handler(*, target_kind: str, operation: str = "", is_schedule: bool = False) -> WriteHandler | None:
    """Return one supported typed handler or ``None`` for a forbidden target.

    Detailed form sub-routing (element, command, embedded module) remains in
    the form handler.  It needs decoded target evidence that is unavailable at
    this pure public-intent stage.
    """
    if is_schedule:
        return SCHEDULE_HANDLER
    normalized_kind = str(target_kind or "").strip().casefold()
    normalized_operation = str(operation or "").strip().casefold()
    if normalized_kind in {"object", "объект", "metadata", "метаданные"}:
        if normalized_operation in {"add_attribute", "attribute_add", "добавить_реквизит", "добавитьреквизит"}:
            return OBJECT_MEMBER_HANDLER
        return OBJECT_PROPERTY_HANDLER
    if normalized_kind in {"module", "модуль", "bsl"}:
        return MODULE_HANDLER
    if normalized_kind in {"form", "форма"}:
        return FORM_HANDLER
    return None


def registered_handlers() -> list[dict[str, str | None]]:
    """Public-safe registry summary; contains no SQL implementation details."""
    handlers = [MODULE_HANDLER, FORM_HANDLER, OBJECT_PROPERTY_HANDLER, OBJECT_MEMBER_HANDLER, SCHEDULE_HANDLER]
    return [
        {"key": handler.key, "target_kind": handler.public_target_kind, "operation": handler.operation}
        for handler in handlers
    ]
