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

		const queryParams = {
			sku_type: skuType,
			service_type: serviceType,
		};

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

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

		logger.info('inventory:devices:getDeviceStats', 'Device stats retrieved successfully');
		return formatResponse(response);
	} catch (error) {
		logger.error('inventory:devices:getDeviceStats:error', error.message);
		throw error;
	}
}
