/**
 * rMeets Automerge document schemas.
 *
 * Syncs meeting scheduling and history per space.
 * Actual video calls run on the Jitsi instance.
 *
 * DocId format: {space}:meets:meetings
 */

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

export interface Meeting {
	id: string;
	roomName: string;
	title: string;
	scheduledAt: number;
	hostDid: string | null;
	participants: string[];
	createdAt: number;
}

export interface MeetsDoc {
	meta: { module: string; collection: string; version: number; spaceSlug: string; createdAt: number };
	meetings: Record<string, Meeting>;
	meetingHistory: { id: string; roomName: string; title: string; startedAt: number; endedAt: number; participantCount: number }[];
}

export const meetsSchema: DocSchema<MeetsDoc> = {
	module: 'meets',
	collection: 'meetings',
	version: 1,
	init: (): MeetsDoc => ({
		meta: { module: 'meets', collection: 'meetings', version: 1, spaceSlug: '', createdAt: Date.now() },
		meetings: {},
		meetingHistory: [],
	}),
	migrate: (doc: any) => {
		if (!doc.meetings) doc.meetings = {};
		if (!doc.meetingHistory) doc.meetingHistory = [];
		doc.meta.version = 1;
		return doc;
	},
};

export function meetsDocId(space: string) { return `${space}:meets:meetings` as const; }
