local addonName, SinsToolkit = ...

SinsToolkit.UI = SinsToolkit.UI or {}
SinsToolkit.UI.Pages = SinsToolkit.UI.Pages or {}

local PageBase = SinsToolkit.UI.Pages.Base or {}
local Helpers = SinsToolkit.UI.Pages.Helpers or {}
local Page = {}
setmetatable(Page, { __index = PageBase })
SinsToolkit.UI.Pages.DeveloperTools = Page
Page.density = Helpers.GetDefaultDensity and Helpers.GetDefaultDensity() or "compact"

function Page:Create(CreateScrollablePage, Window, CreateWrappedText, SetFlatBackdrop)
    local Theme = Helpers.GetTheme and Helpers.GetTheme()
    local devToolsPage, content = CreateScrollablePage("Developer Tools")
    Helpers.CreatePageIntro(content, Window, "Developer Tools", "A shared workspace for inspection, diagnostics, and runtime debugging tools. This MVP links existing developer-adjacent tools while preserving their current pages and slash commands.", CreateWrappedText, 720)

    local devCards = {}
    local function CreateDeveloperToolCard(index, tool)
        local col = (index - 1) % 2
        local row = math.floor((index - 1) / 2)
        local x = col * 335
        local y = -92 - (row * 142)
        local card = Helpers.CreatePageCard(content, tool.name or tool.id or "Developer Tool", 310, 124)

        card:SetPoint("TOPLEFT", content, "TOPLEFT", x, y)
        Helpers.SetCardDescription(card, tool.description or "")

        local rows = Helpers.CreatePropertyRows(card, {
            { label = "Status", value = "|cff00ff00" .. (tool.status or "available") .. "|r" },
            { label = "Command", value = tool.slashCommand and ("|cffd8a84a" .. tool.slashCommand .. "|r") or "Page action" },
        }, { x = 14, y = -74, labelWidth = 70, valueWidth = 126, rowHeight = 18 })

        local button = Window:CreateButton(card, tool.page and "Open Page" or "Open", 98, 24, function()
            if tool.open then
                tool.open()
            elseif tool.page and Window.SelectPage then
                Window:SelectPage(tool.page)
            elseif tool.slashCommand then
                print("|cff9b59b6[Sin's Toolkit: QOL]|r Use " .. tool.slashCommand .. " to open " .. (tool.name or "this tool") .. ".")
            end
        end, "Open " .. (tool.name or "developer tool") .. ".")
        button:SetPoint("BOTTOMRIGHT", -14, 10)

        devCards[#devCards + 1] = card
        return card
    end

    devToolsPage.OnShowCallback = function()
        for _, card in ipairs(devCards) do
            card:Show()
        end

        if #devCards == 0 then
            local tools = SinsToolkit.DevTools and SinsToolkit.DevTools.GetTools and SinsToolkit.DevTools:GetTools() or {}
            for index, tool in ipairs(tools) do
                CreateDeveloperToolCard(index, tool)
            end
        end

        if #devCards == 0 then
            if not devToolsPage.emptyText then
                devToolsPage.emptyText = content:CreateFontString(nil, "OVERLAY", Theme and Theme.GetFont and Theme.GetFont("body") or "GameFontHighlight")
                devToolsPage.emptyText:SetPoint("TOPLEFT", content, "TOPLEFT", 0, -150)
                devToolsPage.emptyText:SetWidth(620)
                devToolsPage.emptyText:SetJustifyH("LEFT")
                if Theme and Theme.ApplyBodyFont then Theme.ApplyBodyFont(devToolsPage.emptyText) end
            end
            devToolsPage.emptyText:SetText("No Developer Tools are registered yet.")
            devToolsPage.emptyText:Show()
        elseif devToolsPage.emptyText then
            devToolsPage.emptyText:Hide()
        end

        local rows = math.ceil(math.max(1, #devCards) / 2)
        content:SetHeight(math.max(360, 120 + (rows * 142)))
    end

    self.frame = devToolsPage
    return devToolsPage
end

Page:Register("Developer Tools")

return Page
