import { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
import { apiRequest } from '../../../../helpers/apiRequest';
import { logger } from '../../../../helpers/logger';
import { handleApiError } from '../../../../helpers/errorHandler';
import { validateClientResponse } from '../client.types';

/**
 * Get wireless client details from Aruba Central
 *
 * @param this The n8n execution context
 * @returns Wireless client details
 */
export async function getWirelessClientDetails(
	this: IExecuteFunctions,
): Promise<INodeExecutionData[]> {
	try {
		const macaddr = this.getNodeParameter('macaddr', 0) as string;
		logger.debug(
			'monitoring:client:getWirelessClientDetails',
			`Getting wireless client details for ${macaddr}`,
		);

		if (!macaddr) {
			throw new Error('MAC address is required');
		}

		// Make API request to get wireless client details
		const response = await apiRequest.call(
			this,
			'GET',
			`/monitoring/v1/clients/wireless/${macaddr}`,
			{},
			{},
		);

		logger.debug('monitoring:client:getWirelessClientDetails', 'Got wireless client details', {
			macaddr,
		});

		// Validate and return the response
		if (validateClientResponse(response)) {
			return [{ json: response }];
		} else {
			logger.warn(
				'monitoring:client:getWirelessClientDetails',
				'Response validation failed',
				response,
			);
			return [{ json: response }];
		}
	} catch (error) {
		return handleApiError.call(this, error, 'Failed to get wireless client details');
	}
}

/**
 * Get wired client details from Aruba Central
 *
 * @param this The n8n execution context
 * @returns Wired client details
 */
export async function getWiredClientDetails(
	this: IExecuteFunctions,
): Promise<INodeExecutionData[]> {
	try {
		const macaddr = this.getNodeParameter('macaddr', 0) as string;
		logger.debug(
			'monitoring:client:getWiredClientDetails',
			`Getting wired client details for ${macaddr}`,
		);

		if (!macaddr) {
			throw new Error('MAC address is required');
		}

		// Make API request to get wired client details
		const response = await apiRequest.call(
			this,
			'GET',
			`/monitoring/v1/clients/wired/${macaddr}`,
			{},
			{},
		);

		logger.debug('monitoring:client:getWiredClientDetails', 'Got wired client details', {
			macaddr,
		});

		// Validate and return the response
		if (validateClientResponse(response)) {
			return [{ json: response }];
		} else {
			logger.warn(
				'monitoring:client:getWiredClientDetails',
				'Response validation failed',
				response,
			);
			return [{ json: response }];
		}
	} catch (error) {
		return handleApiError.call(this, error, 'Failed to get wired client details');
	}
}
