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

const params: NotifierParam[] = [
{ key: 'host', label: 'SMTP 服务器', type: 'text', required: true, placeholder: 'smtp.qq.com' },
    { key: 'port', label: '端口', type: 'text', default: 465, required: false },
    { key: 'secure', label: 'SSL', type: 'text', default: 'true', required: false },
    { key: 'user', label: '用户名', type: 'text', required: true, placeholder: 'user@example.com' },
    { key: 'pass', label: '密码/授权码', type: 'password', required: true, placeholder: 'SMTP 授权码' },
    { key: 'from', label: '发件人', type: 'text', required: true, placeholder: 'sender@example.com' },
    { key: 'to', label: '收件人', type: 'text', required: true, placeholder: 'receiver@example.com' },
    { key: 'title', label: '标题', type: 'text', default: 'CloudSearch', required: false },
    { key: 'content', label: '内容', type: 'text', required: true },
];

export const smtpNotifier: Notifier = {
name: 'smtp',
    label: 'SMTP 邮件',
    params,
    async notify(params) {
        try {
            const nodemailer = require('nodemailer');
            const transporter = nodemailer.createTransport({
                host: params.host,
                port: params.port || 465,
                secure: params.secure !== false,
                auth: { user: params.user, pass: params.pass },
            });
            await transporter.sendMail({
                from: params.from,
                to: params.to,
                subject: `[CloudSearch] ${params.title || ''}`,
                text: params.content || '',
            });
            return { success: true, message: 'SMTP 邮件发送成功' };
        }
        catch (err: any) {
            return { success: false, message: err.message };
        }
    },
};
