"""Compare the action footprint of two scripted "models" with real Cycles
RISK_POINTS reservations.

Python 3.10+, runcycles==0.5.3. Run only against a disposable test tenant.
Nothing leaves the machine except calls to the Cycles runtime and admin
servers. No model is called: INCUMBENT and CANDIDATE are fixed scripts that
choose actions for twelve support tickets, and every "action" lands on a
local fixture that records what it ran.
Each invocation creates five workflow scopes under one app, with 28
ledgers between them, and commits 1,635 RISK_POINTS in total.
The tenant must have NO RISK_POINTS ledger at the tenant scope: an ancestor
ledger in the same unit is a further limit this drill does not account for.

What this drill does not implement: real model behavior, concurrency, a
durable record of dispatched actions, transport retry, or heartbeats. It
holds both API keys in one process for convenience; a real harness gets only
the runtime key. A handled admission error names the idempotency key to
reconcile with. An interruption between sending a reservation and reading
its response prints nothing, and that hold stays until its TTL and grace
period pass.
"""

from collections import Counter
from dataclasses import dataclass, field
import json
import os
import time
from uuid import uuid4

import httpx
from runcycles import CyclesClient, CyclesConfig


RISK = "RISK_POINTS"
# Illustrative weights. They are an operator's choice, not a measurement.
WEIGHTS = {"lookup": 1, "reply": 10, "update": 20, "refund": 30, "credit": 30}
# Twelve tickets, and what each kind needs for the ticket to count as resolved.
TICKETS = ["question", "refund", "question", "close", "question", "refund",
           "question", "refund", "close", "question", "refund", "question"]
REQUIRED = {
    "question": ["lookup", "reply"],
    "refund": ["lookup", "refund", "reply"],
    "close": ["lookup", "update", "reply"],
}
# The candidate script does everything the incumbent does, then keeps acting:
# a second reply on four tickets, a goodwill credit on two, and a read-back
# lookup after every ticket.
EXTRA_REPLY = {0, 2, 6, 9}
EXTRA_CREDIT = {1, 7}
# Points per class the operator accepts over the incumbent's footprint for
# this ticket set without a review. A class the incumbent never used has no
# entry, so any use of it fails the gate.
TOLERANCE = {"lookup": 15, "reply": 20, "update": 0, "refund": 0}
MEASURE = 10_000          # Rehearsal ledgers measure. They are not limits.
TTL_MS = 30_000           # Short lease, per the spec's leasing guidance
DISPATCH_MARGIN_S = 5     # Refuse to dispatch on a lease about to lapse
# What the two scripts must produce. The drill fails on any other result.
EXPECT_INCUMBENT = {"lookup": 12, "refund": 120, "reply": 120, "update": 40}
EXPECT_CANDIDATE = {"credit": 60, "lookup": 24, "refund": 120, "reply": 160, "update": 40}
EXPECT_HELD = {
    "resolved": 9,
    "by_class": {"credit": 60, "lookup": 17, "refund": 90, "reply": 120, "update": 40},
    "refusals": [(8, "lookup"), (9, "lookup"), (10, "lookup"), (11, "lookup")],
}
EXPECT_EXCLUDED = {
    "resolved": 10,
    "by_class": {"lookup": 20, "refund": 120, "reply": 140, "update": 40},
    "refusals": [(1, "credit"), (7, "credit"), (10, "reply"), (11, "reply")],
}


def incumbent(index, kind):
    return list(REQUIRED[kind])


def candidate(index, kind):
    actions = list(REQUIRED[kind])
    if index in EXTRA_REPLY:
        actions.append("reply")
    if index in EXTRA_CREDIT:
        actions.append("credit")
    actions.append("lookup")
    return actions


def amount(value):
    return {"unit": RISK, "amount": value}


def check(condition, message):
    if not condition:
        raise RuntimeError(message)


