from __future__ import annotations

CAPABILITY_SCHEMA_VERSION = 1

_TRANSPORT_CAPABILITIES: dict[str, dict[str, bool]] = {
    "signal": {
        "send": True,
        "reactions": True,
        "edits": False,
        "deletes": False,
        "threaded_replies": True,
        "typing": True,
        "read_receipts": True,
        "media_images": True,
        "media_video": True,
        "media_audio": True,
        "media_documents": True,
        "delivery_receipt": True,
        "read_receipt": True,
        "composing": True,
        "composing_stopped": True,
        "presence": False,
        "presence_last_seen": False,
    },
    "whatsapp": {
        "send": True,
        "reactions": True,
        "edits": False,
        "deletes": False,
        "threaded_replies": True,
        "typing": True,
        "read_receipts": True,
        "media_images": True,
        "media_video": True,
        "media_audio": True,
        "media_documents": True,
        "delivery_receipt": True,
        "read_receipt": True,
        "composing": True,
        "composing_stopped": True,
        "presence": True,
        "presence_last_seen": True,
    },
    "instagram": {
        "send": True,
        "reactions": False,
        "edits": False,
        "deletes": False,
        "threaded_replies": False,
        "typing": True,
        "read_receipts": False,
        "media_images": True,
        "media_video": True,
        "media_audio": False,
        "media_documents": False,
        "delivery_receipt": False,
        "read_receipt": True,
        "composing": True,
        "composing_stopped": False,
        "presence": True,
        "presence_last_seen": False,
    },
    "xmpp": {
        "send": False,
        "reactions": False,
        "edits": False,
        "deletes": False,
        "threaded_replies": False,
        "typing": False,
        "read_receipts": False,
        "media_images": False,
        "media_video": False,
        "media_audio": False,
        "media_documents": False,
        "delivery_receipt": True,
        "read_receipt": True,
        "composing": True,
        "composing_stopped": True,
        "composing_paused": True,
        "composing_inactive": True,
        "composing_gone": True,
        "presence": True,
        "presence_last_seen": True,
    },
}


def _service_key(service: str) -> str:
    return str(service or "").strip().lower()


def _capabilities_for(service: str) -> dict[str, bool]:
    defaults = _TRANSPORT_CAPABILITIES.get(_service_key(service))
    if defaults is None:
        return {}
    return dict(defaults)


def supports(service: str, feature: str) -> bool:
    feature_key = str(feature or "").strip().lower()
    if not feature_key:
        return False
    return bool(_capabilities_for(service).get(feature_key, False))


def unsupported_reason(service: str, feature: str) -> str:
    if supports(service, feature):
        return ""
    service_key = _service_key(service) or "unknown"
    feature_key = str(feature or "").strip().lower() or "requested_action"
    return f"{service_key} does not support {feature_key}."


def capability_snapshot(service: str = "") -> dict:
    if service:
        key = _service_key(service)
        return {
            "schema_version": CAPABILITY_SCHEMA_VERSION,
            "service": key,
            "capabilities": _capabilities_for(key),
        }
    return {
        "schema_version": CAPABILITY_SCHEMA_VERSION,
        "services": {
            key: dict(value) for key, value in sorted(_TRANSPORT_CAPABILITIES.items())
        },
    }
