package com.jsjdesigns.poker

internal const val MAX_PLAYER_NAME_LENGTH = 16
internal const val DEFAULT_BUY_IN = 200
internal const val DEFAULT_SMALL_BLIND = 1
internal const val DEFAULT_BIG_BLIND = 2

data class CashGameConfig(
    val playerName: String,
    val coachEnabled: Boolean = false,
    val buyIn: Int = DEFAULT_BUY_IN,
    val smallBlind: Int = DEFAULT_SMALL_BLIND,
    val bigBlind: Int = DEFAULT_BIG_BLIND,
)

/**
 * Normalises player input once at the session boundary.
 *
 * "You" remains the safe default, but a supplied name is trimmed, internal
 * whitespace is collapsed, and the table-sized label is bounded.
 */
fun cashGameConfig(
    playerName: String,
    coachEnabled: Boolean = false,
): CashGameConfig {
    val normalised = playerName
        .trim()
        .replace(Regex("\\s+"), " ")
        .take(MAX_PLAYER_NAME_LENGTH)
        .ifBlank { "You" }
    return CashGameConfig(
        playerName = normalised,
        coachEnabled = coachEnabled,
    )
}

/**
 * Cash-game bots may top themselves up automatically; the human never does.
 * Returning indices keeps the policy pure and independently testable.
 */
fun automaticTopUpSeatIndices(
    stacks: List<Int>,
    heroSeat: Int,
    bigBlind: Int,
): List<Int> {
    require(heroSeat in stacks.indices) { "hero seat is outside the table" }
    require(bigBlind > 0) { "big blind must be positive" }
    return stacks.indices.filter { it != heroSeat && stacks[it] < bigBlind }
}

fun continuationButtonLabel(
    nextHandRequested: Boolean,
    rebuyRequired: Boolean,
    buyIn: Int,
): String = when {
    nextHandRequested -> "Dealing…"
    rebuyRequired -> "Reload to $buyIn & deal"
    else -> "Next hand"
}
