-- Rack & Ruin: Cards.lua
-- Shared card texture lookup, deck building, and shuffling.

RackAndRuin = RackAndRuin or {}

local CARD_PATH = "Interface\\AddOns\\RackAndRuin\\Textures\\RackAndRuin\\"

RackAndRuin.RANKS = { "A","2","3","4","5","6","7","8","9","10","J","Q","K" }
RackAndRuin.SUITS = { "S","H","D","C" } -- Spades, Hearts, Diamonds, Clubs

RackAndRuin.SUIT_NAMES = {
    S = "Spades",
    H = "Hearts",
    D = "Diamonds",
    C = "Clubs",
}

-- Blackjack value lookup (Ace handled specially at scoring time)
RackAndRuin.BJ_VALUE = {
    A = 11, ["2"]=2, ["3"]=3, ["4"]=4, ["5"]=5, ["6"]=6, ["7"]=7,
    ["8"]=8, ["9"]=9, ["10"]=10, J=10, Q=10, K=10,
}

-- Poker rank value (for hand evaluation), Ace high by default
RackAndRuin.POKER_VALUE = {
    ["2"]=2, ["3"]=3, ["4"]=4, ["5"]=5, ["6"]=6, ["7"]=7, ["8"]=8,
    ["9"]=9, ["10"]=10, J=11, Q=12, K=13, A=14,
}

function RackAndRuin.GetCardTexture(rank, suit)
    if not rank or not suit then return nil end
    return CARD_PATH .. tostring(rank) .. tostring(suit) .. ".png"
end

function RackAndRuin.GetCardBackTexture()
    return CARD_PATH .. "CardBack.png"
end

function RackAndRuin.GetLogoTexture()
    return CARD_PATH .. "Logo.png"
end

function RackAndRuin.GetDartboardTexture()
    return CARD_PATH .. "DartBoard.png"
end

-- Builds a full 52-card deck: each entry = { rank=, suit=, texture= }
function RackAndRuin.BuildDeck()
    local deck = {}
    for _, suit in ipairs(RackAndRuin.SUITS) do
        for _, rank in ipairs(RackAndRuin.RANKS) do
            table.insert(deck, {
                rank = rank,
                suit = suit,
                texture = RackAndRuin.GetCardTexture(rank, suit),
            })
        end
    end
    return deck
end

-- Fisher-Yates shuffle
function RackAndRuin.Shuffle(deck)
    for i = #deck, 2, -1 do
        local j = math.random(i)
        deck[i], deck[j] = deck[j], deck[i]
    end
    return deck
end

-- Draws `count` cards off the top of deck (mutates deck), returns array of cards
function RackAndRuin.Draw(deck, count)
    local drawn = {}
    for i = 1, count do
        local card = table.remove(deck)
        if card then table.insert(drawn, card) end
    end
    return drawn
end
