local addonName, TOAST = ...

-- One deliberately unadvertised home for beta previews and diagnostics.
-- Public slash commands stay focused on player-facing features; testers who
-- know /toast dev can still reach every internal surface without memorizing
-- a collection of temporary commands and globals.

local GOLD = { 0.88, 0.75, 0.36 }
local MUTED = { 0.65, 0.65, 0.65 }
local FRAME_W = 470
local PAD = 22
local BUTTON_W = 204
local BUTTON_H = 26
local COLUMN_GAP = 12
local ROW_GAP = 8

local frame

local function CreateSectionLabel(parent, text, y)
    local label = parent:CreateFontString(nil, "OVERLAY", "TOASTFontNormalSmall")
    label:SetPoint("TOPLEFT", parent, "TOPLEFT", PAD, y)
    label:SetText(text)
    label:SetTextColor(GOLD[1], GOLD[2], GOLD[3])
    return label
end

local function CreateDevButton(parent, text, column, y, action)
    local button = CreateFrame("Button", nil, parent, "UIPanelButtonTemplate")
    button:SetSize(BUTTON_W, BUTTON_H)
    button:SetPoint("TOPLEFT", parent, "TOPLEFT",
        PAD + (column - 1) * (BUTTON_W + COLUMN_GAP), y)
    button:SetText(text)
    button:SetScript("OnClick", action)
    return button
end

local function BuildDevHub()
    frame = CreateFrame("Frame", "TOASTDevHub", UIParent, "BackdropTemplate")
    frame:SetSize(FRAME_W, 330)
    frame:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
    frame:SetFrameStrata("DIALOG")
    frame:SetMovable(true)
    frame:SetClampedToScreen(true)
    frame:EnableMouse(true)
    frame:RegisterForDrag("LeftButton")
    frame:SetScript("OnDragStart", frame.StartMoving)
    frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
    frame:SetBackdrop({
        bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background-Dark",
        edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
        edgeSize = 26,
        insets = { left = 9, right = 9, top = 9, bottom = 9 },
    })
    frame:SetBackdropColor(0.03, 0.05, 0.07, 0.98)
    frame:SetBackdropBorderColor(GOLD[1], GOLD[2], GOLD[3], 0.72)
    frame:Hide()
    tinsert(UISpecialFrames, "TOASTDevHub")

    local title = frame:CreateFontString(nil, "OVERLAY", "TOASTFontNormalHuge")
    title:SetPoint("TOPLEFT", frame, "TOPLEFT", PAD, -18)
    title:SetText("TOAST DEV")
    title:SetTextColor(GOLD[1], GOLD[2], GOLD[3])

    local subtitle = frame:CreateFontString(nil, "OVERLAY", "TOASTFontHighlightSmall")
    subtitle:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -4)
    subtitle:SetText("Internal previews and diagnostics")
    subtitle:SetTextColor(MUTED[1], MUTED[2], MUTED[3])

    local divider = frame:CreateTexture(nil, "ARTWORK")
    divider:SetColorTexture(1, 1, 1, 0.12)
    divider:SetPoint("TOPLEFT", frame, "TOPLEFT", PAD, -65)
    divider:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -PAD, -65)
    divider:SetHeight(1)

    CreateSectionLabel(frame, "RELEASE PREVIEWS", -82)
    CreateDevButton(frame, "Weekly data notice", 1, -104, function()
        TOAST.ShowWeeklyDataUpdateNotice(
            "New weekly TOAST crew data is available. Update your addon through CurseForge to refresh the leaderboard.")
    end)
    CreateDevButton(frame, "Update required", 2, -104, function()
        TOAST.ShowReleaseScreen("required",
            "Preview: this TOAST version is no longer supported. Install the newest version.")
    end)
    CreateDevButton(frame, "New version detected", 1, -104 - BUTTON_H - ROW_GAP, function()
        TOAST.ShowReleaseScreen("version",
            "Installed version: v" .. TOAST.VERSION
                .. "\nAvailable version: vNEXT (preview)"
                .. "\n\nOpen CurseForge and update TOAST, then restart World of Warcraft or use /reload.")
    end)

    CreateSectionLabel(frame, "ONBOARDING", -174)
    CreateDevButton(frame, "Discord join screen", 1, -196, function()
        TOAST.ShowWelcome("discord")
    end)
    CreateDevButton(frame, "Community gate", 2, -196, function()
        TOAST.ShowCommunityGate()
    end)

    CreateSectionLabel(frame, "DIAGNOSTICS", -238)
    CreateDevButton(frame, "Comms debug", 1, -260, function()
        TOAST.ToggleCommsDebug()
    end)
    CreateDevButton(frame, "Comms trace", 2, -260, function()
        TOAST.ShowCommsTrace()
    end)
    CreateDevButton(frame, "Keystone HUD", 1, -260 - BUTTON_H - ROW_GAP, function()
        TOAST.ToggleKeysHUD()
    end)

    TOAST.CreateCloseButton(frame, function() frame:Hide() end)
end

TOAST.commands["dev"] = function()
    if not frame then BuildDevHub() end
    if frame:IsShown() then
        frame:Hide()
    else
        frame:Show()
        frame:Raise()
    end
end