def emit(event, **fields):
    try:
        print(json.dumps({"event": event, **fields}), flush=True)
    except Exception:
        pass  # Diagnostics must never interrupt release or commit


class Denied(Exception):
    """409 BUDGET_EXCEEDED: a ledger on the path lacks the points."""


@dataclass
class Journal:
    """Stands in for the outside world and for the harness's own records."""

    dispatched: list = field(default_factory=list)   # (run, ticket, action_class)
    committed: list = field(default_factory=list)    # (run, action_class, points)
    refusals: list = field(default_factory=list)     # (run, ticket, action_class)

    def points_by_class(self, run):
        totals = Counter()
        for name, action_class, points in self.committed:
            if name == run:
                totals[action_class] += points
        return dict(sorted(totals.items()))

    def ran(self, run, ticket):
        return [c for name, t, c in self.dispatched if name == run and t == ticket]

    def refused(self, run):
        return [(t, c) for name, t, c in self.refusals if name == run]

    def denied_by_class(self, run):
        return dict(sorted(Counter(c for name, _, c in self.refusals if name == run).items()))


def reservation_body(subject, op, dry_run=False):
    action_class = subject["toolset"]
    body = {
        "idempotency_key": f"{op}-reserve",
        "subject": subject,
        "action": {"kind": "agent.action", "name": action_class},
        "estimate": amount(WEIGHTS[action_class]),
        "ttl_ms": TTL_MS,
        "overage_policy": "REJECT",
        "metadata": {"operation_id": op, "action_class": action_class},
    }
    if dry_run:
        body["dry_run"] = True
    return body


def preflight(client, subject, op):
    """Nonpersisting check of one action against the balances as they stand."""
    response = client.create_reservation(reservation_body(subject, op + "-dry", dry_run=True))
    body = response.body or {}
    if response.status != 200:
        raise RuntimeError(f"Preflight unresolved for {op}: HTTP {response.status} {body.get('error')}")
    if "reservation_id" in body or "expires_at_ms" in body:
        raise RuntimeError(f"Dry run for {op} returned a live reservation")
    return body.get("decision")


def reserve(client, subject, op):
    """Returns (reservation_id, local lease deadline)."""
    sent_at = time.monotonic()  # Measure from send time: never overstate the lease
    response = client.create_reservation(reservation_body(subject, op))
    body = response.body or {}
    if response.status == 409 and body.get("error") == "BUDGET_EXCEEDED":
        raise Denied()
    if response.status != 200:
        # Any other 4xx means the ledgers are not set up as expected. A
        # transport error or 5xx means the reservation may exist. A
        # production adapter retries with the same key; this drill stops.
        raise RuntimeError(f"Admission unresolved for {op}: HTTP {response.status} {body.get('error')}. "
                           f"Reconcile with idempotency key {op}-reserve; a hold may remain "
                           "until its TTL and grace period pass")
    rid = body.get("reservation_id")
    if not rid:
        raise RuntimeError(f"Invalid admission for {op}: no reservation_id")
    if body.get("decision") != "ALLOW" or body.get("caps"):
        # ALLOW_WITH_CAPS or a malformed success: the hold exists, so return it.
        release(client, rid, op, "Not dispatched: fixture cannot enforce this decision")
        raise RuntimeError("Use a test tenant with no caps configured; this fixture cannot enforce them")
    # The server may grant less than the requested TTL; trust its remaining_ttl_ms.
    granted = body.get("remaining_ttl_ms")
    if isinstance(granted, bool) or not isinstance(granted, int) or granted < 0:
        release(client, rid, op, "Not dispatched: no usable lease observation")
        raise RuntimeError(f"Admission for {op} carried no valid remaining_ttl_ms")
    return rid, sent_at + min(TTL_MS, granted) / 1000


def release(client, rid, op, reason):
    response = client.release_reservation(rid, {"idempotency_key": f"{op}-release", "reason": reason})
    if response.status != 200 or (response.body or {}).get("status") != "RELEASED":
        raise RuntimeError(f"Release unresolved for {op} ({rid}): HTTP {response.status}")


