package com.jsjdesigns.poker

import com.jsjdesigns.poker.bot.CoachingReview
import com.jsjdesigns.poker.game.Action
import com.jsjdesigns.poker.game.ActionType
import kotlin.math.roundToInt

data class CoachPresentation(val title: String, val detail: String)

/** Renders only audited DecisionTrace fields; it never reconstructs poker math. */
fun coachPresentation(review: CoachingReview): CoachPresentation {
    val trace = review.trace
    val title = if (review.alignedWithBaseline) "Baseline agrees" else "Worth reviewing"
    val baseline = trace.intended.coachLabel()

    val handStrengthPercentile = trace.handStrengthPercentile
    val detail = if (handStrengthPercentile != null) {
        val percentile = (handStrengthPercentile * 100).roundToInt()
        val range = (requireNotNull(trace.preflopRangeThreshold) * 100).roundToInt()
        "Starting-hand percentile $percentile%; baseline plays $range% here. Baseline: $baseline."
    } else {
        val equity = (requireNotNull(trace.estimatedEquity) * 100).roundToInt()
        if (trace.potOdds == "no bet to call") {
            "Estimated $equity% equity vs random hands; no bet to call. Baseline: $baseline."
        } else {
            val needed = (requireNotNull(trace.breakEvenEquity) * 100).roundToInt()
            "Estimated $equity% equity vs random hands; calling needs $needed%. Baseline: $baseline."
        }
    }
    return CoachPresentation(title, detail)
}

private fun Action.coachLabel(): String = when (type) {
    ActionType.FOLD -> "Fold"
    ActionType.CHECK -> "Check"
    ActionType.CALL -> "Call $amount"
    ActionType.BET -> "Bet $amount"
    ActionType.RAISE -> "Raise to $amount"
}
