"""Reproduce the declared models. Run: python3 media/verify_math.py"""
from itertools import combinations
from collections import Counter
from math import comb
from pathlib import Path
import json

DECK=tuple((rank,suit) for rank in range(2,15) for suit in range(4))
EXPECTED={"Trail":52,"Pure sequence":48,"Sequence":720,"Colour":1096,"Pair":3744,"High card":16440}
NAMES={5:"Trail",4:"Pure sequence",3:"Sequence",2:"Colour",1:"Pair",0:"High card"}

def evaluate(hand,rule="a23-high"):
    assert rule in ("a23-high","akq-high")
    assert len(hand)==3 and len(set(hand))==3 and all(c in DECK for c in hand)
    ranks=sorted((rank for rank,_ in hand),reverse=True)
    counts=Counter(ranks)
    flush=len({suit for _,suit in hand})==1
    if ranks==[14,3,2]:run=15 if rule=="a23-high" else 14
    elif ranks==[14,13,12]:run=14 if rule=="a23-high" else 15
    elif len(counts)==3 and ranks[0]-ranks[-1]==2:run=ranks[0]
    else:run=0
    if len(counts)==1:return (5,ranks[0])
    if run and flush:return (4,run)
    if run:return (3,run)
    if flush:return (2,*ranks)
    if len(counts)==2:return (1,next(rank for rank,n in counts.items() if n==2),next(rank for rank,n in counts.items() if n==1))
    return (0,*ranks)

def main():
    observed={}
    for rule in ("a23-high","akq-high"):
        counts=Counter(NAMES[evaluate(hand,rule)[0]] for hand in combinations(DECK,3))
        assert dict(counts)==EXPECTED,(rule,counts)
        observed[rule]=dict(counts)
    hero=((12,0),(12,1),(5,2))
    remaining=tuple(c for c in DECK if c not in hero)
    conditional={}
    for rule in ("a23-high","akq-high"):
        value=evaluate(hero,rule)
        outcomes=Counter("win" if value>evaluate(h,rule) else "loss" if value<evaluate(h,rule) else "tie" for h in combinations(remaining,3))
        assert outcomes=={"win":16273,"loss":2148,"tie":3}
        conditional[rule]=dict(outcomes)
    wildcounts=Counter(sum(rank in (14,13,4,7) for rank,_ in h) for h in combinations(DECK,3))
    assert wildcounts=={0:7140,1:10080,2:4320,3:560}
    assert all(wildcounts[k]==comb(16,k)*comb(36,3-k) for k in range(4))
    # Comparison edges independent of category histogram.
    a23=((14,0),(2,1),(3,2));akq=((14,1),(13,2),(12,3))
    assert evaluate(a23)>evaluate(akq)
    assert evaluate(a23,"akq-high")<evaluate(akq,"akq-high")
    assert evaluate(((13,0),(14,1),(2,2)))[0]==0
    assert evaluate(((13,0),(14,0),(2,0)))[0]==2
    assert evaluate(hero)>evaluate(((11,0),(11,1),(14,2)))
    assert evaluate(hero)==evaluate(((12,2),(12,3),(5,3)))
    output={"verified_on":"2026-11-18","method":"Exhaustive unordered physical-hand enumeration; independent closed-form category and wildcard counts.","assumptions":{"deck":52,"hand_size":3,"jokers":False,"valid_runs":12,"wraparound_K_A_2":False,"ace_models":["A23 first; AKQ second","AKQ first; A23 second"],"QQ5_opponent":"One unconditioned uniformly random hand from 49 remaining cards","tie":"Ranking tie only. Show procedures are modelled separately."},"total_hands":comb(52,3),"expected_classic_counts":EXPECTED,"observed_classic_counts":observed,"QQ5":{"cards":["Q-spades","Q-hearts","5-clubs"],"opponent_hands":comb(49,3),"observed":conditional,"strict_win_probability":16273/18424,"split_tie_equity":(16273+1.5)/18424,"terminal_EV_P60_C20_requester_loses_ties":16273/18424*80-20},"AK47_wildcard_counts":dict(sorted(wildcounts.items())),"at_least_one_AK47_wild_probability":1-comb(36,3)/comb(52,3),"edge_assertions":"passed"}
    path=Path(__file__).with_name('calculation-checks.json')
    path.write_text(json.dumps(output,indent=2)+'\n')
    print(json.dumps(output,indent=2))

if __name__=='__main__':main()
