/**
 * Layer & Flow types for the rSpace tab/layer system.
 *
 * Each "tab" is a Layer — a named canvas page backed by a module.
 * Layers stack vertically. Flows are typed connections (economic, trust,
 * data, attention, governance) that move between shapes on different layers.
 *
 * The "stack view" renders all layers from the side, showing flows as
 * arcs/lines between strata.
 */

// ── Flow types ──

export type FlowKind =
	| "economic"    // token/currency/value flows
	| "trust"       // reputation, attestation, endorsement
	| "data"        // information, content, feeds
	| "attention"   // views, engagement, focus
	| "governance"  // votes, proposals, decisions
	| "resource"    // files, assets, media
	| "custom";     // user-defined

export const FLOW_COLORS: Record<FlowKind, string> = {
	economic:   "#4ade80", // green
	trust:      "#c4b5fd", // violet
	data:       "#60a5fa", // blue
	attention:  "#fcd34d", // amber
	governance: "#a78bfa", // purple
	resource:   "#6ee7b7", // emerald
	custom:     "#94a3b8", // slate
};

export const FLOW_LABELS: Record<FlowKind, string> = {
	economic:   "Economic",
	trust:      "Trust",
	data:       "Data",
	attention:  "Attention",
	governance: "Delegation",
	resource:   "Resource",
	custom:     "Custom",
};

// ── Feed definition ──

/**
 * Feed definition — describes a data feed that a module can expose to other layers.
 * Feeds are the connective tissue between layers: they let data, economic value,
 * trust signals, etc. flow between rApps.
 */
export interface FeedDefinition {
	/** Feed identifier (unique within the module) */
	id: string;
	/** Human-readable name */
	name: string;
	/** What kind of flow this feed carries */
	kind: FlowKind;
	/** Description of what this feed provides */
	description: string;
	/** Shape types this feed emits (e.g. "folk-note", "folk-token-mint") */
	emits?: string[];
	/** Whether this feed supports filtering */
	filterable?: boolean;
}

// ── Layer definition ──

export interface Layer {
	/** Unique layer ID (e.g. "layer-abc123") */
	id: string;
	/** Module ID this layer is bound to (e.g. "canvas", "notes", "funds") */
	moduleId: string;
	/** Display label (defaults to module name, user-customizable) */
	label: string;
	/** Position in the tab bar (0-indexed, left to right) */
	order: number;
	/** Layer color for the stack view strata */
	color: string;
	/** Whether this layer is visible in stack view */
	visible: boolean;
	/** Created timestamp */
	createdAt: number;
	/** If set, this layer shows data from another space (cross-space layer). */
	spaceSlug?: string;
	/** Resolved permission level in the source space (for cross-space layers). */
	spaceRole?: 'viewer' | 'member' | 'moderator' | 'admin';
}

// ── Inter-layer flow ──

export interface LayerFlow {
	/** Unique flow ID */
	id: string;
	/** Flow type */
	kind: FlowKind;
	/** Source layer ID */
	sourceLayerId: string;
	/** Source shape ID (optional — can be layer-wide) */
	sourceShapeId?: string;
	/** Target layer ID */
	targetLayerId: string;
	/** Target shape ID (optional — can be layer-wide) */
	targetShapeId?: string;
	/** Human-readable label */
	label?: string;
	/** Flow strength/weight (0-1, affects visual thickness) */
	strength: number;
	/** Whether this flow is currently active */
	active: boolean;
	/** Custom color override */
	color?: string;
	/** Metadata for the flow */
	meta?: Record<string, unknown>;
}

// ── Layer config stored in Automerge doc ──

export interface LayerConfig {
	/** Ordered list of layers */
	layers: { [id: string]: Layer };
	/** Inter-layer flows */
	flows: { [id: string]: LayerFlow };
	/** Currently active layer ID */
	activeLayerId: string;
	/** View mode: 'flat' (normal tabs) or 'stack' (side/3D view) */
	viewMode: "flat" | "stack";
}
