"""
Practice: what crosses a handoff. Lab 10's three ways to pass a case to the next agent
(agents.run_team, context="note" | "full" | "text"), its labelled log (agents.render_log), and the
counts the chapter uses: repeated lookups, "answered at once" after a handoff, and ping-pong.

Fill in the TODOs, then run the tests:
    pytest test_handoff_practice.py        (locally)
or press "Run tests" on the chapter page.
"""
import json

# Tools that only read. Calling one again with the same inputs is a repeated lookup.
READS = {"find_customer", "get_customer", "list_orders", "get_order", "track_shipment", "search_products",
         "check_stock", "get_refunds"}


def render_log(agent: str, messages: list[dict]) -> list[str]:
    """What an agent did, as labelled lines for the next agent ("text" mode). For each message:
      - assistant: one line per tool call:  f"[{agent}] called {name}({arguments})"
                   and, if it has text:      f"[{agent}] replied to the customer: {content}"
      - tool:      f"    → {content[:600]}"   (four spaces, an arrow, the result)
      - anything else: nothing
    A tool call looks like {"function": {"name": ..., "arguments": "<a JSON string>"}}.

    TODO 1
    """
    return []


def next_input(mode: str, system: str, customer: str, transcript: list[dict], log: list[str],
               note_from: str, note: str) -> list[dict]:
    """The messages the next agent is given after a handoff.
      - "full": its own system prompt, then every earlier message exactly as it was (`transcript`:
                the customer's message, the earlier agent's assistant tool calls and the tool results)
      - "note": its own system prompt, then ONE user message:
                customer + f"\n\nHandoff note from the {note_from} agent: {note}"
      - "text": like "note", but the log goes between the customer's words and the note:
                customer + "\n\nWhat the team has done so far (you are taking over):\n" + the log lines
                joined by "\n" + the note part above

    TODO 2
    """
    return []


def repeated_lookups(steps: list[dict]) -> int:
    """How many successful read calls (tool in READS, status "ok") repeat an earlier successful read with
    the same tool and the same arguments, in the same task. Compare arguments with
    json.dumps(args, sort_keys=True), so key order doesn't matter. Each step is
    {"tool": ..., "args": {...}, "status": ...}.

    TODO 3
    """
    return 0


def answered_at_once(runs: list[dict]) -> tuple[int, int, int]:
    """Among runs with at least one handoff: how many runs had one, how many ended with an agent that
    answered on its first model call (the last agent in `path` has turns == 1: it used no tool), and how
    many of those failed. Each run is {"handoffs": int, "path": [...], "last_turns": int, "success": bool}.
    Return (runs with a handoff, answered at once, of those failed).

    TODO 4
    """
    return (0, 0, 0)


def ping_pong(path: list[str]) -> int:
    """How many times the case bounced straight back: A → B → A counts once at each return
    (path[i] == path[i - 2] and path[i] != path[i - 1]).

    TODO 5
    """
    return 0
