-- HDG_HT_W_Records.lua  (D2)
-- Personal records pulled from HDG's existing logs + dailyXP delta tracker.
-- Renders via the StatLines pattern.

local _, privateDB = ...
privateDB.HDG_HT = privateDB.HDG_HT or {}
privateDB.HDG_HT.Widgets = privateDB.HDG_HT.Widgets or {}
local W = { id = "records", title = "Personal Records" }
privateDB.HDG_HT.Widgets.Records = W

local Patterns = privateDB.HDG_HT.Patterns
local R        = privateDB.HDG_HT.RecordsAggregator

local function fmt(n) return BreakUpLargeNumbers and BreakUpLargeNumbers(n) or tostring(n) end

function W.Create(parent, dims)
    local self = {}
    self.pattern = Patterns.StatLines(parent, dims, { rowHeight = 16 })

    function self:Refresh(_)
        if not R or not R.Build then self.pattern:Update(nil); return end
        local r = R.Build()

        local lines = {}
        if r.bestDay > 0 then
            local label = "Best XP day"
            if r.bestDate then
                local m, dy = r.bestDate:match("%d+-(%d+)-(%d+)")
                if m and dy then label = label .. "  (" .. m .. "/" .. dy .. ")" end
            end
            lines[#lines + 1] = { label = label, value = fmt(r.bestDay) }
        end
        if r.longestStreak > 0 then
            lines[#lines + 1] = { label = "Longest streak",  value = r.longestStreak .. " days" }
        end
        if r.houseAgeDays > 0 then
            lines[#lines + 1] = { label = "House age",       value = r.houseAgeDays .. " days" }
        end
        if r.totalPlaced > 0 then
            lines[#lines + 1] = { label = "Decor placed",    value = fmt(r.totalPlaced) }
        end
        if r.bestSession > 0 then
            lines[#lines + 1] = { label = "Best session",    value = fmt(r.bestSession) }
        end
        if r.totalCrafted > 0 then
            lines[#lines + 1] = { label = "Decor crafted",   value = fmt(r.totalCrafted) }
        end

        if #lines == 0 then
            lines[1] = { label = "Records", value = "no data yet" }
        end
        self.pattern:Update({ lines = lines })
    end

    function self:Resize(w, h) self.pattern:Resize(w, h) end
    return self
end

return W
