from __future__ import annotations

CHANNEL_TRUST: dict[str, str] = {
    "imessage":        "high",
    "bluebubbles":     "high",
    "macos":           "high",
    "ios":             "high",
    "android":         "high",
    "webchat":         "high",
    "slack":           "medium",
    "discord":         "medium",
    "msteams":         "medium",
    "googlechat":      "medium",
    "nextcloud":       "medium",
    "telegram":        "low",
    "whatsapp":        "low",
    "signal":          "low",
    "matrix":          "low",
    "nostr":           "low",
    "tlon":            "low",
    "zalo":            "low",
    "zalo_personal":   "low",
    "webhook":         "untrusted",
    "gmail":           "untrusted",
    "unknown":         "untrusted",
}

TRUST_SEVERITY: dict[str, str] = {
    "high":      "none",
    "medium":    "none",
    "low":       "warning",
    "untrusted": "high",
}

TRUST_RANK: dict[str, int] = {
    "high":      3,
    "medium":    2,
    "low":       1,
    "untrusted": 0,
}


def score(channel: str) -> dict:
    channel_norm = channel.strip().lower()
    trust = CHANNEL_TRUST.get(channel_norm, "untrusted")
    severity = TRUST_SEVERITY[trust]

    findings = []
    if trust in ("low", "untrusted"):
        findings.append({
            "type":     "low_trust_channel",
            "channel":  channel,
            "trust":    trust,
            "severity": severity,
        })

    return {
        "detected":  bool(findings),
        "channel":   channel,
        "trust":     trust,
        "findings":  findings,
        "severity":  severity,
        "layer":     "channel_trust",
    }


def is_higher_trust(channel_a: str, channel_b: str) -> bool:
    trust_a = CHANNEL_TRUST.get(channel_a.strip().lower(), "untrusted")
    trust_b = CHANNEL_TRUST.get(channel_b.strip().lower(), "untrusted")
    return TRUST_RANK[trust_a] > TRUST_RANK[trust_b]


def get_trust(channel: str) -> str:
    return CHANNEL_TRUST.get(channel.strip().lower(), "untrusted")


def is_risky(channel: str) -> bool:
    return get_trust(channel) in ("low", "untrusted")