"""priority_queue.py: your turn, work the most-at-risk ticket next.

First-in-first-out is the wrong order for a busy desk: a sev1 with a 15-minute SLA that has already
waited 12 minutes is far more urgent than a sev3 that just arrived with a 4-hour SLA. Order the routed
tickets by TIME REMAINING to their SLA (smallest first), so the desk always picks up whatever is
closest to breaching.

Steps:
  1. Finish `by_sla_urgency` below: return the tickets sorted by (sla_minutes - minutes_waited) ascending.
  2. A ticket already past its SLA (negative remaining) should sort to the very front.
  3. Run:  python priority_queue.py

The worked desk logic (triage, route, sla_check) is in ../solution/support_desk.py.
"""
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "solution"))
from support_desk import Ticket, triage, route


def by_sla_urgency(routed):
    """routed: list of (Ticket, minutes_waited). Return it ordered most-urgent-first.

    Urgency = time remaining to the SLA = ticket.sla_minutes - minutes_waited. Smaller (or negative)
    means more urgent and should come first.
    """
    # TODO: return sorted(routed, key=lambda pair: pair[0].sla_minutes - pair[1])
    raise NotImplementedError("implement the SLA-urgency ordering")


if __name__ == "__main__":
    raw = [
        ("A", "the whole app is down for all users", 12),   # sev1, SLA 15 -> 3 min left (urgent)
        ("B", "quick question how do i export", 30),        # sev3, SLA 240 -> 210 left (relaxed)
        ("C", "some users get a timeout error", 58),        # sev2, SLA 60 -> 2 min left (very urgent)
    ]
    tickets = [(route(triage(Ticket(i, text))), waited) for (i, text, waited) in raw]
    for tk, waited in by_sla_urgency(tickets):
        remaining = tk.sla_minutes - waited
        print(f"{tk.id}: {tk.severity} {tk.tier}  {remaining:>4} min to SLA")
    # Goal order: C (2 min), A (3 min), B (210 min).
