#!/usr/bin/env python3
"""Verify the two published examples. Python 3.9+, standard library only.

This checks these literal teaching cases, not arbitrary poker histories.
Run beside examples.json. Does not modify files or process personal records.
"""
import json
from pathlib import Path


def require(condition, message):
    if not condition:
        raise ValueError(message)


def verify(data):
    chip = data['chip_case']
    require(chip['starting'] == {'BTN': 200, 'SB': 300, 'BB': 260}, 'Starting fixture changed')
    require(chip['blinds'] == [1, 2], 'Blinds changed')
    require(all(chip[k] == 0 for k in ['antes', 'straddles', 'deductions']), 'Fixture exclusions changed')
    require(chip['preflop_street_totals'] == {'BTN': 6, 'SB': 1, 'BB': 6}, 'Preflop fixture changed')
    require(chip['flop'] == ['Ks', '7d', '2c'] and chip['btn_flop_bet'] == 4, 'Flop fixture changed')
    require(len(chip['branches']) == 2, 'Expected two chip branches')
    literal = [(16, 33, 12, 238), (20, 37, 16, 234)]
    for branch, (total, pot, call, bb_behind) in zip(chip['branches'], literal):
        require((branch['bb_flop_total'], branch['pot'], branch['call'], branch['behind']['BB']) ==
                (total, pot, call, bb_behind), 'Literal independent ledger mismatch')
        contributions = dict(chip['preflop_street_totals'])
        contributions['BTN'] += chip['btn_flop_bet']
        contributions['BB'] += total
        derived_behind = {p: chip['starting'][p] - contributions[p] for p in contributions}
        require(derived_behind == branch['behind'], 'Stack ledger mismatch')
        require(all(v >= 0 for v in derived_behind.values()), 'Unaffordable action')
        require(total - chip['btn_flop_bet'] >= chip['btn_flop_bet'], 'Not a full raise')
        require(sum(contributions.values()) == pot, 'Pot ledger mismatch')
        require(total - chip['btn_flop_bet'] == call, 'Call price mismatch')
        require(pot + sum(derived_behind.values()) == 760, 'Chip conservation mismatch')
    suit = data['suit_case']
    require(suit['hero'] == ['As', 'Qd'] and suit['flop'] == ['Ks', '9s', '4d'], 'Suit fixture changed')
    require([b['turn'] for b in suit['branches']] == ['2s', '2h'], 'Turn alternatives changed')
    deck = [rank + suit_code for rank in '23456789TJQKA' for suit_code in 'cdhs']
    for branch, expected_count in zip(suit['branches'], [9, 0]):
        known = suit['hero'] + suit['flop'] + [branch['turn']]
        require(len(set(known)) == 6 and all(c in deck for c in known), 'Illegal known cards')
        rivers = [c for c in deck if c not in known]
        require(len(rivers) == 46, 'Expected 46 candidate river cards')
        completions = [r for r in rivers if any(sum(c[1] == code for c in known + [r]) >= 5 for code in 'cdhs')]
        require(completions == branch['river_cards_completing_hero_flush'], 'Flush-completion list mismatch')
        require(len(completions) == expected_count, 'Independent suit-count mismatch')
    return 'PASS: two chip branches conserve 760; 92 single-river checks give 9 and 0 flush completions.'


if __name__ == '__main__':
    print(verify(json.loads(Path(__file__).with_name('examples.json').read_text(encoding='utf-8'))))
