package com.jsjdesigns.poker.core

import kotlin.random.Random

/**
 * A shuffled 52-card deck. Seedable via [random] so any hand the bots misplay can
 * be replayed exactly — which matters a lot when tuning profiles.
 */
class Deck(private val random: Random = Random.Default) : CardSource {

    private val cards = IntArray(Card.DECK_SIZE) { it }
    private var next = 0

    val remaining: Int get() = Card.DECK_SIZE - next

    override fun shuffle() {
        for (i in cards.indices) cards[i] = i
        for (i in Card.DECK_SIZE - 1 downTo 1) {
            val j = random.nextInt(i + 1)
            val tmp = cards[i]; cards[i] = cards[j]; cards[j] = tmp
        }
        next = 0
    }

    override fun deal(): Int {
        check(next < Card.DECK_SIZE) { "deck exhausted" }
        return cards[next++]
    }

    override fun deal(count: Int): IntArray = IntArray(count) { deal() }
}
