import { Node } from '../types/node';

interface NodeType extends Partial<Node> {
	type: string;
	name: string;
	canBeSource: boolean;
	canBeTarget: boolean;
	panel?: string;
	switchParams?: {
		code: string;
		cases: Array<{
			name: string;
			value: number;
		}>;
	};
	questionParams?: {
		modelConfig: {
			model: string;
			max_tokens: number;
			temperature: number;
			top_p: number;
			frequency_penalty: number;
			presence_penalty: number;
			stream: boolean;
		};
		categories: Array<{
			name: string;
			value: string;
		}>;
	};
	codeParams?: {
		code: string;
	};
	noticeParams?: {
		message: string;
	};
	httpParams?: {
		url: string;
		method: string;
		contentType: string;
		bodyParams: any[];
		paramsParams: any[];
		headerParams: any[];
	};
	dbParams?: {
		sql: string;
		dbId: string;
		dbName: string;
	};
	llmParams?: {
		messages: Array<{
			role: string;
			content: string;
		}>;
		modelConfig: {
			model: string;
			isVision: string;
			max_tokens: number;
			temperature: number;
			top_p: number;
			frequency_penalty: number;
			presence_penalty: number;
			stream: boolean;
		};
	};
	mcpParams?: {
		mcpId: string;
		mcpName: string;
		prompt: string;
	};
	textParams?: {
		content: string;
	};
}

export const nodeTypes: NodeType[] = [
	{
		type: 'start',
		name: '开始',
		canBeSource: true,
		canBeTarget: false,
		panel: 'StartPanel',
	},
	{
		type: 'switch',
		name: '分支节点',
		canBeSource: true,
		canBeTarget: true,
		switchParams: {
			code: 'function main(args) {\n  // 根据参数，选择执行分支0 ，还是分支1\n  return args.arg1 === "xxx" ? 0 : 1;\n}',
			cases: [
				{
					name: '分支1',
					value: 0,
				},
				{
					name: '分支2',
					value: 1,
				}
			],
		},
		outputParams: [
			{
				name: 'index',
				type: 'String',
			},
		],
	},
	{
		type: 'question',
		name: '问题分类',
		canBeSource: true,
		canBeTarget: true,
		questionParams: {
			modelConfig: {
				model: '',
				max_tokens: 4096,
				temperature: 0.7,
				top_p: 1,
				frequency_penalty: 0,
				presence_penalty: 0,
				stream: true,
			},
			categories: [
				{
					name: '分类1',
					value: '1',
				},
				{
					name: '分类2',
					value: '2',
				},
			],
		},
		outputParams: [
			{
				name: 'index',
				type: 'String',
			},
		],
	},
	{
		type: 'code',
		name: '执行代码',
		canBeSource: true,
		canBeTarget: true,
		codeParams: {
			code: 'function main(args){\n  return ""\n}',
		},
		outputParams: [
			{
				name: 'result',
				type: 'String',
			}
		],
	},
	{
		type: 'notice',
		name: '消息通知',
		canBeSource: true,
		canBeTarget: true,
		noticeParams: {
			message: '',
		},
		outputParams: [
			{
				name: 'result',
				type: 'Boolean',
			},
		],
	},
	{
		type: 'http',
		name: 'HTTP请求',
		canBeSource: true,
		canBeTarget: true,
		httpParams: {
			url: '',
			method: '',
			contentType: 'application/json',
			bodyParams: [],
			paramsParams: [],
			headerParams: [],
		},
		outputParams: [
			{
				name: 'body',
				type: 'Object',
			},
			{
				name: 'status_code',
				type: 'Number',
			},
			{
				name: 'headers',
				type: 'Object',
			},
		],
	},
	{
		type: 'rag',
		name: 'RAG知识库',
		canBeSource: true,
		canBeTarget: true,
		ragParams: {
			datasetId: 0,
			datasetName: '',
			prompt: '${arg1}',
			onlyRecall: '1',
		},
		outputParams: [
			{
				name: 'result',
				type: 'String',
			},
			{
				name: 'ragSearched',
				type: 'String',
			},
		],
	},
	{
		type: 'db',
		name: 'DB数据库',
		canBeSource: true,
		canBeTarget: true,
		dbParams: {
			sql: '',
			dbId: '',
			dbName: '',
		},
		outputParams: [
			{
				name: 'result',
				type: 'String',
			},
		],
	},
	{
		type: 'llm',
		name: 'LLM大模型',
		canBeSource: true,
		canBeTarget: true,
		llmParams: {
			messages: [
				{
					role: 'user',
					content: '你是一个问题总结助手。\n任务：根据用户提问 ${arg1} 和系统答案 ${arg2}，生成一个标准的问题答案。\n要求：基于系统答案内容回答，回答准确、简洁、有用。',
				},
			],
			modelConfig: {
				model: '',
				isVision: '0',
				max_tokens: 4096,
				temperature: 0.7,
				top_p: 1,
				frequency_penalty: 0,
				presence_penalty: 0,
				stream: true,
			},
		},
		outputParams: [
			{
				name: 'content',
				type: 'String',
			},
		],
	},
	{
		type: 'mcp',
		name: 'MCP服务',
		canBeSource: true,
		canBeTarget: true,
		mcpParams: {
			mcpId: '',
			mcpName: '',
			prompt: '${arg1}',
		},
		outputParams: [
			{
				name: 'result',
				type: 'Object',
			},
			{
				name: 'status',
				type: 'String',
			},
		],
	},
	{
		type: 'text',
		name: '文本节点',
		canBeSource: true,
		canBeTarget: true,
		textParams: {
			content: '',
		},
		outputParams: [
			{
				name: 'result',
				type: 'String',
			},
		],
	},
	{
		type: 'end',
		name: '结束',
		canBeSource: false,
		canBeTarget: true,
	},
];

import { deepClone } from '/@/utils/other';

export const getNodeConfig = (type: string): NodeType => {
	const nodeConfig = nodeTypes.find((node) => node.type === type) || nodeTypes[0];
	// 深拷贝节点配置，避免多个节点共享同一个对象引用
	return deepClone(nodeConfig) as NodeType;
};
