"""Bounded read-only state projection for the Telegram operator console."""

from __future__ import annotations

from dataclasses import dataclass


@dataclass(frozen=True, slots=True)
class OperatorRuntimeState:
    status_override: str | None
    checkin_state: str
    proposal_state: str


def _first_text(
    candidates: tuple[object | None, ...],
    attribute: str,
    fallback: str,
) -> str:
    for candidate in candidates:
        value = getattr(candidate, attribute, None)
        text = str(value or "").strip()
        if text:
            return text
    return fallback


def operator_runtime_state(
    coordinator: object,
    customer_key: str,
) -> OperatorRuntimeState:
    resolved = getattr(coordinator, "_by_key", {}).get(customer_key)
    runtime = getattr(resolved, "customer", resolved)
    safety = getattr(coordinator, "_customer_has_safety_hold", None)
    held = callable(safety) and safety(customer_key) is True
    return OperatorRuntimeState(
        status_override="안전 보류" if held else None,
        checkin_state=_first_text(
            (runtime, resolved),
            "today_checkin_state",
            "확인 필요",
        ),
        proposal_state=_first_text(
            (resolved, runtime),
            "proposal_state",
            "내부 검토방에서 확인",
        ),
    )
