// Example context mapper for a custom framework plugin.
// Import types from @aperant/framework:
//   import type { AperantContextView, ContextDecision } from "@aperant/framework/types";
//
// In the plugin template, we define the types inline for illustration.
// Replace with the import above when building a real plugin.

interface AperantContextView {
	frameworkId: string
	title: string
	scope: string
	decisions: Array<{ id: string; summary: string; rationale?: string }>
	deferredItems: string[]
	openQuestions: string[]
	metadata: Record<string, unknown>
}

const FRAMEWORK_ID = 'my-framework'

/**
 * Translates your framework's context artifacts into AperantContextView.
 *
 * @param artifacts - Map of filename -> file content from your framework's context stage
 * @returns Normalized context view for the Aperant UI
 */
export function toContextView(artifacts: Map<string, string>): AperantContextView {
	const requirementsMd = artifacts.get('requirements.md') ?? ''

	// Parse your native format into the Aperant shape.
	// This example extracts a title from the first markdown heading
	// and treats each bullet point as a decision.
	const titleMatch = requirementsMd.match(/^#\s+(.+)$/m)
	const bullets = [...requirementsMd.matchAll(/^-\s+(.+)$/gm)]

	return {
		frameworkId: FRAMEWORK_ID,
		title: titleMatch?.[1] ?? 'Untitled',
		scope: 'Custom framework context',
		decisions: bullets.map((m, i) => ({
			id: `REQ-${String(i + 1).padStart(2, '0')}`,
			summary: m[1],
		})),
		deferredItems: [],
		openQuestions: [],
		metadata: {
			rawRequirements: requirementsMd,
		},
	}
}
