"""
Practice: the logic that turns the acme-crm-mcp demo's suites into a release decision
(scripts/smoke.sh, evals/runToolEvals.ts, and the CI step that reads their exit codes).

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

# Inspector CLI exit codes, as the demo's smoke test uses them
OK, AUTH_REQUIRED, TOOL_ERROR = 0, 3, 5


def smoke(checks: list[tuple[str, int, int]]) -> tuple[int, list[str]]:
    """Each check is (name, expected_exit, actual_exit). Return (exit_code, lines):
      - one line per check: "✓ <name>" if actual == expected, else
        "✗ <name> (exit <actual>, expected <expected>)"
      - a last line "<passed> passed, <failed> failed"
      - exit_code 0 if nothing failed, else 1

    TODO 1
    """
    return 0, []


def eval_gate(turns_by_model: dict[str, list[bool]], gate: float = 0.95) -> tuple[int, str]:
    """The tool-use eval's summary. Each model maps to its turns' pass/fail.
      - a model with no turns means the run didn't complete: (2, "eval failed: no turns for <model>")
        (check models in the order given)
      - otherwise the weakest model's pass rate decides:
        (0, "gate: weakest model <r>% ≥ <g>% → PASS") or (1, "gate: weakest model <r>% < <g>% → BLOCK RELEASE")
        with r and g as whole percentages (round(rate * 100), round(gate * 100))

    TODO 2
    """
    return 0, ""


def suites_for(trigger: str) -> list[str]:
    """Which suites to run, cheapest first:
      - "commit":  ["contract", "scorer"]                     (deterministic, every commit)
      - "deploy":  ["smoke"]                                  (the deployed server, in seconds)
      - "release": ["contract", "scorer", "tool-use eval"]    (plus real models, before a release)
    Anything else: []

    TODO 3
    """
    return []


def release_decision(exits: dict[str, int]) -> tuple[str, list[str]]:
    """Read the release suites' exit codes and decide ("ship" or "block", reasons). In this order:
      - a suite in suites_for("release") that's missing from `exits` -> "<suite> didn't run"
      - contract != 0       -> "contract tests failed: a real bug"
      - scorer != 0         -> "scorer tests failed: the eval can't be trusted"
      - tool-use eval == 1  -> "tool-use gate: weakest model below threshold"
      - tool-use eval == 2  -> "tool-use eval didn't complete: no verdict"
    No reasons -> ("ship", []). Any reason -> ("block", reasons). "Couldn't measure" blocks too.

    TODO 4
    """
    return "ship", []
