// api/inventory/devices/verifyDevice.methods.ts
import { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
import { apiRequest } from '../../../helpers/apiRequest';
import { formatResponse } from '../../../helpers/responseFormatter';
import { logger } from '../../../helpers/logger';

export async function verifyDevice(this: IExecuteFunctions): Promise<INodeExecutionData[]> {
	logger.info('inventory:devices:verifyDevice', 'Starting verifyDevice operation');
	try {
		const mac = this.getNodeParameter('mac', 0) as string;
		const serial = this.getNodeParameter('serial', 0) as string;
		const partNumber = this.getNodeParameter('partNumber', 0) as string;

		const deviceObject: { [key: string]: any } = {
			mac,
			serial,
		};

		if (partNumber) {
			deviceObject.partNumber = partNumber;
		}

		// The API expects an array of devices, even for single device verification
		const requestBody = [deviceObject];

		logger.debug(
			'inventory:devices:verifyDevice',
			`Verifying device with MAC: ${mac}, Serial: ${serial}`,
		);

		// Make API request
		const response = await apiRequest.call(
			this,
			'POST',
			'/platform/device_inventory/v1/devices/verify',
			requestBody,
			{},
		);

		logger.info('inventory:devices:verifyDevice', 'Device verification completed');
		return formatResponse(response);
	} catch (error) {
		logger.error('inventory:devices:verifyDevice:error', error.message);
		throw error;
	}
}
