import Foundation
import PokerCore

/// One-off generator for `PokerAI/PreflopTable.swift`.
///
/// Measuring each starting hand's equity takes seconds, which is fine once and far too slow at
/// app launch. This prints the table as Swift source to be pasted in. Rerun it only if the
/// method changes; the numbers themselves are properties of poker and will not move.
func generatePreflopTable(trials: Int) {
    var rng = SeededRandom(seed: 20260730)

    struct Row {
        let hand: HandClass
        let headsUp: Double
        let multiway: Double
    }

    var rows: [Row] = []
    for hand in HandClass.all {
        let cards = hand.combinations()[0]
        let hu = EquityCalculator.equity(hero: cards, opponentCount: 1, trials: trials, rng: &rng).equity
        let mw = EquityCalculator.equity(hero: cards, opponentCount: 5, trials: trials, rng: &rng).equity
        rows.append(Row(hand: hand, headsUp: hu, multiway: mw))
        FileHandle.standardError.write("  \(hand) \(String(format: "%.3f %.3f", hu, mw))\n".data(using: .utf8)!)
    }

    print("// Generated by `pokerlab gen-preflop --trials \(trials)`. Do not edit by hand.")
    print("// Equity of each starting hand against that many random opponents, all cards run out.")
    print("let preflopEquityTable: [(String, Double, Double)] = [")
    for row in rows.sorted(by: { $0.headsUp > $1.headsUp }) {
        print(String(format: "    (\"%@\", %.4f, %.4f),", row.hand.description, row.headsUp, row.multiway))
    }
    print("]")
}
