from __future__ import annotations

from app.text import duration_phrase


def assess(info: dict, max_duration: int) -> tuple[bool, str | None, str | None]:
    """Return (allowed, code, message) for a resolved video."""
    live_status = (info.get("live_status") or "").lower()
    if info.get("is_live") or live_status in {"is_live", "is_upcoming"}:
        return False, "live", "This video is live or has not finished. Choose a finished video."

    availability = (info.get("availability") or "").lower()
    if availability in {"private", "needs_auth", "subscriber_only", "premium_only"}:
        return (
            False,
            "not_public",
            "This video is private or limited to members, so the desk will not take its audio.",
        )

    duration = info.get("duration")
    if duration is None:
        return False, "unknown_length", "This video has no length, so it cannot be checked against the limit."
    try:
        seconds = int(duration)
    except (TypeError, ValueError):
        return False, "unknown_length", "This video has no length, so it cannot be checked against the limit."
    if seconds <= 0:
        return False, "unknown_length", "This video has no length, so it cannot be checked against the limit."
    if seconds > max_duration:
        return (
            False,
            "too_long",
            f"This video is {duration_phrase(seconds)}. The desk limit is {duration_phrase(max_duration)}.",
        )
    return True, None, None
