// ArubaCentral/methods/configuration/ap/dot11aRadio.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 getAllDot11aRadioProfiles(
	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/dot11a_radio_profiles/${groupNameOrGuidOrSerial}`;

		// Log the request
		console.log('Getting all Dot11a 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: ['dot11a_list'],
					fallbackPaths: [['data', 'dot11a_list']],
				},
				returnAll,
				limit,
				100, // Conservative batch size for dot11a profiles
			);
		} else {
			// Make a single API call without pagination
			const response = await apiRequest.call(this, 'GET', endpoint, {}, {});
			console.log('Received Dot11a radio profiles');
			return formatResponse.call(this, response);
		}
	} catch (error) {
		console.log('ERROR in getAllDot11aRadioProfiles:', error);
		throw error;
	}
}

export async function getDot11aRadioProfile(
	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 Dot11a radio profile with parameters:', {
			groupNameOrGuidOrSerial,
			profileName,
		});

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

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

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

export async function updateDot11aRadioProfile(
	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 Dot11a radio profile:', {
			groupNameOrGuidOrSerial,
			profileName,
			radioProfile,
		});

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

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

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

export async function deleteDot11aRadioProfile(
	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 Dot11a radio profile with parameters:', {
			groupNameOrGuidOrSerial,
			profileName,
		});

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

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

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