import {
	NodeConnectionTypes,
	type INodeDataBaseSchema,
	type INodeType,
	type NodeVariable
} from '../../Interface'
import Setter from './setter.vue'
import i18n from '@/i18n'
import { getNodeDescription, getNodeDisplayName } from '@/nodes/i18n'

export type KnowledgeRetrievalData = INodeDataBaseSchema & {
	query: string
	top_k: number
	knowledge_base_ids: string[]
	knowledge_ids: string[]
	score_threshold_enable: boolean
	score_threshold: number
}

export const createDefaultQueryVariable = (): NodeVariable => ({
	name: 'query',
	describe: '查询文本',
	type: 'string',
	value: ''
})

export const knowledgeRetrievalNode: INodeType = {
	version: ['1'],
	displayName: getNodeDisplayName('knowledge-retrieval'),
	name: 'knowledge-retrieval',
	Setter,
	description: getNodeDescription('knowledge-retrieval'),
	group: 'data',
	icon: 'lucide:book-open',
	iconColor: '#2e90fa',
	inputs: [NodeConnectionTypes.main],
	outputs: [NodeConnectionTypes.main],
	validate: (data: KnowledgeRetrievalData) => {
		if (!data?.query?.trim()) {
			return '请输入查询内容'
		}

		const hasKnowledgeBase = data?.knowledge_base_ids?.some((id) => id?.trim())
		const hasKnowledge = data?.knowledge_ids?.some((id) => id?.trim())
		return hasKnowledgeBase || hasKnowledge
			? false
			: i18n.t('pages.knowledgeRetrievalSetter.knowledgeRequired')
	},
	getSubtitle: (data: KnowledgeRetrievalData) => {
		const knowledgeCount = data?.knowledge_ids?.length || 0
		const baseCount = data?.knowledge_base_ids?.length || 0
		if (knowledgeCount) return `${knowledgeCount} knowledge`
		if (baseCount) return `${baseCount} base`
		return ''
	},
	schema: {
		appAgentId: '',
		parentId: '',
		position: {
			x: 20,
			y: 30
		},
		width: 96,
		height: 96,
		selected: false,
		nodeType: 'knowledge-retrieval',
		zIndex: 1,
		data: {}
	}
}
