-- HDG_HT_W_RecentActivity.lua  (C2)
-- Last 5 placed-decor events with relative timestamps.

local _, privateDB = ...
privateDB.HDG_HT = privateDB.HDG_HT or {}
privateDB.HDG_HT.Widgets = privateDB.HDG_HT.Widgets or {}
local W = { id = "recentActivity", title = "Recent Activity" }
privateDB.HDG_HT.Widgets.RecentActivity = W

local Patterns = privateDB.HDG_HT.Patterns

local function relTime(t)
    if not t then return "" end
    local age = time() - t
    if age < 60 then return string.format("%ds ago", age)
    elseif age < 3600 then return string.format("%dm ago", math.floor(age / 60))
    elseif age < 86400 then return string.format("%dh ago", math.floor(age / 3600))
    else return string.format("%dd ago", math.floor(age / 86400)) end
end

function W.Create(parent, dims)
    local self = {}
    self.pattern = Patterns.IconRowList(parent, dims, {
        rowHeight = dims.rowHeight or 28,
        iconSize  = dims.iconSize  or 22,
    })

    function self:Refresh(snap)
        local placed = snap and snap.recentPlaced or {}
        local rows = {}
        for i, e in ipairs(placed) do
            rows[i] = {
                icon  = e.icon,
                lineA = e.name or "?",
                lineB = e.source or "",        -- "Crafted" / "Learned" / etc.
                chip  = relTime(e.timestamp),
            }
        end
        self.pattern:Update({ rows = rows })
    end

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

return W
