import { ICredentialType, NodePropertyTypes } from 'n8n-workflow';

// credentials/ArubaCentralOAuth2Api.credentials.ts
export class ArubaCentralOAuth2Api implements ICredentialType {
	name = 'ArubaCentralOAuth2Api';
	displayName = 'Aruba Central OAuth2 API';
	documentationUrl = 'arubacentral';

	// These properties are essential for proper token storage in n8n
	// We're keeping our custom flow but declaring we store tokens like OAuth2
	__defaults = {
		tokenType: 'Bearer',
		tokenExpiredStatusCode: 401,
		tokenPropertyName: 'access_token',
	};

	properties = [
		{
			displayName: 'Base URL',
			name: 'baseUrl',
			type: 'string' as NodePropertyTypes,
			default: '',
			placeholder: 'https://apigw-prod2.central.arubanetworks.com',
			required: true,
			description: 'The domain base URL for HPE Aruba Networking Central API Gateway.',
		},
		{
			displayName: 'Authentication Method',
			name: 'authMethod',
			type: 'options' as NodePropertyTypes,
			options: [
				{
					name: 'OAuth2 (Username / Password)',
					value: 'oauth2',
				},
				{
					name: 'Bearer Token',
					value: 'bearerToken',
				},
			],
			default: 'oauth2',
			description: 'Authentication method to use when connecting to Aruba Central.',
		},
		// ── OAuth2 fields ──────────────────────────────────────────────────────
		{
			displayName: 'Client ID',
			name: 'clientId',
			type: 'string' as NodePropertyTypes,
			default: '',
			description: 'The OAuth client ID from API Gateway.',
			displayOptions: {
				show: {
					authMethod: ['oauth2'],
				},
			},
		},
		{
			displayName: 'Client Secret',
			name: 'clientSecret',
			type: 'string' as NodePropertyTypes,
			default: '',
			description: 'The OAuth client secret from API Gateway.',
			displayOptions: {
				show: {
					authMethod: ['oauth2'],
				},
			},
		},
		{
			displayName: 'Username',
			name: 'username',
			type: 'string' as NodePropertyTypes,
			default: '',
			description: 'Username for HPE Aruba Networking Central.',
			displayOptions: {
				show: {
					authMethod: ['oauth2'],
				},
			},
		},
		{
			displayName: 'Password',
			name: 'password',
			type: 'string' as NodePropertyTypes,
			typeOptions: {
				password: true,
			},
			default: '',
			description: 'Password for HPE Aruba Networking Central.',
			displayOptions: {
				show: {
					authMethod: ['oauth2'],
				},
			},
		},
		{
			displayName: 'Customer ID',
			name: 'customerId',
			type: 'string' as NodePropertyTypes,
			default: '',
			description: 'HPE Aruba Networking Central Customer ID.',
			displayOptions: {
				show: {
					authMethod: ['oauth2'],
				},
			},
		},
		// ── Bearer Token fields ────────────────────────────────────────────────
		{
			displayName: 'Bearer Token',
			name: 'bearerToken',
			type: 'string' as NodePropertyTypes,
			typeOptions: {
				password: true,
			},
			default: '',
			description: 'Static Bearer token for Aruba Central API authentication.',
			displayOptions: {
				show: {
					authMethod: ['bearerToken'],
				},
			},
		},
	];
}
