/**
 * Author: Marco Visin
 * Date: 2025-06-02
 * Updated to support Bearer token authentication
 */

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

export class InNotesApi implements ICredentialType {
	name = 'inNotesApi';
	displayName = 'InNotes API';
	documentationUrl = 'https://innotes.me';
	icon: Icon = 'file:Logo_128.png';
	properties: INodeProperties[] = [
		{
			displayName: 'Base URL',
			name: 'baseUrl',
			type: 'string',
			default: 'https://innotes.me',
			description: 'The base URL of your InNotes instance',
			required: true,
		},
		{
			displayName: 'Bearer Token',
			name: 'token',
			type: 'string',
			typeOptions: {
				password: true,
			},
			default: '',
			description: 'Your InNotes API Bearer token. Generate one in your dashboard settings.',
			required: true,
		},
		{
			displayName: 'Callback Secret',
			name: 'callbackSecret',
			type: 'string',
			typeOptions: {
				password: true,
			},
			default: '',
			// The old text said "Found in your InNotes dashboard automation
			// settings". That screen exists (app/dashboard/profile/AutomationSettings)
			// and renders no secret — the value is injected server-side when InNotes
			// provisions the credential, and the identifier appears nowhere under
			// app/ or components/. So the instruction could not be followed by
			// anyone setting this up by hand, which is the only case it addressed.
			//
			// It does NOT say "optional, leave it empty" either: the node throws
			// without it (InNotes.node.ts checks callbackSecret and raises
			// NodeApiError before building the request), so that advice would break
			// the very action the field exists for.
			// Author: Marco — 2026-08-03
			// Nor does it say the VALUE has to be obtained from somewhere. Since the
			// callback started authenticating with the API token above, the server
			// compares this secret only when no token is present — and the node
			// always sends one. What still matters is that the field is non-empty,
			// because the node checks that locally before it builds the request.
			// Saying otherwise stranded anyone whose credential predates this field:
			// provisioning writes it once and there is no back-fill.
			description:
				'Required for the "Report Job Created" action — the node refuses to run without a ' +
				'value. InNotes fills it in automatically when it provisions your automation. If you ' +
				'are setting this credential up by hand, any non-empty value will do: the callback ' +
				'authenticates with the API token above.',
		},
		{
			displayName: 'Allowed Domains',
			name: 'allowedDomains',
			type: 'string',
			default: '*',
			description: 'Domains allowed to use this credential (use * for all)',
		},
	];

	authenticate: IAuthenticateGeneric = {
		type: 'generic',
		properties: {
			headers: {
				'Authorization': '=Bearer {{$credentials.token}}',
			},
		},
	};

	test: ICredentialTestRequest = {
		request: {
			baseURL: '={{$credentials.baseUrl}}',
			url: '/api/user',
			method: 'GET',
		},
	};
}
