// Example plan mapper for a custom framework plugin.
// Import types from @aperant/framework:
//   import type { AperantPlanView, PlanWave, PlanTask } from "@aperant/framework/types";

interface PlanTask {
	id: string
	name: string
	status: 'pending' | 'in-progress' | 'completed' | 'failed' | 'skipped'
	files: string[]
	dependsOn: string[]
	description?: string
}

interface AperantPlanView {
	frameworkId: string
	title: string
	totalTasks: number
	waves: Array<{ waveNumber: number; tasks: PlanTask[] }>
	metadata: Record<string, unknown>
}

const FRAMEWORK_ID = 'my-framework'

/**
 * Translates your framework's plan artifacts into AperantPlanView.
 *
 * @param artifacts - Map of filename -> file content from your framework's plan stage
 * @returns Normalized plan view for the Aperant UI
 */
export function toPlanView(artifacts: Map<string, string>): AperantPlanView {
	const taskBreakdownRaw = artifacts.get('task-breakdown.json')

	if (!taskBreakdownRaw) {
		return {
			frameworkId: FRAMEWORK_ID,
			title: 'No plan found',
			totalTasks: 0,
			waves: [],
			metadata: {},
		}
	}

	// Parse your native plan format
	const breakdown = JSON.parse(taskBreakdownRaw)
	const tasks: PlanTask[] = (breakdown.tasks ?? []).map(
		(t: Record<string, unknown>, i: number) => ({
			id: String(t.id ?? `task-${i + 1}`),
			name: String(t.title ?? t.name ?? `Task ${i + 1}`),
			status: 'pending' as const,
			files: Array.isArray(t.files) ? (t.files as string[]) : [],
			dependsOn: Array.isArray(t.depends_on) ? (t.depends_on as string[]) : [],
			description: t.description ? String(t.description) : undefined,
		}),
	)

	return {
		frameworkId: FRAMEWORK_ID,
		title: String(breakdown.title ?? 'Implementation Plan'),
		totalTasks: tasks.length,
		waves: tasks.length > 0 ? [{ waveNumber: 1, tasks }] : [],
		metadata: {
			rawBreakdown: breakdown,
			designNotes: artifacts.get('design-notes.md') ?? null,
		},
	}
}
