import type { ProviderDescriptor } from './types';

export const plankaDescriptor: ProviderDescriptor = {
	type: 'planka',
	defaultName: 'Planka',
	icon: 'mdiViewDashboard',
	hasUrl: true,
	urlPlaceholder: 'https://planka.example.com',
	webhookBased: true,

	configFields: [
		{
			key: 'webhook_secret', configKey: 'webhook_secret',
			label: 'providers.webhookSecret', editLabel: 'providers.webhookSecretKeep',
			type: 'password', required: 'create-only', hint: 'providers.plankaWebhookSecretHint',
		},
		{
			key: 'api_key', configKey: 'api_key',
			label: 'providers.apiKey',
			type: 'password', optional: true, hint: 'providers.plankaApiKeyHint',
		},
	],

	buildConfig(form, editing) {
		const config: Record<string, any> = { url: form.url };
		if (form.api_key) config.api_key = form.api_key;
		if (form.webhook_secret) config.webhook_secret = form.webhook_secret;
		if (!editing && !form.webhook_secret) {
			return { config, error: 'providers.webhookSecretRequired' };
		}
		return { config };
	},

	hasConfigChanged(form, existing) {
		return form.url !== (existing.url || '') ||
			!!form.api_key || !!form.webhook_secret;
	},

	eventFields: [
		{ key: 'track_card_created', label: 'trackingConfig.cardCreated', default: true },
		{ key: 'track_card_updated', label: 'trackingConfig.cardUpdated', default: false },
		{ key: 'track_card_moved', label: 'trackingConfig.cardMoved', default: true },
		{ key: 'track_card_deleted', label: 'trackingConfig.cardDeleted', default: false },
		{ key: 'track_card_commented', label: 'trackingConfig.cardCommented', default: true },
		{ key: 'track_comment_updated', label: 'trackingConfig.commentUpdated', default: false },
		{ key: 'track_board_created', label: 'trackingConfig.boardCreated', default: true },
		{ key: 'track_board_updated', label: 'trackingConfig.boardUpdated', default: false },
		{ key: 'track_board_deleted', label: 'trackingConfig.boardDeleted', default: true },
		{ key: 'track_list_created', label: 'trackingConfig.listCreated', default: false },
		{ key: 'track_list_updated', label: 'trackingConfig.listUpdated', default: false },
		{ key: 'track_list_deleted', label: 'trackingConfig.listDeleted', default: false },
		{ key: 'track_attachment_created', label: 'trackingConfig.attachmentCreated', default: true },
		{ key: 'track_card_label_added', label: 'trackingConfig.cardLabelAdded', default: false },
		{ key: 'track_task_completed', label: 'trackingConfig.taskCompleted', default: true },
	],

	collectionMeta: {
		label: 'notificationTracker.boards',
		icon: 'mdiViewDashboard',
		placeholder: 'notificationTracker.selectBoards',
		countLabel: 'notificationTracker.boards_count',
		desc: () => '',
	},

	webhookUrlPattern: '/api/webhooks/planka/{token}',
};
