local addonName, ns = ...

------------------------------------------------------------
-- What's New tab: the most recent release-notes entries, rendered as
-- scrollable rich text (gold version/date headings, white bullets).
-- Sourced from ns.releaseNotes (src/ReleaseNotes.lua).
------------------------------------------------------------

ns.WHATSNEW_LIMIT = 5

local CONTENT_W = 550
local ADDON_SITE = "housing-herald.com"

local panel = ns.whatsnewPanel

local scroll = CreateFrame("ScrollFrame", nil, panel, "UIPanelScrollFrameTemplate")
scroll:SetPoint("TOPLEFT", 0, 0)
scroll:SetPoint("BOTTOMRIGHT", -24, 24)

local content = CreateFrame("Frame", nil, scroll)
content:SetSize(CONTENT_W, 10)
scroll:SetScrollChild(content)

local text = content:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
text:SetPoint("TOPLEFT", 0, 0)
text:SetWidth(CONTENT_W)
text:SetJustifyH("LEFT")
text:SetWordWrap(true)
ns._whatsnewText = text

local footer = panel:CreateFontString(nil, "OVERLAY", "GameFontDisableSmall")
footer:SetPoint("BOTTOM", 0, 6)
footer:SetTextColor(ns.Theme.TEXT_MUTED:GetRGBA())
footer:SetText("Older versions on " .. ADDON_SITE)
ns._whatsnewFooter = footer

local function buildText()
    local notes = ns.releaseNotes
    if not notes or #notes == 0 then
        return ns.Colorize(ns.Theme.TEXT_MUTED, "No release notes yet.")
    end

    local lines = {}
    local limit = math.min(ns.WHATSNEW_LIMIT, #notes)
    for i = 1, limit do
        local entry = notes[i]
        local heading = string.format("%s  (%s)", entry.version, entry.date or "")
        lines[#lines + 1] = ns.Colorize(ns.Theme.HEADING, heading)
        for _, change in ipairs(entry.changes) do
            lines[#lines + 1] = "- " .. change
        end
        if i < limit then lines[#lines + 1] = "" end
    end
    return table.concat(lines, "\n")
end

function ns:RefreshWhatsNewTab()
    text:SetText(buildText())
    content:SetHeight(text:GetStringHeight() + 8)
end
