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

/**
 * Get Switch Port PoE Info for CX Switch
 *
 * GET /monitoring/v1/cx_switches/{serial}/poe_detail
 *
 * @param this The n8n execution context
 * @returns Formatted API response
 */
export async function getCxSwitchPortPoeInfo(
	this: IExecuteFunctions,
): Promise<INodeExecutionData[]> {
	try {
		// Get parameters
		const serial = this.getNodeParameter('serial', 0) as string;
		const port = this.getNodeParameter('port', 0) as string;

		// Construct query parameters
		const qs: IDataObject = {
			port,
		};

		logger.debug(
			'monitoring:switch:getCxSwitchPortPoeInfo',
			`Getting port PoE info for CX switch with serial: ${serial}, port: ${port}`,
		);

		// Make API request
		const endpoint = `/monitoring/v1/cx_switches/${encodeURIComponent(serial)}/poe_detail`;
		const responseData = await apiRequest.call(this, 'GET', endpoint, {}, qs);

		logger.debug(
			'monitoring:switch:getCxSwitchPortPoeInfo',
			'Successfully retrieved CX switch port PoE info',
		);

		// Return formatted response
		return [{ json: responseData }];
	} catch (error) {
		logger.error('monitoring:switch:getCxSwitchPortPoeInfo:error', { message: error.message });
		return handleApiError.call(this, error, 'Failed to get CX switch port PoE info');
	}
}
