/**
 * Smoke test: universal creation-log projection end-to-end.
 * Run with: bun run scripts/smoke-rpast.ts
 */

import { initMarkwhen, enumerateCreations, renderMarkwhen } from '../shared/markwhen';
import { renderMarkwhenHtml } from '../shared/markwhen/html-render';
import { writeFileSync, mkdirSync } from 'node:fs';

const now = Date.now();
const d = (offsetDays: number) => now + offsetDays * 86_400_000;

const docs: Record<string, any> = {
	'demo:cal:events': {
		events: {
			'e1': { id: 'e1', title: 'TEC retrospective kickoff', startTime: d(-30), endTime: d(-30), createdAt: d(-31), updatedAt: d(-31), allDay: false, tags: ['research'], isVirtual: true, virtualUrl: 'https://meet.example' },
			'e2': { id: 'e2', title: 'Markwhen exploration', startTime: d(-14), endTime: d(-14), createdAt: d(-15), updatedAt: d(-15), allDay: false, bookingStatus: 'booked' },
			'e3': { id: 'e3', title: 'rPast ship review', startTime: d(3), endTime: d(3), createdAt: d(-1), updatedAt: d(-1), allDay: false },
		},
		sources: {
			's1': { id: 's1', name: 'Personal', createdAt: d(-90), updatedAt: d(-90) },
		},
		views: {},
	},
	'demo:rnotes:vaults:vault-a': {
		notes: {
			'daily/2026-04-10.md': { path: 'daily/2026-04-10.md', title: 'April 10 daily', tags: ['daily'], lastModifiedAt: d(-6) },
			'daily/2026-04-15.md': { path: 'daily/2026-04-15.md', title: 'April 15 daily', tags: ['daily'], lastModifiedAt: d(-1) },
			'ideas/markwhen.md': { path: 'ideas/markwhen.md', title: 'markwhen idea',  lastModifiedAt: d(-3), frontmatter: { date: '2026-04-13' } },
		},
	},
	'demo:rtasks:boards:main': {
		tasks: {
			't1': { id: 't1', title: 'Wire rPast bootstrap', status: 'done', priority: 'high', createdAt: d(-2), updatedAt: d(-1) },
			't2': { id: 't2', title: 'Build universal enumerator', status: 'done', priority: 'high', createdAt: d(-5), updatedAt: d(-2) },
			't3': { id: 't3', title: 'Add rVote adapter', status: 'todo', createdAt: d(-1) },
		},
	},
	'demo:rvote:proposals:round-1': {
		proposals: {
			'p1': { id: 'p1', title: 'Fund rPast v1', status: 'executed', createdAt: d(-20), updatedAt: d(-10) },
			'p2': { id: 'p2', title: 'Enable x402 export', status: 'open', createdAt: d(-2) },
		},
	},
};

initMarkwhen({
	async loadDoc<T>(id: string) { return (docs[id] as T) ?? null; },
	async listDocIds(prefix: string) { return Object.keys(docs).filter(k => k.startsWith(prefix)); },
});

const sources = await enumerateCreations('demo');
const projection = renderMarkwhen(sources, {
	view: 'timeline',
	title: 'demo — rPast (smoke)',
	baseUrl: 'https://demo.rspace.online',
});

console.log(`\n== Sources ==`);
for (const s of sources) console.log(`  ${s.id}: ${s.events.length} events`);
console.log(`\n== Total ==\n  ${projection.count} creations across ${sources.length} modules`);

console.log(`\n== .mw (first 40 lines) ==`);
console.log(projection.text.split('\n').slice(0, 40).join('\n'));

mkdirSync('output', { recursive: true });
writeFileSync('output/rpast-smoke.mw', projection.text);
writeFileSync('output/rpast-smoke.html', renderMarkwhenHtml(projection.text, 'timeline'));

console.log(`\n== Wrote ==`);
console.log(`  output/rpast-smoke.mw  (${projection.text.length} bytes)`);
console.log(`  output/rpast-smoke.html (${renderMarkwhenHtml(projection.text, 'timeline').length} bytes)`);
