// api/inventory/devices/archiveDevices.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 archiveDevices(this: IExecuteFunctions): Promise<INodeExecutionData[]> {
	logger.info('inventory:devices:archiveDevices', 'Starting archiveDevices operation');
	try {
		const serialsString = this.getNodeParameter('serials', 0) as string;
		const serials = serialsString
			.split(',')
			.map((s) => s.trim())
			.filter((s) => s.length > 0);

		if (serials.length === 0) {
			throw new Error('At least one serial number must be provided');
		}

		const requestBody = {
			serials,
		};

		logger.debug(
			'inventory:devices:archiveDevices',
			`Archiving ${serials.length} devices: ${serials.join(', ')}`,
		);

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

		logger.info('inventory:devices:archiveDevices', 'Devices archived successfully');
		return formatResponse(response);
	} catch (error) {
		logger.error('inventory:devices:archiveDevices:error', error.message);
		throw error;
	}
}
