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

local GameRegistry = {}
MonkeyGames.GameRegistry = GameRegistry

GameRegistry.games = {}

--- Register a new game module.
-- gameData fields:
--   name            (string)   Display name
--   dbKey           (string)   Unique key for stats/settings
--   description     (string)   Description shown in browser
--   icon            (string)   Icon texture path
--   boardSize       (table)    { rows = N, cols = N }
--   minPlayers      (number)   Minimum players
--   maxPlayers      (number)   Maximum players
--   createGameFunc  (function) Returns a new game state table
--   validateMoveFunc(function) (state, move) -> boolean
--   getLegalMovesFunc(function)(state, row, col) -> moves table
--   getAllMovesFunc  (function) (state) -> all legal moves
--   applyMoveFunc   (function) (state, move) -> applies move in place
--   getGameStatusFunc(function)(state) -> "playing"|"checkmate"|"stalemate"|"draw_*"
--   getAIFunc       (function) Returns the AI module
--   createUIFunc    (function) (parent, gameState) -> creates game UI
--   copyStateFunc   (function) (state) -> deep copy of state
function GameRegistry:AddGame(gameData)
    if not gameData.name or not gameData.dbKey then
        MonkeyGames:ErrorPrint("GameRegistry: game must have name and dbKey")
        return
    end
    table.insert(self.games, gameData)
end

function GameRegistry:GetGame(name)
    for _, game in ipairs(self.games) do
        if game.name == name then
            return game
        end
    end
    return nil
end

function GameRegistry:GetGameByKey(dbKey)
    for _, game in ipairs(self.games) do
        if game.dbKey == dbKey then
            return game
        end
    end
    return nil
end

function GameRegistry:GetAllGames()
    return self.games
end
