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

export type DatabaseType = 'mysql' | 'sqlserver' | 'oracle' | 'postgresql'

export type DatabaseData = INodeDataBaseSchema & {
	db_type: DatabaseType
	db_host: string
	db_port: number
	db_username: string
	db_password: {
		// aesKey的RSA加密后置
		rsa_aesKey: string
		// 密文
		cipher_text: string
	}
	db_password_rsa_aesKey: string
	db_name: string
	db_properties: string
	query_sql: string
}

export const databaseNode: INodeType = {
	version: ['1'],
	displayName: getNodeDisplayName('database'),
	name: 'database-query',
	Setter,
	description: getNodeDescription('database'),
	group: 'data',
	icon: 'lucide:database-zap',
	iconColor: '#64dc34',
	inputs: [NodeConnectionTypes.main],
	outputs: [NodeConnectionTypes.main],
	validate: (data: DatabaseData) => {
		if (!data?.db_type) return i18n.t('pages.databaseSetter.dbTypeRequired')
		if (!data?.db_host?.trim()) return i18n.t('pages.databaseSetter.dbHostRequired')
		if (!data?.db_port) return i18n.t('pages.databaseSetter.dbPortRequired')
		if (!data?.db_username?.trim()) return i18n.t('pages.databaseSetter.dbUsernameRequired')
		if (!data?.db_name?.trim()) return i18n.t('pages.databaseSetter.dbNameRequired')
		if (!data?.query_sql?.trim()) return i18n.t('pages.databaseSetter.querySqlRequired')
		return false
	},
	schema: {
		appAgentId: '',
		parentId: '',
		position: {
			x: 20,
			y: 30
		},
		width: 96,
		height: 96,
		selected: false,
		nodeType: 'database-query',
		zIndex: 1,
		data: {}
	}
}
