local MonkeyGames = LibStub("AceAddon-3.0"):GetAddon("MonkeyGames")
local Blackjack = MonkeyGames.Blackjack

local BlackjackAI = {}
MonkeyGames.BlackjackAI = BlackjackAI

-- Blackjack "AI" is just the standard dealer rules.
-- The dealer always follows fixed rules: hit on 16 or less, stand on 17+.
-- This module exists for GameRegistry compatibility.
-- In multiplayer mode, the dealer is always automated.

--- For Blackjack, AI doesn't take "turns" in the traditional sense.
-- The dealer plays automatically after the player stands.
-- This GetBestMove is called if we need the AI to act as a player (multiplayer).
function BlackjackAI:GetBestMove(state, difficulty)
    -- Basic strategy hint for multiplayer opponent simulation
    -- (In single player, dealer logic is handled directly in Blackjack:PlayDealer)
    if state.phase == "player_turn" then
        local hand = state.playerHands[state.activeHandIndex]
        local value = Blackjack:HandValue(hand)

        -- Simple strategy based on difficulty
        if difficulty == 1 then
            -- Easy: random play
            if math.random() < 0.5 and value < 19 then
                return { action = "hit" }
            else
                return { action = "stand" }
            end
        elseif difficulty == 2 then
            -- Medium: basic threshold
            if value < 17 then
                return { action = "hit" }
            else
                return { action = "stand" }
            end
        else
            -- Hard: basic strategy (simplified)
            local dealerUpCard = state.dealerHand[1]
            local dealerValue = Blackjack.RANK_VALUES and 10 or 10

            if value <= 11 then
                return { action = "hit" }
            elseif value == 12 then
                return (dealerUpCard.rank >= "4" and dealerUpCard.rank <= "6") and { action = "stand" } or { action = "hit" }
            elseif value <= 16 then
                local dv = ({ ["2"]=2,["3"]=3,["4"]=4,["5"]=5,["6"]=6,["7"]=7,["8"]=8,["9"]=9,["10"]=10,["J"]=10,["Q"]=10,["K"]=10,["A"]=11 })[dealerUpCard.rank] or 10
                if dv >= 7 then
                    return { action = "hit" }
                else
                    return { action = "stand" }
                end
            else
                return { action = "stand" }
            end
        end
    elseif state.phase == "betting" then
        return { action = "deal" }
    elseif state.phase == "insurance" then
        return { action = "insurance_no" }
    end

    return nil
end
