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

/**
 * Delete Serial
 * DELETE /monitoring/v2/mobility_controllers/{serial}
 *
 * @param this The n8n execution context
 * @returns Formatted API response
 */
export async function deleteSerial(this: IExecuteFunctions): Promise<INodeExecutionData[]> {
	try {
		// Get parameters
		const serial = this.getNodeParameter('serial', 0) as string;

		// Construct request options
		const options: IDataObject = {};

		// Construct API endpoint manually to avoid template literal issues
		const endpoint = '/monitoring/v2/mobility_controllers/' + encodeURIComponent(serial);
		console.log('Constructed endpoint:', endpoint);

		// Make API request
		const responseData = await apiRequest.call(this, 'DELETE', endpoint, options);

		// Format and return response
		return [{ json: responseData }];
	} catch (error) {
		logger.error('monitoring:mobilitycontroller:deleteSerial:error', { message: error.message });
		return handleApiError.call(this, error, 'Failed to execute delete serial');
	}
}
