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

export const giteaDescriptor: ProviderDescriptor = {
	type: 'gitea',
	defaultName: 'Gitea',
	icon: 'mdiGit',
	hasUrl: true,
	urlPlaceholder: 'https://gitea.example.com',
	webhookBased: true,

	configFields: [
		{
			key: 'webhook_secret', configKey: 'webhook_secret',
			label: 'providers.webhookSecret', editLabel: 'providers.webhookSecretKeep',
			type: 'password', required: 'create-only', hint: 'providers.webhookSecretHint',
		},
		{
			key: 'api_token', configKey: 'api_token',
			label: 'providers.apiToken',
			type: 'password', optional: true, hint: 'providers.apiTokenHint',
		},
	],

	buildConfig(form, editing) {
		const config: Record<string, any> = { url: form.url };
		if (form.api_token) config.api_token = form.api_token;
		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_token || !!form.webhook_secret;
	},

	eventFields: [
		{ key: 'track_push', label: 'trackingConfig.push', default: true },
		{ key: 'track_issue_opened', label: 'trackingConfig.issueOpened', default: true },
		{ key: 'track_issue_closed', label: 'trackingConfig.issueClosed', default: true },
		{ key: 'track_issue_commented', label: 'trackingConfig.issueCommented', default: false },
		{ key: 'track_pr_opened', label: 'trackingConfig.prOpened', default: true },
		{ key: 'track_pr_closed', label: 'trackingConfig.prClosed', default: true },
		{ key: 'track_pr_merged', label: 'trackingConfig.prMerged', default: true },
		{ key: 'track_pr_commented', label: 'trackingConfig.prCommented', default: false },
		{ key: 'track_release_published', label: 'trackingConfig.releasePublished', default: true },
	],

	collectionMeta: {
		label: 'notificationTracker.repositories',
		icon: 'mdiGit',
		placeholder: 'notificationTracker.selectRepositories',
		countLabel: 'notificationTracker.repos_count',
		desc: () => '',
	},

	userFilters: [
		{
			key: 'senders',
			label: 'notificationTracker.userAllowlist',
			placeholder: 'notificationTracker.selectUsers',
			icon: 'mdiAccountCheck',
		},
		{
			key: 'exclude_senders',
			label: 'notificationTracker.userBlocklist',
			placeholder: 'notificationTracker.selectUsers',
			icon: 'mdiAccountOff',
		},
	],

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