-- ApiVault Secure / Taint workflow panel for AppShell. Added in v1.3.1.
local addonName, ApiVault = ...

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

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

local function createBackdropFrame(name, parent)
    return CreateFrame("Frame", name, parent, BackdropTemplateMixin and "BackdropTemplate" or nil)
end

local function setBackdrop(frame)
    if not frame.SetBackdrop then return end
    frame: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 },
    })
    frame:SetBackdropColor(0.02, 0.02, 0.03, 0.86)
end

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

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

function Panel:SetOutput(text)
    if self.output then
        if ApiVault.CapOutputText then text = ApiVault:CapOutputText(text) end
        self.output:SetText(tostring(text or ""))
        self.output:SetCursorPosition(0)
    end
end

function Panel:Analyze()
    local text = self.input and self.input:GetText() or ""
    if text == "" then
        self:SetOutput("Paste code or an error stack that may contain secure/taint-sensitive patterns, then click Analyze.\n\nExamples:\nSetAttribute\nRegisterStateDriver\nSecureActionButtonTemplate\nInCombatLockdown")
        return
    end
    if ApiVault.TaintCombatSafetyAssistant and ApiVault.TaintCombatSafetyAssistant.FormatReport then
        self:SetOutput(ApiVault.TaintCombatSafetyAssistant:FormatReport(text))
    else
        self:SetOutput("Taint / Combat Safety Assistant unavailable.")
    end
end

function Panel:OpenSecurePatterns()
    if ApiVault.SecurePatternBrowser and ApiVault.SecurePatternBrowser.Open then ApiVault.SecurePatternBrowser:Open()
    elseif ApiVault.SecurePatternBrowser and ApiVault.SecurePatternBrowser.Toggle then ApiVault.SecurePatternBrowser:Toggle()
    else self:SetOutput("Secure Pattern Browser unavailable.") end
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("Secure / Taint")

    local sub = self:Add(parent, createText(parent, "GameFontDisable"))
    sub:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -8)
    sub:SetPoint("RIGHT", parent, "RIGHT", -20, 0)
    sub:SetText("Analyze secure/taint-sensitive patterns and generate OOC/combat review steps. Inspect-only; no protected execution.")

    local input = self:Add(parent, CreateFrame("EditBox", nil, parent, "InputBoxTemplate"))
    self.input = input
    input:SetSize(420, 24)
    input:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, -78)
    input:SetAutoFocus(false)
    input:SetScript("OnEnterPressed", function(box)
        box:ClearFocus()
        Panel:Analyze()
    end)

    local analyze = self:Add(parent, CreateFrame("Button", nil, parent, "UIPanelButtonTemplate"))
    analyze:SetSize(82, 22)
    analyze:SetPoint("LEFT", input, "RIGHT", 10, 0)
    analyze:SetText("Analyze")
    analyze:SetScript("OnClick", function() Panel:Analyze() end)

    local patterns = self:Add(parent, CreateFrame("Button", nil, parent, "UIPanelButtonTemplate"))
    patterns:SetSize(142, 22)
    patterns:SetPoint("LEFT", analyze, "RIGHT", 6, 0)
    patterns:SetText("Secure Patterns")
    patterns:SetScript("OnClick", function() Panel:OpenSecurePatterns() end)

    local bg = self:Add(parent, createBackdropFrame(nil, parent))
    bg:SetPoint("TOPLEFT", parent, "TOPLEFT", 20, -118)
    bg:SetPoint("BOTTOMRIGHT", parent, "BOTTOMRIGHT", -20, 20)
    setBackdrop(bg)

    local scroll = self:Add(parent, CreateFrame("ScrollFrame", nil, bg, "UIPanelScrollFrameTemplate"))
    scroll:SetPoint("TOPLEFT", bg, "TOPLEFT", 12, -12)
    scroll:SetPoint("BOTTOMRIGHT", bg, "BOTTOMRIGHT", -30, 12)

    local output = self:Add(parent, CreateFrame("EditBox", nil, scroll))
    self.output = output
    output:SetMultiLine(true)
    output:SetAutoFocus(false)
    output:SetFontObject(ChatFontNormal)
    output:SetWidth(720)
    output:SetText("Paste code or an error stack and click Analyze.\n\nApiVault will generate a risk summary and OOC/combat checklist.\n\nPolicy: inspect-only; no secure/protected execution.")
    output:SetScript("OnEscapePressed", function(box) box:ClearFocus() end)
    scroll:SetScrollChild(output)

    parent:SetScript("OnSizeChanged", function()
        if output and bg then output:SetWidth(math.max(520, bg:GetWidth() - 50)) end
    end)
end
