/**
 * rBooks Automerge document schemas.
 *
 * Granularity: one Automerge document per space (all books together).
 * DocId format: {space}:books:catalog
 *
 * PDF files stay on the filesystem — only metadata migrates.
 */

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

// ── Document types ──

export interface BookItem {
	id: string;
	slug: string;
	title: string;
	author: string;
	description: string;
	pdfPath: string;
	pdfSizeBytes: number;
	pageCount: number;
	tags: string[];
	license: string | null;
	coverColor: string | null;
	contributorId: string | null;
	contributorName: string | null;
	status: string;
	featured: boolean;
	viewCount: number;
	downloadCount: number;
	createdAt: number;
	updatedAt: number;
}

export interface BooksCatalogDoc {
	meta: {
		module: string;
		collection: string;
		version: number;
		spaceSlug: string;
		createdAt: number;
	};
	items: Record<string, BookItem>;
}

// ── Schema registration ──

export const booksCatalogSchema: DocSchema<BooksCatalogDoc> = {
	module: 'books',
	collection: 'catalog',
	version: 1,
	init: (): BooksCatalogDoc => ({
		meta: {
			module: 'books',
			collection: 'catalog',
			version: 1,
			spaceSlug: '',
			createdAt: Date.now(),
		},
		items: {},
	}),
};

// ── Helpers ──

export function booksCatalogDocId(space: string) {
	return `${space}:books:catalog` as const;
}
