import {
	IAuthenticateGeneric,
	ICredentialTestRequest,
	ICredentialType,
	INodeProperties,
} from 'n8n-workflow';

export class LibreNmsApi implements ICredentialType {
	name = 'libreNmsApi';
	displayName = 'LibreNMS API';
	documentationUrl = 'https://docs.librenms.org/API/';
	properties: INodeProperties[] = [
		{
			displayName: 'LibreNMS URL',
			name: 'url',
			type: 'string',
			default: '',
			placeholder: 'https://librenms.example.com',
			required: true,
			description: 'The URL of your LibreNMS instance',
		},
		{
			displayName: 'API Token',
			name: 'apiToken',
			type: 'string',
			typeOptions: {
				password: true,
			},
			default: '',
			required: true,
			description: 'API token generated in LibreNMS at /api-access/',
		},
		{
			displayName: 'Ignore SSL Issues',
			name: 'allowUnauthorizedCerts',
			type: 'boolean',
			default: false,
			description: 'Whether to connect even if SSL certificate validation is not possible',
		},
	];

	authenticate: IAuthenticateGeneric = {
		type: 'generic',
		properties: {
			headers: {
				'X-Auth-Token': '={{$credentials.apiToken}}',
				Accept: 'application/json',
				'Content-Type': 'application/json',
			},
		},
	};

	test: ICredentialTestRequest = {
		request: {
			baseURL: '={{$credentials.url.replace(/\\/$/, "")}}',
			url: '/api/v0/system',
			method: 'GET',
			skipSslCertificateValidation: '={{$credentials.allowUnauthorizedCerts}}',
		},
	};
}