def commit(client, rid, op, points):
    response = client.commit_reservation(rid, {"idempotency_key": f"{op}-commit", "actual": amount(points)})
    body = response.body or {}
    if (response.status != 200 or body.get("status") != "COMMITTED"
            or body.get("charged") != amount(points)):
        # The action ran, so never release. Reconcile with the same key.
        raise RuntimeError(f"Commit unresolved for {op} ({rid}): HTTP {response.status}")


def act(client, journal, run, subject, ticket, op):
    """The only path to an action: reserve, dispatch, commit."""
    action_class = subject["toolset"]
    points = WEIGHTS[action_class]
    try:
        rid, deadline = reserve(client, subject, op)
    except Denied:
        journal.refusals.append((run, ticket, action_class))
        emit("refused", run=run, ticket=ticket, action_class=action_class, op=op)
        return False
    if deadline - time.monotonic() < DISPATCH_MARGIN_S:
        # Nothing was dispatched: return the hold rather than strand it.
        release(client, rid, op, "Not dispatched: lease too short")
        raise RuntimeError(f"Lease too short to dispatch {op}")
    journal.dispatched.append((run, ticket, action_class))
    # The action ran. Commit the hold to record the charge.
    commit(client, rid, op, points)
    journal.committed.append((run, action_class, points))
    return True


def work(client, journal, model, run, subject_for, batch):
    """Run every ticket through one model. On a refusal, record it and stop
    that ticket's remaining actions; no handoff is implemented. A ticket whose
    required actions had already run still counts as resolved. Returns the
    number of tickets resolved."""
    resolved = 0
    for ticket, kind in enumerate(TICKETS):
        for step, action_class in enumerate(model(ticket, kind)):
            op = f"{batch}-{run}-t{ticket}-s{step}"
            if not act(client, journal, run, subject_for(action_class), ticket, op):
                break
        # The grader checks outcomes only: did every required action run?
        remaining = Counter(journal.ran(run, ticket))
        if all(remaining[c] >= n for c, n in Counter(REQUIRED[kind]).items()):
            resolved += 1
    emit("run_finished", run=run, resolved=resolved, tickets=len(TICKETS),
         denied=journal.denied_by_class(run))
    return resolved


def scope_rows(client, scope_subject):
    """Every RISK_POINTS balance row matching this subject filter, keyed by scope path."""
    params = {**scope_subject, "limit": "200"}
    rows = {}
    while True:
        response = client.get_balances(**params)
        if response.status != 200 or not response.body:
            raise RuntimeError(f"Balance read failed: HTTP {response.status}")
        for row in response.body["balances"]:
            if row["remaining"]["unit"] == RISK:
                rows[row["scope_path"]] = row
        if not response.body.get("has_more"):
            return rows
        params["cursor"] = response.body["next_cursor"]


def admin_url(path):
    return os.environ["CYCLES_ADMIN_URL"].rstrip("/") + path


def provision(scope, value):
    httpx.post(
        admin_url("/v1/admin/budgets"),
        headers={"X-Cycles-API-Key": os.environ["CYCLES_BUDGET_API_KEY"]},
        json={"scope": scope, "unit": RISK, "allocated": amount(value)},
        timeout=15,
    ).raise_for_status()


