import type { Edge, Node } from '@vue-flow/core';

/**
 * 从画布连线生成 delegateHints
 */
export function canvasToHints(
	nodes: Node[],
	edges: Edge[]
): { hints: string; edges: Array<{ from: string; to: string; type: string; note?: string }> } {
	if (!edges.length) {
		return { hints: '无特定调度建议，请根据任务需求自行安排。', edges: [] };
	}

	const nodeMap = new Map<string, string>();
	nodes.forEach(n => {
		if (n.data?.label) nodeMap.set(n.id, n.data.label);
	});

	const edgeInfos = edges.map(e => ({
		from: nodeMap.get(e.source) || e.source,
		to: nodeMap.get(e.target) || e.target,
		type: e.data?.edgeType || 'serial',
		note: e.data?.note || '',
	}));

	const serialEdges = edgeInfos.filter(e => e.type === 'serial');
	const parallelEdges = edgeInfos.filter(e => e.type === 'parallel');

	let hints = '建议执行顺序：\n';
	if (serialEdges.length) {
		const chains = serialEdges.map(e => {
			const note = e.note ? `（${e.note}）` : '';
			return `${e.from} → ${e.to}${note}`;
		});
		hints += `串行依赖：${chains.join('；')}\n`;
	}
	if (parallelEdges.length) {
		const parallel = parallelEdges.map(e => `${e.from} 与 ${e.to}`);
		hints += `可并行执行：${parallel.join('；')}\n`;
	}

	return { hints, edges: edgeInfos };
}
