---@class AddonPrivate
local Private = select(2, ...)

---@class Commands
local commands = {}
Private.Commands = commands

local const = Private.constants

function commands:AddCommand(cmdName, cmdFunc)
    local addon = Private.Addon
    addon:RegisterChatCommand(cmdName, function(...) cmdFunc(self, ...) end)
end

function commands:InitCommands()
    self:AddCommand("drustminimap", self.DrustMinimap)
    self:AddCommand("drustui", self.DrustUI)
    self:AddCommand("drustdebug", self.DrustDebug)
end

function commands:DrustMinimap()
    local addon = Private.Addon
    addon.DB.minimap.hide = not addon.DB.minimap.hide
    if addon.DB.minimap.hide then
        Private.MinimapButton:GetIcon():Hide(const.ADDON_NAME)
        addon:Print("Minimap icon hidden.")
    else
        Private.MinimapButton:GetIcon():Show(const.ADDON_NAME)
        addon:Print("Minimap icon shown.")
    end
end

function commands:DrustUI()
    Private.UI:ToggleUI()
end

function commands:DrustDebug(input)
    local achievementID = tonumber(input)
    if not achievementID then
        print("Error: Please provide a valid achievement ID. Example: /drustdebug 41205")
        return
    end

    -- Function to search for the achievement ID in a given table
    local function FindAchievementByID(partsTable, id)
        if not partsTable or type(partsTable) ~= "table" then
            return nil
        end

        for _, achievement in ipairs(partsTable) do
            if achievement.achievementID == id then
                return achievement
            end
        end
        return nil
    end

    -- Search in DrustWaypointsPart1
    local foundInPart1 = FindAchievementByID(DrustWaypointsPart1, achievementID)
    if foundInPart1 then
        print(string.format("Achievement found in DrustWaypointsPart1: %s (ID: %d)", foundInPart1.name, foundInPart1.achievementID))
    else
        print("Achievement not found in DrustWaypointsPart1.")
    end

    -- Search in DrustWaypointsPart2
    local foundInPart2 = FindAchievementByID(DrustWaypointsPart2, achievementID)
    if foundInPart2 then
        print(string.format("Achievement found in DrustWaypointsPart2: %s (ID: %d)", foundInPart2.name, foundInPart2.achievementID))
    else
        print("Achievement not found in DrustWaypointsPart2.")
    end

    -- Combine results
    if not foundInPart1 and not foundInPart2 then
        print(string.format("Achievement ID %d not found in any Waypoints data.", achievementID))
    end
end