/**
 * rFiles Automerge document schemas.
 *
 * Granularity: one Automerge document per shared_space.
 * DocId format: {space}:files:cards:{sharedSpace}
 *
 * Binary file data stays on the filesystem — only metadata migrates.
 */

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

// ── Document types ──

export interface MediaFile {
	id: string;
	originalFilename: string;
	title: string | null;
	description: string;
	mimeType: string | null;
	fileSize: number;
	fileHash: string | null;
	storagePath: string;
	tags: string[];
	isProcessed: boolean;
	processingError: string | null;
	uploadedBy: string | null;
	sharedSpace: string | null;
	createdAt: number;
	updatedAt: number;
}

export interface MemoryCard {
	id: string;
	sharedSpace: string;
	title: string;
	body: string;
	cardType: string | null;
	tags: string[];
	position: number;
	createdBy: string | null;
	createdAt: number;
	updatedAt: number;
}

export interface FilesDoc {
	meta: {
		module: string;
		collection: string;
		version: number;
		spaceSlug: string;
		sharedSpace: string;
		createdAt: number;
	};
	files: Record<string, MediaFile>;
	memoryCards: Record<string, MemoryCard>;
}

// ── Schema registration ──

export const filesSchema: DocSchema<FilesDoc> = {
	module: 'files',
	collection: 'cards',
	version: 1,
	init: (): FilesDoc => ({
		meta: {
			module: 'files',
			collection: 'cards',
			version: 1,
			spaceSlug: '',
			sharedSpace: '',
			createdAt: Date.now(),
		},
		files: {},
		memoryCards: {},
	}),
};

// ── Helpers ──

export function filesDocId(space: string, sharedSpace: string) {
	return `${space}:files:cards:${sharedSpace}` as const;
}
