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

export type HttpRequestData = INodeDataBaseSchema & {
	method: 'get' | 'post' | 'put' | 'delete' | 'patch'
	ssl_verify: boolean
	body: {
		type: string
		data: Array<{
			key: string
			type: 'text' | 'file' | string
			value: string
		}>
	}
	params: Array<{
		name: string
		value: string
	}>
	title: string
	url: string
	authorization: {
		type: 'none' | 'api-key' | string
		config: {
			api_key?: string
			header?: string
			type?: 'basic' | 'bearer' | 'custom' | string
			[key: string]: any
		}
	}
	timeout_config: {
		max_write_timeout: number
		max_read_timeout: number
		max_connect_timeout: number
	}
	heads: Array<{
		name: string
		value: string
	}>
}

export const httpNode: INodeType = {
	version: ['1'],
	displayName: getNodeDisplayName('http-request'),
	name: 'http-request',
	Setter,
	description: getNodeDescription('http-request'),
	group: 'data',
	icon: 'lucide:link',
	iconColor: '#875bf7',
	inputs: [NodeConnectionTypes.main],
	outputs: [NodeConnectionTypes.main],
	validate: (data: HttpRequestData) => {
		return !!data?.url ? false : i18n.t('pages.httpSetter.urlRequired')
	},
	getSubtitle: (data: HttpRequestData) => {
		return data?.url ? `${data.method} ${data.url}` : ''
	},
	schema: {
		appAgentId: '',
		parentId: '',
		position: {
			x: 20,
			y: 30
		},
		width: 96,
		height: 96,
		selected: false,
		nodeType: 'http-request',
		zIndex: 1,
		data: {
			outputs: [
				{
					name: 'body',
					describe: getNodeOutputText('http', 'body'),
					type: 'string'
				},
				{
					name: 'status_code',
					describe: getNodeOutputText('http', 'statusCode'),
					type: 'number'
				},
				{
					name: 'headers',
					describe: getNodeOutputText('http', 'headers'),
					type: 'object'
				}
			],
			output_can_alter: false,
			variables: [],
			method: 'post',
			ssl_verify: false,
			isInIteration: false,
			default_value: [],
			body: {
				type: 'json',
				data: [
					{
						key: '',
						type: 'text',
						value: '{"id":"b3a4aabb-a6b8-47f3-8a32-f45930f7d7b8"}'
					}
				]
			},
			params: [],
			title: getNodeDisplayName('http-request'),
			type: 'http-request',
			error_strategy: 'none',
			retry_config: {
				max_retries: 3,
				retry_enabled: false,
				retry_interval: 100
			},
			url: '#{env.api_address}/api/agent/getAgentInfo',
			authorization: {
				type: 'none',
				config: {
					api_key: '',
					header: '',
					type: ''
				}
			},
			timeout_config: {
				max_write_timeout: 0,
				max_read_timeout: 0,
				max_connect_timeout: 0
			},
			heads: [
				{
					name: 'Authorization',
					value: ''
				}
			],
			selected: true,
			desc: '',
			isInLoop: false
		}
	}
}
