/**
 * rPast Automerge schemas — chronicle-of-self viewer.
 *
 * DocId format: {space}:rpast:chronicles
 *
 * A chronicle is a saved configuration over the universal creation log:
 *   - which modules to include (defaults: all registered)
 *   - date window
 *   - record-type filter
 *   - tag filter
 *   - view mode
 *
 * The projected `.mw` text is NEVER persisted — it is re-derived from
 * canonical CRDT state every open. This guarantees rPast can never drift
 * from source-of-truth.
 */

import type { DocSchema } from '../../shared/local-first/document';

export interface Chronicle {
	id: string;
	name: string;
	icon: string | null;
	/** Module ids included, in render-order (becomes section order). */
	modules: string[];
	/** Optional record-type narrowing (e.g. only "note" + "event"). */
	recordTypes: string[];
	/** Tag allow-list (match-any). Empty = no tag filter. */
	tags: string[];
	/** Unix ms; null = unbounded. */
	fromMs: number | null;
	toMs: number | null;
	viewMode: 'timeline' | 'calendar' | 'gantt';
	createdAt: number;
	updatedAt: number;
}

export interface PastDoc {
	meta: {
		module: string;
		collection: string;
		version: number;
		spaceSlug: string;
		createdAt: number;
	};
	chronicles: Record<string, Chronicle>;
}

export const pastSchema: DocSchema<PastDoc> = {
	module: 'rpast',
	collection: 'chronicles',
	version: 1,
	init: (): PastDoc => ({
		meta: {
			module: 'rpast',
			collection: 'chronicles',
			version: 1,
			spaceSlug: '',
			createdAt: Date.now(),
		},
		chronicles: {},
	}),
	migrate: (doc: PastDoc, _fromVersion: number): PastDoc => doc,
};

export function pastDocId(space: string) {
	return `${space}:rpast:chronicles` as const;
}
