package web
import (
"context"
"net/url"
"sourcecraft.dev/bigbes/sr-ht-dolt/beads"
"sourcecraft.dev/bigbes/sr-ht-dolt/browse"
"sourcecraft.dev/bigbes/sr-ht-dolt/core"
)
// memoryView renders the memories `bd remember` writes: config rows keyed
// kv.memory.<slug>, each with the revision its value was last written at. The
// row itself is (key, value) and carries no timestamp, so the date comes out of
// the history — see beads.BuildMemories for the walk that finds it.
//
// The reading is not here. It lives in the beads package, which the MCP
// surface's list_memories shares; this type is only the View adapter — slug,
// label, template, and the hand-off of the request's ref and query.
type memoryView struct{}
func (*memoryView) Name() string { return "memory" }
func (*memoryView) Label() string { return "Memory" }
func (*memoryView) Template() string { return "memory.html" }
// Applies is the beads fingerprint plus a config table carrying key and value;
// see beads.AppliesMemories. Applies sees table shapes and never rows, so a
// tracker that has never had a memory written into it still gets the tab and
// renders an empty state — the same contract Milestones has.
func (*memoryView) Applies(tables []browse.TableInfo) bool { return beads.AppliesMemories(tables) }
// Build reads the memories and dates them from the history. The result is a
// *beads.MemoryView, handed to memory.html as its .Data.
//
// The clock is passed in rather than read inside the projection: staleness is
// the one thing on this page that depends on when it was rendered, and timeNow
// is the same swappable clock the freshness line uses, so a test pins both at
// once.
func (*memoryView) Build(ctx context.Context, sess BrowseSession, _ *core.Repo, ref string, query url.Values) (any, error) {
data, err := beads.BuildMemories(ctx, sess, ref, query, timeNow())
if err != nil {
return nil, err
}
return data, nil
}