"""services/access_notice.py

The "Not available" card the UI draws when a capability is refused because
the caller isn't a manager (HA-193 / Figma).

Background: every manager-only node already refused non-managers with a
sentence of prose, which the renderer had no way to tell apart from an
ordinary answer — so a permission refusal arrived looking like a normal
chat bubble, and the Figma's titled amber notice could not be built. The
refusals also apologised ("this isn't something I can pull up for your
role") instead of saying what the caller CAN still do.

This module supplies both halves of the fix:

  * `build()` — the structured payload the card renders from. It rides on
    the `access_notice` stream event AND inside the `done` event's `data`,
    because only the second survives for a non-streaming client and for
    conversation history replay.
  * `message()` — the two-sentence copy pattern the Figma uses: first what
    the capability requires, then the self-serve alternative. No apology.

The `*_restricted` done-intents are deliberately left alone. They are the
established contract (team_leave_restricted, team_attendance_restricted,
team_members_restricted), several acceptance suites key off them, and the
card needs a payload rather than a new intent label to render.
"""

#: The card heading, verbatim from the Figma. One title for every refusal —
#: the varying half is the message, not the heading.
TITLE = "Not available"

#: Why access was refused. Only one cause reaches this module today, but the
#: card is a permission notice in general, so the reason is stated in the
#: payload rather than left implicit in the copy.
REASON_NOT_A_MANAGER = "not_a_manager"

#: What the caller would need. Named as a requirement rather than a role
#: because erp-api gates on having reports, not on holding a title — an "hr"
#: JWT claim with no reports is refused by erp-api all the same.
REQUIRES_REPORTS = "manager_with_reports"


def message(capability_phrase: str, alternative: str) -> str:
    """The Figma's two-sentence refusal.

    `capability_phrase` names the thing in the plural as the user asked for
    it ("Team leave summaries"); `alternative` completes "You can still ..."
    with something the caller genuinely has access to — never invent one.
    """
    return (f"{capability_phrase} are available to managers with direct or "
            f"indirect reports. You can still {alternative}.")


def build(capability: str, text: str) -> dict:
    """The `access_notice` payload for one refused capability.

    `capability` is the stable machine name of what was refused
    ("team_leave", "team_attendance", "team_members") so the frontend can
    branch on it without parsing prose; `text` is the copy from `message()`.
    """
    return {
        "title": TITLE,
        "message": text,
        "capability": capability,
        "reason": REASON_NOT_A_MANAGER,
        "requires": REQUIRES_REPORTS,
    }


# ── "Access restricted": another employee's own records ────────────────────
# A different refusal from "Not available". That card says a CAPABILITY needs
# reports; this one says the RECORD belongs to someone else ("Show Adarsh
# attendance details?" from an employee). Same event and payload shape, so
# the renderer needs no new event type - it tells the two apart by `reason`
# (and may style this one differently, as the Access-restricted design does).
#
# The copy never confirms whether the named person exists or has records
# (FR-06.4 / FR-08.2): the reply is identical for every name.

RESTRICTED_TITLE = "Access restricted"

#: The record requested belongs to another employee.
REASON_OTHER_EMPLOYEE = "other_employee_record"

#: Who may see another employee's records.
REQUIRES_MANAGER_OR_HR = "manager_or_hr"


def build_restricted(capability: str, text: str) -> dict:
    """The `access_notice` payload for a request about someone else's record."""
    return {
        "title": RESTRICTED_TITLE,
        "message": text,
        "capability": capability,
        "reason": REASON_OTHER_EMPLOYEE,
        "requires": REQUIRES_MANAGER_OR_HR,
    }


# ── ONE intent for every refusal a normal user gets ─────────────────────────
# Product direction: a user who is neither manager nor HR is refused under a
# single done-intent, whatever they asked for - team leave, team attendance,
# the roster, approvals, someone else's attendance / punches / shift / leave /
# balance - so the frontend has one value to pick the restricted UI from.
# WHAT was refused travels in the card payload (`capability`, `reason`,
# `title`), not in the intent. Replaces team_leave_restricted,
# team_attendance_restricted, team_members_restricted,
# individual_report_restricted, leave_approval_restricted,
# regularisation_approval_restricted and attendance_restricted.
#
# The value is team_attendance_restricted because that is the intent the
# frontend ALREADY renders the restricted UI for - every deny response now
# carries it, rather than asking the frontend to learn a new one.
RESTRICTED_INTENT = "team_attendance_restricted"


def refuse(writer, capability: str, text: str, *, other_employee: bool = False) -> dict:
    """Stream the card for one refusal and return the `extra_data` to put on
    the done event (so history renders the same card). `other_employee`
    picks the "Access restricted" payload (someone else's record) over the
    "Not available" one (a manager-only capability)."""
    notice = build_restricted(capability, text) if other_employee else build(capability, text)
    writer({"type": "access_notice", "data": notice})
    return {"access_notice": notice}
