import { x as head, c as escape_html, e as ensure_array_like, a as attr, d as attr_style, f as stringify } from './dev-BNpEUZI3.js';
import { C as Card, B as Badge } from './Tabs-DzG6Bn9a.js';
import { B as Button, E as EmptyState } from './ui-DR8zOSpC.js';
import { T as TierBadge, V as VerifyQueuePanel, a as PromotionCountdown } from './memory2-Cgc_4D5q.js';
import 'node:module';

//#region src/routes/brain/tier-stats/+page.svelte
function _page($$renderer, $$props) {
	$$renderer.component(($$renderer) => {
		let { data } = $$props;
		const initialStats = data.stats;
		const initialLoadedAt = data.loadedAt;
		let stats = initialStats;
		let loadedAt = initialLoadedAt;
		let loading = false;
		let error = null;
		function tableDisplayName(t) {
			return t.replace("brain_", "").replace(/_/g, " ");
		}
		function tableTotal(row) {
			return row.short + row.medium + row.long;
		}
		/** Stack bar percentages (three-segment). */
		function stackPct(row) {
			const total = tableTotal(row);
			if (total === 0) return {
				short: 0,
				medium: 0,
				long: 0
			};
			return {
				short: row.short / total * 100,
				medium: row.medium / total * 100,
				long: row.long / total * 100
			};
		}
		async function refresh() {
			loading = true;
			error = null;
			try {
				const res = await fetch("/api/memory/tier-stats");
				if (!res.ok) throw new Error(`HTTP ${res.status}`);
				stats = await res.json();
				loadedAt = (/* @__PURE__ */ new Date()).toISOString();
			} catch (e) {
				error = e instanceof Error ? e.message : "Failed to refresh tier stats";
			} finally {
				loading = false;
			}
		}
		function formatTime(iso) {
			try {
				return new Date(iso).toLocaleTimeString("en-US", { hour12: false });
			} catch {
				return iso.slice(11, 19);
			}
		}
		function tableRoute(tbl) {
			switch (tbl) {
				case "brain_observations": return "/brain/observations";
				case "brain_decisions": return "/brain/decisions";
				case "brain_patterns": return "/brain/patterns";
				case "brain_learnings": return "/brain/learnings";
				default: return "/brain";
			}
		}
		head("4h06go", $$renderer, ($$renderer) => {
			$$renderer.title(($$renderer) => {
				$$renderer.push(`<title>BRAIN Tier Stats — CLEO Studio</title>`);
			});
		});
		$$renderer.push(`<section class="tier-page svelte-4h06go"><header class="page-head svelte-4h06go"><div class="head-left svelte-4h06go"><a class="back svelte-4h06go" href="/brain/overview">← Overview</a> <h1 class="title svelte-4h06go">Tier Stats</h1> <span class="subtitle svelte-4h06go">Short · Medium · Long retention split across memory tables</span></div> <div class="head-right svelte-4h06go"><span class="updated svelte-4h06go">Updated ${escape_html(formatTime(loadedAt))}</span> `);
		Button($$renderer, {
			variant: "secondary",
			size: "sm",
			loading,
			onclick: () => {
				refresh();
			},
			children: ($$renderer) => {
				$$renderer.push(`<!---->Refresh`);
			}});
		$$renderer.push(`<!----></div></header> `);
		if (error) {
			$$renderer.push("<!--[0-->");
			{
				function action($$renderer) {
					Button($$renderer, {
						variant: "secondary",
						size: "sm",
						onclick: () => {
							refresh();
						},
						children: ($$renderer) => {
							$$renderer.push(`<!---->Retry`);
						}});
				}
				EmptyState($$renderer, {
					title: "Couldn't load tier stats",
					subtitle: error,
					variant: "warning",
					action});
			}
		} else if (!stats) {
			$$renderer.push("<!--[1-->");
			EmptyState($$renderer, {
				title: "brain.db not available",
				subtitle: "Start a CLEO session to populate memory — then come back for the split."
			});
		} else {
			$$renderer.push("<!--[-1-->");
			$$renderer.push(`<div class="legend svelte-4h06go"><span class="legend-title svelte-4h06go">Tiers</span> `);
			TierBadge($$renderer, { tier: "short" });
			$$renderer.push(`<!----> `);
			TierBadge($$renderer, { tier: "medium" });
			$$renderer.push(`<!----> `);
			TierBadge($$renderer, { tier: "long" });
			$$renderer.push(`<!----></div> <div class="grid svelte-4h06go"><!--[-->`);
			const each_array = ensure_array_like(stats.tables);
			for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
				let row = each_array[$$index];
				const total = tableTotal(row);
				const pct = stackPct(row);
				{
					function header($$renderer) {
						$$renderer.push(`<a class="card-head svelte-4h06go"${attr("href", tableRoute(row.table))}><span class="card-table svelte-4h06go">${escape_html(tableDisplayName(row.table))}</span> <span class="card-total svelte-4h06go"><span class="total-number svelte-4h06go">${escape_html(total)}</span> <span class="total-label svelte-4h06go">entries</span></span></a>`);
					}
					Card($$renderer, {
						header,
						children: ($$renderer) => {
							if (total === 0) {
								$$renderer.push("<!--[0-->");
								$$renderer.push(`<p class="empty-table svelte-4h06go">No entries in this table yet.</p>`);
							} else {
								$$renderer.push("<!--[-1-->");
								$$renderer.push(`<div class="stack svelte-4h06go" role="img"${attr("aria-label", `${tableDisplayName(row.table)} tier split`)}>`);
								if (pct.short > 0) {
									$$renderer.push("<!--[0-->");
									$$renderer.push(`<div class="seg seg-short svelte-4h06go"${attr_style(`width:${stringify(pct.short)}%`)}${attr("title", `short: ${row.short}`)}>`);
									if (pct.short >= 12) {
										$$renderer.push("<!--[0-->");
										$$renderer.push(`<span class="seg-label svelte-4h06go">${escape_html(row.short)}</span>`);
									} else $$renderer.push("<!--[-1-->");
									$$renderer.push(`<!--]--></div>`);
								} else $$renderer.push("<!--[-1-->");
								$$renderer.push(`<!--]--> `);
								if (pct.medium > 0) {
									$$renderer.push("<!--[0-->");
									$$renderer.push(`<div class="seg seg-medium svelte-4h06go"${attr_style(`width:${stringify(pct.medium)}%`)}${attr("title", `medium: ${row.medium}`)}>`);
									if (pct.medium >= 12) {
										$$renderer.push("<!--[0-->");
										$$renderer.push(`<span class="seg-label svelte-4h06go">${escape_html(row.medium)}</span>`);
									} else $$renderer.push("<!--[-1-->");
									$$renderer.push(`<!--]--></div>`);
								} else $$renderer.push("<!--[-1-->");
								$$renderer.push(`<!--]--> `);
								if (pct.long > 0) {
									$$renderer.push("<!--[0-->");
									$$renderer.push(`<div class="seg seg-long svelte-4h06go"${attr_style(`width:${stringify(pct.long)}%`)}${attr("title", `long: ${row.long}`)}>`);
									if (pct.long >= 12) {
										$$renderer.push("<!--[0-->");
										$$renderer.push(`<span class="seg-label svelte-4h06go">${escape_html(row.long)}</span>`);
									} else $$renderer.push("<!--[-1-->");
									$$renderer.push(`<!--]--></div>`);
								} else $$renderer.push("<!--[-1-->");
								$$renderer.push(`<!--]--></div> <dl class="kv svelte-4h06go"><div class="svelte-4h06go"><dt class="svelte-4h06go">`);
								TierBadge($$renderer, { tier: "short" });
								$$renderer.push(`<!----></dt> <dd class="svelte-4h06go">${escape_html(row.short)}</dd></div> <div class="svelte-4h06go"><dt class="svelte-4h06go">`);
								TierBadge($$renderer, { tier: "medium" });
								$$renderer.push(`<!----></dt> <dd class="svelte-4h06go">${escape_html(row.medium)}</dd></div> <div class="svelte-4h06go"><dt class="svelte-4h06go">`);
								TierBadge($$renderer, { tier: "long" });
								$$renderer.push(`<!----></dt> <dd class="svelte-4h06go">${escape_html(row.long)}</dd></div></dl>`);
							}
							$$renderer.push(`<!--]-->`);
						}});
				}
			}
			$$renderer.push(`<!--]--></div> `);
			{
				function header($$renderer) {
					$$renderer.push(`<div class="promo-head svelte-4h06go"><h2 class="promo-title svelte-4h06go">Upcoming long-tier promotions</h2> <span class="promo-sub svelte-4h06go">Medium entries closest to the 7-day citation gate</span></div>`);
				}
				Card($$renderer, {
					header,
					children: ($$renderer) => {
						if (stats.upcomingLongPromotions.length === 0) {
							$$renderer.push("<!--[0-->");
							EmptyState($$renderer, {
								title: "Nothing queued",
								subtitle: "Entries need ≥ 7 days age and citation ≥ 5 (or owner-verified) to promote."
							});
						} else {
							$$renderer.push("<!--[-1-->");
							$$renderer.push(`<ul class="promo-list svelte-4h06go"><!--[-->`);
							const each_array_1 = ensure_array_like(stats.upcomingLongPromotions);
							for (let $$index_1 = 0, $$length = each_array_1.length; $$index_1 < $$length; $$index_1++) {
								let promo = each_array_1[$$index_1];
								$$renderer.push(`<li class="promo-row svelte-4h06go"><a class="promo-id svelte-4h06go"${attr("href", tableRoute(promo.table))}><code class="svelte-4h06go">${escape_html(promo.id)}</code></a> <span class="promo-table-cell svelte-4h06go">${escape_html(tableDisplayName(promo.table))}</span> `);
								Badge($$renderer, {
									tone: "accent",
									size: "sm",
									subtle: true,
									children: ($$renderer) => {
										$$renderer.push(`<!---->${escape_html(promo.track)}`);
									}});
								$$renderer.push(`<!----> <div class="promo-end svelte-4h06go">`);
								PromotionCountdown($$renderer, { daysUntil: promo.daysUntil });
								$$renderer.push(`<!----></div></li>`);
							}
							$$renderer.push(`<!--]--></ul>`);
						}
						$$renderer.push(`<!--]-->`);
					}});
			}
			$$renderer.push(`<!----> `);
			VerifyQueuePanel($$renderer, { limit: 8 });
			$$renderer.push(`<!---->`);
		}
		$$renderer.push(`<!--]--></section>`);
	});
}

export { _page as default };
//# sourceMappingURL=_page.svelte-Byz2djto.js.map
