import { Notifier, NotifyParams, NotifyResult, NotifierParam } from './notifier.types';

const params: NotifierParam[] = [
{ key: 'webhook_url', label: 'Webhook URL', type: 'url', required: true, placeholder: 'https://discord.com/api/webhooks/...' },
    { key: 'title', label: '标题', type: 'text', default: 'CloudSearch', required: false },
    { key: 'content', label: '内容', type: 'text', required: true },
    { key: 'level', label: '级别', type: 'text', default: 'info', required: false },
];

export const discordNotifier: Notifier = {
name: 'discord',
    label: 'Discord',
    params,
    async notify(params) {
        try {
            const level = params.level || 'info';
            const color = level === 'error' ? 0xff0000 : level === 'warn' ? 0xffa500 : 0x3498db;
            const resp = await fetch(params.webhook_url, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    embeds: [{
                            title: params.title || 'CloudSearch',
                            description: params.content || '',
                            color,
                            footer: { text: 'CloudSearch \u00b7 ' + new Date().toLocaleString('zh-CN') },
                        }],
                }),
            });
            if (resp.ok)
                return { success: true, message: 'Discord 推送成功' };
            return { success: false, message: `HTTP ${resp.status}` };
        }
        catch (err: any) {
            return { success: false, message: err.message };
        }
    },
};
