-- ApiVault Home panel for AppShell. Added in v1.3.0.
local addonName, ApiVault = ...

ApiVault.HomePanel = ApiVault.HomePanel or {}
local Panel = ApiVault.HomePanel

local function createText(parent, template)
    local fs = parent:CreateFontString(nil, "OVERLAY", template or "GameFontNormal")
    fs:SetJustifyH("LEFT")
    fs:SetJustifyV("TOP")
    return fs
end

function Panel:Clear(parent)
    if parent.ApiVaultHomeChildren then
        for _, child in ipairs(parent.ApiVaultHomeChildren) do
            if child.Hide then child:Hide() end
            if child.SetParent then child:SetParent(nil) end
        end
    end
    parent.ApiVaultHomeChildren = {}
end

function Panel:Add(parent, child)
    parent.ApiVaultHomeChildren = parent.ApiVaultHomeChildren or {}
    table.insert(parent.ApiVaultHomeChildren, child)
    return child
end

function Panel:CreateCard(parent, title, body, x, y, w, h, onClick)
    local card = self:Add(parent, CreateFrame("Button", nil, parent, BackdropTemplateMixin and "BackdropTemplate" or nil))
    card:SetSize(w, h)
    card:SetPoint("TOPLEFT", parent, "TOPLEFT", x, y)
    if card.SetBackdrop then
        card:SetBackdrop({
            bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
            edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
            tile = true,
            tileSize = 16,
            edgeSize = 12,
            insets = { left = 4, right = 4, top = 4, bottom = 4 },
        })
        card:SetBackdropColor(0.04, 0.04, 0.06, 0.88)
    end
    card.title = createText(card, "GameFontHighlight")
    card.title:SetPoint("TOPLEFT", card, "TOPLEFT", 12, -10)
    card.title:SetPoint("RIGHT", card, "RIGHT", -12, 0)
    card.title:SetText(title)
    card.body = createText(card, "GameFontDisableSmall")
    card.body:SetPoint("TOPLEFT", card.title, "BOTTOMLEFT", 0, -8)
    card.body:SetPoint("BOTTOMRIGHT", card, "BOTTOMRIGHT", -12, 12)
    card.body:SetText(body)
    card:SetScript("OnClick", onClick)
    return card
end

function Panel:Render(parent)
    self:Clear(parent)
    local title = self:Add(parent, createText(parent, "GameFontHighlightLarge"))
    title:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, -18)
    title:SetText("ApiVault Developer Cockpit")

    local subtitle = self:Add(parent, createText(parent, "GameFontDisable"))
    subtitle:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -8)
    subtitle:SetPoint("RIGHT", parent, "RIGHT", -20, 0)
    subtitle:SetText("Choose a task. ApiVault will show the correct workflow instead of making one input box mean everything.")

    local w, h = 245, 112
    local y1 = -86
    local y2 = -214
    local y3 = -342

    self:CreateCard(parent, "Inspect Addon", "Find loaded addons, ApiVault integration, SavedVariables, errors and request reports.", 20, y1, w, h, function()
        ApiVault.AppShell:Open("addons")
    end)
    self:CreateCard(parent, "Explain Error", "Open Error Inbox and Bug Advisor. Deduplicate errors, explain likely causes and copy reports.", 285, y1, w, h, function()
        ApiVault.AppShell:Open("errors")
    end)
    self:CreateCard(parent, "Search API", "Open the cockpit API workflow for APIs, source symbols, runtime globals and catalogs.", 550, y1, w, h, function()
        ApiVault.AppShell:Open("api")
    end)

    self:CreateCard(parent, "Check SavedVariables", "Open Audit V2 for SavedVariables runtime probe and checklist workflows.", 20, y2, w, h, function()
        ApiVault.AppShell:Open("audit")
    end)
    self:CreateCard(parent, "Check Taint Risk", "Analyze secure/taint-sensitive code and generate OOC/combat test steps.", 285, y2, w, h, function()
        ApiVault.AppShell:Open("secure")
    end)
    self:CreateCard(parent, "Audit Snippet", "Paste Lua/XML/TOC snippets and generate API, performance, package and taint checks.", 550, y2, w, h, function()
        ApiVault.AppShell:Open("audit")
    end)

    self:CreateCard(parent, "Event Lab", "Record selected event payloads manually through Test Lab V2. No auto-register-all behavior.", 20, y3, w, h, function()
        ApiVault.AppShell:Open("testlab")
    end)
    self:CreateCard(parent, "Export Report", "Create complete developer session export and copyable reports.", 285, y3, w, h, function()
        ApiVault.AppShell:Open("reports")
    end)
    self:CreateCard(parent, "Read Documentation", "Search the manual, browse sections and use the Which Tool? workflow guide.", 550, y3, w, h, function()
        ApiVault.AppShell:Open("docs")
    end)

    local y4 = -470
    self:CreateCard(parent, "Open Settings", "Configure compact mode, output limits, default start page, bridge preferences and window reset.", 20, y4, w, h, function()
        ApiVault.AppShell:Open("settings")
    end)
end
