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

export type WebhookContentType =
	| 'application/json'
	| 'application/x-www-form-urlencoded'
	| 'text/plain'
	| 'application/octet-stream'
	| 'multipart/form-data'

export type WebhookMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head'

export type WebhookParam = {
	name: string
	type: Extract<NodeVariableType, 'string' | 'number' | 'boolean'>
	required: boolean
}

export type WebhookHeader = {
	name: string
	required: boolean
}

export type WebhookBodyParam = {
	name: string
	type: NodeVariableType
	required: boolean
}

export type WebhookData = INodeDataBaseSchema & {
	async_mode: boolean
	method: WebhookMethod
	webhook_url: string
	webhook_debug_url: string
	content_type: WebhookContentType
	headers: WebhookHeader[]
	params: WebhookParam[]
	body: WebhookBodyParam[]
	response_content_type: WebhookContentType
	response_body: string
	response_status_code: number
}

export const webhookNode: INodeType = {
	version: ['1'],
	displayName: getNodeDisplayName('trigger-webhook'),
	name: 'trigger-webhook',
	description: getNodeDescription('trigger-webhook'),
	group: 'start',
	icon: 'lucide:webhook',
	iconColor: '#2e90fa',
	inputs: [],
	outputs: [NodeConnectionTypes.main],
	Setter,
	schema: {
		appAgentId: '',
		parentId: '',
		position: {
			x: 20,
			y: 30
		},
		width: 96,
		height: 96,
		selected: false,
		nodeType: 'trigger-webhook',
		zIndex: 1,
		data: {
			title: getNodeDisplayName('trigger-webhook'),
			type: 'trigger-webhook',
			isInIteration: false,
			iteration_id: '',
			isInLoop: false,
			loop_id: '',
			variables: [],
			retry_config: {
				max_retries: 3,
				retry_enabled: false,
				retry_interval: 100
			},
			error_strategy: 'none',
			fail_branch_node_id: '',
			default_value: [],
			output_can_alter: false,
			outputs: [
				{
					name: 'payload._webhook_raw',
					describe: getNodeOutputText('webhook', 'rawRequestBody'),
					type: 'object'
				}
			],
			async_mode: false,
			method: 'post',
			webhook_url: '',
			webhook_debug_url: '',
			content_type: 'application/json',
			headers: [],
			params: [],
			body: [],
			response_content_type: 'application/json',
			response_body: '{\n  "success": true\n}',
			response_status_code: 200
		}
	}
}
