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

/**
 * Get LLDP Device Neighbor Info for CX Switch Stack
 *
 * GET /monitoring/v1/cx_switch_stacks/{stack_id}/neighbors
 *
 * @param this The n8n execution context
 * @returns Formatted API response
 */
export async function getCxSwitchStackNeighborInfo(
	this: IExecuteFunctions,
): Promise<INodeExecutionData[]> {
	try {
		// Get parameters
		const stackId = this.getNodeParameter('stack_id', 0) as string;

		logger.debug(
			'monitoring:switch:getCxSwitchStackNeighborInfo',
			`Getting LLDP neighbor info for CX switch stack with ID: ${stackId}`,
		);

		// Make API request
		const endpoint = `/monitoring/v1/cx_switch_stacks/${encodeURIComponent(stackId)}/neighbors`;
		const responseData = await apiRequest.call(this, 'GET', endpoint);

		logger.debug(
			'monitoring:switch:getCxSwitchStackNeighborInfo',
			'Successfully retrieved CX switch stack LLDP neighbor info',
		);

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