/**
 * rForum Automerge document types — provisioning metadata for Discourse instances.
 *
 * One global doc (`global:forum:instances`) holds all forum instances and provision logs.
 * Not space-scoped — instances belong to users, not spaces.
 */

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

// ── Instance types ──

export type InstanceStatus =
	| 'pending'
	| 'provisioning'
	| 'installing'
	| 'configuring'
	| 'active'
	| 'error'
	| 'destroying'
	| 'destroyed';

export interface ForumInstance {
	id: string;
	userId: string;
	name: string;
	domain: string;
	status: InstanceStatus;
	errorMessage: string;
	discourseVersion: string;
	provider: string;
	vpsId: string;
	vpsIp: string;
	region: string;
	size: string;
	adminEmail: string;
	smtpConfig: Record<string, unknown>;
	dnsRecordId: string;
	sslProvisioned: boolean;
	createdAt: number;
	updatedAt: number;
	provisionedAt: number;
	destroyedAt: number;
}

// ── Provision log types ──

export type StepStatus = 'running' | 'success' | 'error' | 'skipped';

export interface ProvisionLog {
	id: string;
	step: string;
	status: StepStatus;
	message: string;
	metadata: Record<string, unknown>;
	startedAt: number;
	completedAt: number;
}

// ── Document root ──

export interface ForumDoc {
	meta: {
		module: string;
		collection: string;
		version: number;
		createdAt: number;
	};
	instances: Record<string, ForumInstance>;
	provisionLogs: Record<string, ProvisionLog[]>;
}

// ── Schema definition ──

export const forumSchema: DocSchema<ForumDoc> = {
	module: 'forum',
	collection: 'instances',
	version: 1,
	init: (): ForumDoc => ({
		meta: {
			module: 'forum',
			collection: 'instances',
			version: 1,
			createdAt: Date.now(),
		},
		instances: {},
		provisionLogs: {},
	}),
};

// ── Document ID ──

export const FORUM_DOC_ID = 'global:forum:instances' as const;