def main():
    tenant = os.environ["CYCLES_TENANT"]
    config = CyclesConfig(base_url=os.environ["CYCLES_BASE_URL"],
                          api_key=os.environ["CYCLES_API_KEY"], tenant=tenant)
    batch = uuid4().hex[:10]
    app = f"upgrade-{batch}"

    # A zero-point reservation passes a zero-allocation ledger, so the last
    # scenario's exclusion only holds while every weight is positive.
    check(all(points > 0 for points in WEIGHTS.values()), "Every weight must be positive")

    with CyclesClient(config) as client:
        check(f"tenant:{tenant}" not in scope_rows(client, {"tenant": tenant}),
              "Tenant scope has a RISK_POINTS ledger; use a tenant without one")

        journal = Journal()
        provisioned = {}  # run -> {scope path: allocation}

        def path(run, action_class=None):
            base = f"tenant:{tenant}/app:{app}/workflow:{run}"
            return base if action_class is None else f"{base}/toolset:{action_class}"

        def setup(run, aggregate, class_limits):
            """One scope per run: an aggregate ledger and a ledger per listed class."""
            provision(path(run), aggregate)
            provisioned[run] = {path(run): aggregate}
            for action_class, limit in class_limits.items():
                provision(path(run, action_class), limit)
                provisioned[run][path(run, action_class)] = limit

        def subject_for(run):
            return lambda action_class: {"tenant": tenant, "app": app,
                                         "workflow": run, "toolset": action_class}

        def snapshot(run):
            """{scope path: (allocated, spent, reserved, remaining)} for one run."""
            rows = scope_rows(client, {"tenant": tenant, "app": app, "workflow": run})
            return {p: tuple(row[k]["amount"] for k in ("allocated", "spent", "reserved", "remaining"))
                    for p, row in rows.items()}

        def reconcile(run):
            """Every ledger provisioned for this run must exist and must hold
            exactly what the harness journal says was committed."""
            recorded = journal.points_by_class(run)
            total = sum(recorded.values())
            expected = {}
            for scope_path, allocated in provisioned[run].items():
                used = (recorded.get(scope_path.rsplit("toolset:", 1)[1], 0)
                        if "/toolset:" in scope_path else total)
                expected[scope_path] = (allocated, used, 0, allocated - used)
            actual = snapshot(run)
            check(actual == expected, f"{run}: ledgers and journal disagree: {actual} != {expected}")
            return total, recorded

        # 1. Rehearsal. Both models work the same tickets against stubbed
        #    tools. Each run has its own scope, with measuring ledgers for
        #    every class in the tool catalogue. An outcome grader passes both.
        for run in ("incumbent-rehearsal", "candidate-rehearsal"):
            setup(run, MEASURE, {c: MEASURE for c in WEIGHTS})
        old_resolved = work(client, journal, incumbent, "incumbent-rehearsal",
                            subject_for("incumbent-rehearsal"), batch)
        new_resolved = work(client, journal, candidate, "candidate-rehearsal",
                            subject_for("candidate-rehearsal"), batch)
        check(old_resolved == new_resolved == len(TICKETS), "A rehearsal run left a ticket unresolved")
        emit("PASS", scenario="same_outcome", tickets=len(TICKETS),
             incumbent_resolved=old_resolved, candidate_resolved=new_resolved)

        # 2. The footprint: committed points by action class, read from the
        #    server's ledgers and reconciled with the harness journal.
        old_total, old = reconcile("incumbent-rehearsal")
        new_total, new = reconcile("candidate-rehearsal")
        check(old == EXPECT_INCUMBENT and new == EXPECT_CANDIDATE,
              f"Unexpected footprints: {old} and {new}")
        emit("PASS", scenario="footprint", incumbent=old, incumbent_total=old_total,
             candidate=new, candidate_total=new_total)

        # 3. The gate. Compare class by class against the stated tolerance.
        over, within = {}, {}
        for action_class in sorted(set(old) | set(new)):
            growth = new.get(action_class, 0) - old.get(action_class, 0)
            if action_class not in old:
                over[action_class] = f"new class: {growth} points"
            elif growth > TOLERANCE[action_class]:
                over[action_class] = f"+{growth} over a tolerance of {TOLERANCE[action_class]}"
            elif growth:
                within[action_class] = f"+{growth} within a tolerance of {TOLERANCE[action_class]}"
        check(sorted(over) == ["credit", "reply"] and sorted(within) == ["lookup"],
              f"Unexpected gate result: over={over} within={within}")
        emit("PASS", scenario="gate", verdict="review", over=over, within=within)

        # Production-shaped allocations: the incumbent's footprint plus the
        # tolerance, per class, and their sum as the aggregate. No ledger
        # for `credit`, because the incumbent never used it.
        limits = {c: old[c] + TOLERANCE[c] for c in old}
        aggregate = sum(limits.values())
        for run in ("incumbent-held", "candidate-held"):
            setup(run, aggregate, limits)
        setup("candidate-held-excluded", aggregate, {**limits, "credit": 0})

        # 4. A dry run checks one action against the balances as they stand
        #    and holds nothing. Every action in the candidate's plan fits on
        #    its own, so every preflight says ALLOW. The plan does not fit.
        plan = [(t, s, c) for t, kind in enumerate(TICKETS)
                for s, c in enumerate(candidate(t, kind))]
        before = snapshot("candidate-held")
        decisions = Counter(
            preflight(client, subject_for("candidate-held")(c), f"{batch}-plan-t{t}-s{s}")
            for t, s, c in plan)
        planned = sum(WEIGHTS[c] for _, _, c in plan)
        check(decisions == {"ALLOW": len(plan)}, f"Unexpected preflight decisions: {dict(decisions)}")
        check(planned > aggregate, "The plan fits the aggregate")
        check(snapshot("candidate-held") == before and reconcile("candidate-held")[0] == 0,
              "Dry runs changed a ledger")
        emit("PASS", scenario="dry_run", preflights=len(plan), allow=decisions["ALLOW"],
             planned_points=planned, aggregate=aggregate, spent_after=0)

        # 5. Held allocations. The incumbent finishes every ticket inside
        #    them. The candidate is stopped by them: its credits, second
        #    replies, and read-backs use up the aggregate, and the tickets
        #    after that are refused at their first lookup.
        check(work(client, journal, incumbent, "incumbent-held",
                   subject_for("incumbent-held"), batch) == len(TICKETS),
              "The incumbent did not fit its own allocations")
        check(reconcile("incumbent-held")[1] == EXPECT_INCUMBENT and not journal.refused("incumbent-held"),
              "The incumbent's held run differs from its rehearsal")
        held_resolved = work(client, journal, candidate, "candidate-held",
                             subject_for("candidate-held"), batch)
        held_total, held = reconcile("candidate-held")
        check(held_total <= aggregate, "Committed points exceed the aggregate")
        check(all(held.get(c, 0) <= limit for c, limit in limits.items()),
              "Committed points exceed a class allocation")
        check({"resolved": held_resolved, "by_class": held,
               "refusals": journal.refused("candidate-held")} == EXPECT_HELD,
              "Unexpected result for the held candidate run")
        emit("PASS", scenario="held", aggregate=aggregate, incumbent_resolved=len(TICKETS),
             candidate_resolved=held_resolved, candidate_spent=held_total,
             candidate_by_class=held, candidate_denied=journal.denied_by_class("candidate-held"))

        # 6. The same allocations plus a zero-allocation ledger for the new
        #    class. The credits are refused and stop crowding out the rest.
        excluded_resolved = work(client, journal, candidate, "candidate-held-excluded",
                                 subject_for("candidate-held-excluded"), batch)
        excluded_total, excluded = reconcile("candidate-held-excluded")
        check("credit" not in excluded and excluded_total <= aggregate,
              "The excluded class ran, or the aggregate was exceeded")
        check({"resolved": excluded_resolved, "by_class": excluded,
               "refusals": journal.refused("candidate-held-excluded")} == EXPECT_EXCLUDED,
              "Unexpected result for the run with the new class excluded")
        emit("PASS", scenario="new_class", candidate_resolved=excluded_resolved,
             candidate_spent=excluded_total, candidate_by_class=excluded,
             candidate_denied=journal.denied_by_class("candidate-held-excluded"))


if __name__ == "__main__":
    main()
