"""Exact synthetic response tables; Python 3.11+, standard library only.

Run from any folder. Rewrites only results.json and response-tables.csv beside
this script. No randomness, network, real player observations or card model.
"""
import csv
import json
from fractions import Fraction
from pathlib import Path

ROOT = Path(__file__).resolve().parent


def case(joint):
    cells = {'FF': joint, 'FC': 60-joint, 'CF': 60-joint, 'CC': joint-20}
    return {
        'both_fold_count': joint,
        'cells': cells,
        'first_fold': '3/5',
        'second_fold_overall': '3/5',
        'second_fold_given_first_fold': str(Fraction(joint, 60)),
        'both_fold': str(Fraction(joint, 100)),
        'bet_ev_chips': str(Fraction(joint*100 - (100-joint)*50, 100)),
    }


def main():
    result = {
        'model': {
            'synthetic': True, 'units_per_table': 100,
            'pot_before_bet_chips': 100, 'hero_river_all_in_chips': 50,
            'defenders': 2, 'response_order': ['A', 'B'],
            'responses': ['F', 'C'], 'hero_showdown_payout_when_called': 0,
            'rake_and_fees': 0,
            'assumptions': 'Both defenders cover Hero. No raises or future streets. Cells are probability mass units, not observed hands. Prior contributions are sunk.',
        },
        'featured': [case(j) for j in (20, 36, 60)],
        'sensitivity': [case(j) for j in range(20, 61)],
    }
    (ROOT/'results.json').write_text(json.dumps(result, indent=2)+'\n', encoding='utf-8', newline='\n')
    with (ROOT/'response-tables.csv').open('w', encoding='utf-8', newline='') as stream:
        writer = csv.writer(stream, lineterminator='\n')
        writer.writerow(['joint_count', 'A_response', 'B_response', 'mass_of_100'])
        for row in result['sensitivity']:
            for pair, mass in row['cells'].items():
                writer.writerow([row['both_fold_count'], pair[0], pair[1], mass])
    print('Generated three featured tables, 41 sensitivity tables and 164 CSV rows.')


if __name__ == '__main__':
    main()
