// methods/configuration/ap/sshCredentials.methods.ts
import { IExecuteFunctions, INodeExecutionData, IDataObject } from 'n8n-workflow';
import { apiRequest } from '../../../helpers/apiRequest';
import { formatResponse } from '../../../helpers/responseFormatter';
import { APCredential } from './apConfiguration.types';

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

		// Log the request
		console.log('Getting group SSH credentials with parameters:', { groupName });

		// Build the endpoint
		const endpoint = `/configuration/v1/group/ssh_credential/${groupName}`;

		// Make API call
		const response = await apiRequest.call(this, 'GET', endpoint, {}, {});
		console.log('Received group SSH credentials');

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

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

		// Parse the SSH credentials JSON
		let sshCredentials: APCredential;
		try {
			sshCredentials = JSON.parse(sshCredentialsJson);
		} catch (parseError) {
			throw new Error(`Invalid JSON format for SSH credentials: ${parseError.message}`);
		}

		// Log the request
		console.log('Updating group SSH credentials:', {
			groupName,
			username: sshCredentials.username,
		});

		// Build the endpoint
		const endpoint = `/configuration/v1/group/ssh_credential/${groupName}`;

		// Make API call
		const response = await apiRequest.call(
			this,
			'POST',
			endpoint,
			sshCredentials as IDataObject,
			{},
		);
		console.log('Group SSH credentials updated successfully');

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

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

		// Log the request
		console.log('Getting device SSH credentials with parameters:', { serialNumberOrGuid });

		// Build the endpoint
		const endpoint = `/configuration/v1/device/ssh_credential/${serialNumberOrGuid}`;

		// Make API call
		const response = await apiRequest.call(this, 'GET', endpoint, {}, {});
		console.log('Received device SSH credentials');

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

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

		// Parse the SSH credentials JSON
		let sshCredentials: APCredential;
		try {
			sshCredentials = JSON.parse(sshCredentialsJson);
		} catch (parseError) {
			throw new Error(`Invalid JSON format for SSH credentials: ${parseError.message}`);
		}

		// Log the request
		console.log('Updating device SSH credentials:', {
			serialNumberOrGuid,
			username: sshCredentials.username,
		});

		// Build the endpoint
		const endpoint = `/configuration/v1/device/ssh_credential/${serialNumberOrGuid}`;

		// Make API call
		const response = await apiRequest.call(
			this,
			'POST',
			endpoint,
			sshCredentials as IDataObject,
			{},
		);
		console.log('Device SSH credentials updated successfully');

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