/**
 * rFlows Automerge document schemas.
 *
 * Granularity: one Automerge document per space (flow associations).
 * DocId format: {space}:flows:data
 *
 * v1: space-flow associations only
 * v2: adds canvasFlows (full node data) and activeFlowId
 * v3: adds mortgagePositions and reinvestmentPositions
 * v4: adds budgetSegments, budgetAllocations, budgetTotalAmount
 * v5: adds linkedTaskIds and linkedBoardId to OutcomeNodeData
 * v6: adds dripsSyncs to CanvasFlow for Drips protocol import tracking
 */

import type { DocSchema } from '../../shared/local-first/document';
import type { FlowNode, MortgagePosition, ReinvestmentPosition, DripsNodeConfig } from './lib/types';

// ── Document types ──

export interface SpaceFlow {
	id: string;
	spaceSlug: string;
	flowId: string;
	addedBy: string | null;
	createdAt: number;
}

export interface DripsSync {
	chainId: number;
	address: string;
	lastSyncedAt: number;
	streamIds: string[];
	splitsHash: string;
	importedNodeIds: string[];
}

export interface CanvasFlow {
	id: string;
	name: string;
	nodes: FlowNode[];
	createdAt: number;
	updatedAt: number;
	createdBy: string | null;
	dripsSyncs?: Record<string, DripsSync>;
}

export interface FlowsDoc {
	meta: {
		module: string;
		collection: string;
		version: number;
		spaceSlug: string;
		createdAt: number;
	};
	spaceFlows: Record<string, SpaceFlow>;
	canvasFlows: Record<string, CanvasFlow>;
	activeFlowId: string;
	mortgagePositions: Record<string, MortgagePosition>;
	reinvestmentPositions: Record<string, ReinvestmentPosition>;
	budgetSegments: Record<string, { name: string; color: string; createdBy: string | null }>;
	budgetAllocations: Record<string, { participantDid: string; allocations: Record<string, number>; updatedAt: number }>;
	budgetTotalAmount: number;
}

// ── Schema registration ──

export const flowsSchema: DocSchema<FlowsDoc> = {
	module: 'flows',
	collection: 'data',
	version: 6,
	init: (): FlowsDoc => ({
		meta: {
			module: 'flows',
			collection: 'data',
			version: 6,
			spaceSlug: '',
			createdAt: Date.now(),
		},
		spaceFlows: {},
		canvasFlows: {},
		activeFlowId: '',
		mortgagePositions: {},
		reinvestmentPositions: {},
		budgetSegments: {},
		budgetAllocations: {},
		budgetTotalAmount: 0,
	}),
	migrate: (doc: any, _fromVersion: number) => {
		if (!doc.canvasFlows) doc.canvasFlows = {};
		if (!doc.activeFlowId) doc.activeFlowId = '';
		if (!doc.mortgagePositions) doc.mortgagePositions = {};
		if (!doc.reinvestmentPositions) doc.reinvestmentPositions = {};
		if (!doc.budgetSegments) doc.budgetSegments = {};
		if (!doc.budgetAllocations) doc.budgetAllocations = {};
		if (doc.budgetTotalAmount === undefined) doc.budgetTotalAmount = 0;
		// v5: migrate outcome nodes — add linkedTaskIds/linkedBoardId
		if (doc.canvasFlows) {
			for (const flow of Object.values(doc.canvasFlows) as any[]) {
				if (!flow.nodes) continue;
				for (const node of flow.nodes as any[]) {
					if (node.type === 'outcome' && !node.data.linkedTaskIds) {
						node.data.linkedTaskIds = [];
					}
				}
			}
		}
		// v6: add dripsSyncs to canvas flows
		if (doc.canvasFlows) {
			for (const flow of Object.values(doc.canvasFlows) as any[]) {
				if (!flow.dripsSyncs) flow.dripsSyncs = {};
			}
		}
		doc.meta.version = 6;
		return doc;
	},
};

// ── Helpers ──

export function flowsDocId(space: string) {
	return `${space}:flows:data` as const;
}
