// ArubaCentral.node.ts
import {
	IExecuteFunctions,
	INodeExecutionData,
	INodeType,
	INodeTypeDescription,
} from 'n8n-workflow';
import { executeOperation } from './helpers/executeOperation';
import { allDescriptions } from './api/descriptions';
import { logger } from './helpers/logger';

export class ArubaCentral implements INodeType {
	description: INodeTypeDescription = {
		displayName: 'Aruba Central',
		name: 'arubaCentral',
		icon: 'file:arubaCentral.svg',
		group: ['transform'],
		version: 1,
		subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
		description: 'Interact with the Aruba Central API',
		defaults: {
			name: 'Aruba Central',
		},
		usableAsTool: true,
		inputs: ['main'],
		outputs: ['main'],
		credentials: [
			{
				name: 'ArubaCentralOAuth2Api',
				required: true,
			},
		],
		// Use the consolidated descriptions from the API structure directly
		properties: allDescriptions,
	};

	async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
		logger.info('node:execute', 'Starting Aruba Central node execution');

		try {
			const domain = this.getNodeParameter('domain', 0) as string;
			const resource = this.getNodeParameter('resource', 0) as string;
			const operation = this.getNodeParameter('operation', 0) as string;

			logger.info('node:execute', `Executing operation: ${domain}.${resource}.${operation}`);

			const result = await executeOperation.call(this);
			logger.debug('node:execute', `Operation completed successfully with ${result.length} items`);

			return [result];
		} catch (error) {
			logger.error('node:execute:error', error.message);

			if (this.continueOnFail()) {
				logger.info(
					'node:execute',
					'Continuing execution despite error (continueOnFail is enabled)',
				);
				return [[{ json: { error: error.message } }]];
			}

			throw error;
		} finally {
			logger.debug('node:execute', 'Node execution completed');
		}
	}
}
