// api/inventory/devices/getArchivedDevices.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 getArchivedDevices(this: IExecuteFunctions): Promise<INodeExecutionData[]> {
	logger.info('inventory:devices:getArchivedDevices', 'Starting getArchivedDevices operation');
	try {
		const options = this.getNodeParameter('options', 0, {}) as any;

		const queryParams: { [key: string]: any } = {};

		if (options.limit) {
			queryParams.limit = options.limit;
		}

		if (options.offset) {
			queryParams.offset = options.offset;
		}

		logger.debug(
			'inventory:devices:getArchivedDevices',
			`Query params: ${JSON.stringify(queryParams)}`,
		);

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

		logger.info(
			'inventory:devices:getArchivedDevices',
			`Retrieved ${response.devices?.length || 0} archived devices`,
		);
		return formatResponse(response);
	} catch (error) {
		logger.error('inventory:devices:getArchivedDevices:error', error.message);
		throw error;
	}
}
