"""
cards_cs03.py — the ONE card list for CASE-STUDY-03, imported by both the report
builder and the site builder.

AUDIT-08: these two builders each carried their own hand-written copy. When the report's
OpenAI row was corrected the site kept the old one, and the site<->report gate did not
notice because it pinned only the four headline figures. One list, two consumers, and a
gate that now pins every rendered cell.

Every multiple and window below was re-read from the vendor's own live page on
2026-07-29. The OpenAI read multiple is taken from the cached-input-to-input RATIO
within a pricing-table row, not from absolute prices: that page carries Standard, Batch,
Flex and Priority tiers, the ratio is invariant to which tier a reader is billed on and
the absolute prices are not — and this study never needs them.
"""
from __future__ import annotations

CARDS = [
    {"label": "Anthropic 5-minute cache", "r": 0.10, "w": 1.25, "windows": (300,),
     "note": "cited card, default tier"},
    {"label": "Anthropic 1-hour cache", "r": 0.10, "w": 2.00, "windows": (3600,),
     "note": "cited card, extended tier"},
    {"label": "OpenAI automatic, GPT-5.6+", "r": 0.10, "w": 1.25, "windows": (1800,),
     "note": "1.25x write; at least 30 minutes"},
    {"label": "OpenAI automatic, before GPT-5.6", "r": 0.10, "w": 1.00, "windows": (300, 600),
     "note": "no additional fee; 5-10 min idle"},
]


def crossover(a, b):
    """Hit rate at which two cards return the same share of the input bill.

    share(h) = 1 - (h*r + (1-h)*w) = (w - r)*h - (w - 1), a straight line in h.
    Two lines meet where h*[(w1 - r1) - (w2 - r2)] = w1 - w2. Parallel lines
    (identical read/write spread) never cross, and this returns None.

    This closed form REPLACES a hand-derived constant (0.25/0.40 = 0.625) that was
    correct arithmetic applied to a misread card. The assertions below prove the general
    form still reproduces that constant when fed the old card, so the v1.2.0 correction
    is provably to the DATA and not to the mathematics.
    """
    den = (a["w"] - a["r"]) - (b["w"] - b["r"])
    if abs(den) < 1e-15:
        return None
    return (a["w"] - b["w"]) / den


_A5 = {"r": 0.10, "w": 1.25}
assert abs(crossover(_A5, {"r": 0.25, "w": 1.00}) - 0.625) < 1e-12, \
    "closed form must reproduce the old hand-derived constant on the OLD (misread) card"
assert abs(crossover(_A5, {"r": 0.10, "w": 1.00}) - 1.0) < 1e-12, \
    "against a correctly-read no-premium card the crossover is h = 1"
assert crossover(_A5, {"r": 0.10, "w": 1.25}) is None, \
    "identical multiples are the same line and cannot cross"
assert abs(crossover({"r": 0.10, "w": 2.00}, _A5) - 1.0) < 1e-12, "argument order"
assert {tuple(sorted(c)) for c in CARDS} == {("label", "note", "r", "w", "windows")}, \
    "card shape drifted"
