// ArubaCentral/methods/configuration/ap/dot11gRadio.methods.ts
import { IExecuteFunctions, INodeExecutionData, IDataObject } from 'n8n-workflow';
import { apiRequest } from '../../../helpers/apiRequest';
import { formatResponse } from '../../../helpers/responseFormatter';
import { handlePagination } from '../../../helpers/pagination';
import { RadioProfile } from './apConfiguration.types';

export async function getAllDot11gRadioProfiles(
	this: IExecuteFunctions,
): Promise<INodeExecutionData[]> {
	try {
		// Get parameters
		const groupNameOrGuidOrSerial = this.getNodeParameter('groupNameOrGuidOrSerial', 0) as string;
		const returnAll = this.getNodeParameter('returnAll', 0, false) as boolean;

		let limit: number | undefined;
		if (!returnAll) {
			limit = this.getNodeParameter('limit', 0, 50) as number;
		}

		// Build the endpoint
		const endpoint = `/configuration/v1/dot11g_radio_profiles/${groupNameOrGuidOrSerial}`;

		// Log the request
		console.log('Getting all Dot11g radio profiles with parameters:', {
			groupNameOrGuidOrSerial,
			returnAll,
			limit,
		});

		if (returnAll || limit) {
			// Use pagination helper with a batch size of 100
			return await handlePagination.call(
				this,
				endpoint,
				'GET',
				{},
				{},
				{
					path: ['dot11g_list'],
					fallbackPaths: [['data', 'dot11g_list']],
				},
				returnAll,
				limit,
				100, // Conservative batch size for dot11g profiles
			);
		} else {
			// Make a single API call without pagination
			const response = await apiRequest.call(this, 'GET', endpoint, {}, {});
			console.log('Received Dot11g radio profiles');
			return formatResponse.call(this, response);
		}
	} catch (error) {
		console.log('ERROR in getAllDot11gRadioProfiles:', error);
		throw error;
	}
}

export async function getDot11gRadioProfile(
	this: IExecuteFunctions,
): Promise<INodeExecutionData[]> {
	try {
		// Get parameters
		const groupNameOrGuidOrSerial = this.getNodeParameter('groupNameOrGuidOrSerial', 0) as string;
		const profileName = this.getNodeParameter('profileName', 0) as string;

		// Log the request
		console.log('Getting Dot11g radio profile with parameters:', {
			groupNameOrGuidOrSerial,
			profileName,
		});

		// Build the endpoint
		const endpoint = `/configuration/v1/dot11g_radio_profile/${groupNameOrGuidOrSerial}/${profileName}`;

		// Make API call
		const response = await apiRequest.call(this, 'GET', endpoint, {}, {});
		console.log('Received Dot11g radio profile');

		return formatResponse.call(this, response);
	} catch (error) {
		console.log('ERROR in getDot11gRadioProfile:', error);
		throw error;
	}
}

export async function updateDot11gRadioProfile(
	this: IExecuteFunctions,
): Promise<INodeExecutionData[]> {
	try {
		// Get parameters
		const groupNameOrGuidOrSerial = this.getNodeParameter('groupNameOrGuidOrSerial', 0) as string;
		const profileName = this.getNodeParameter('profileName', 0) as string;
		const radioProfileJson = this.getNodeParameter('radioProfile', 0) as string;

		// Parse the Radio Profile JSON
		let radioProfile: RadioProfile;
		try {
			radioProfile = JSON.parse(radioProfileJson);
		} catch (parseError) {
			throw new Error(`Invalid JSON format for radio profile: ${parseError.message}`);
		}

		// Log the request
		console.log('Updating Dot11g radio profile:', {
			groupNameOrGuidOrSerial,
			profileName,
			radioProfile,
		});

		// Build the endpoint
		const endpoint = `/configuration/v1/dot11g_radio_profile/${groupNameOrGuidOrSerial}/${profileName}`;

		// Make API call
		const response = await apiRequest.call(this, 'POST', endpoint, radioProfile as IDataObject, {});
		console.log('Dot11g radio profile updated successfully');

		return formatResponse.call(this, response);
	} catch (error) {
		console.log('ERROR in updateDot11gRadioProfile:', error);
		throw error;
	}
}

export async function deleteDot11gRadioProfile(
	this: IExecuteFunctions,
): Promise<INodeExecutionData[]> {
	try {
		// Get parameters
		const groupNameOrGuidOrSerial = this.getNodeParameter('groupNameOrGuidOrSerial', 0) as string;
		const profileName = this.getNodeParameter('profileName', 0) as string;

		// Log the request
		console.log('Deleting Dot11g radio profile with parameters:', {
			groupNameOrGuidOrSerial,
			profileName,
		});

		// Build the endpoint
		const endpoint = `/configuration/v1/dot11g_radio_profile/${groupNameOrGuidOrSerial}/${profileName}`;

		// Make API call
		const response = await apiRequest.call(this, 'DELETE', endpoint, {}, {});
		console.log('Dot11g radio profile deleted successfully');

		return formatResponse.call(this, response);
	} catch (error) {
		console.log('ERROR in deleteDot11gRadioProfile:', error);
		throw error;
	}
}
