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

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

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

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

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

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

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