package cn.reghao.tnb.message.app.service;

import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.tnb.common.util.StringRegexp;
import cn.reghao.tnb.message.app.db.mapper.EmailAccountMapper;
import cn.reghao.tnb.message.app.db.mapper.WebhookMapper;
import cn.reghao.tnb.message.app.model.po.EmailAccount;
import cn.reghao.tnb.message.app.model.po.Webhook;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author reghao
 * @date 2024-09-25 13:56:58
 */
@Service
public class EmailAccountService {
    private final int maxSize = 10;
    private final EmailAccountMapper emailAccountMapper;
    private final WebhookMapper webhookMapper;

    public EmailAccountService(EmailAccountMapper emailAccountMapper, WebhookMapper webhookMapper) {
        this.emailAccountMapper = emailAccountMapper;
        this.webhookMapper = webhookMapper;
    }

    public Result addEmailAccount(EmailAccount emailAccount) {
        if (emailAccountMapper.findAll().size() > maxSize) {
            String errMsg = String.format("可使用的邮箱数量不能超过 %s 个, 请删除一些邮箱后再尝试添加", maxSize);
            return Result.fail(errMsg);
        }

        String email = emailAccount.getUsername();
        boolean matched = StringRegexp.matchEmail(email);
        if (!matched) {
            return Result.fail("邮箱格式不正确");
        }

        EmailAccount emailAccount1 = emailAccountMapper.findByUsername(email);
        if (emailAccount1 != null) {
            String errMsg = String.format("邮箱 %s 已存在", email);
            return Result.fail(errMsg);
        }
        emailAccountMapper.save(emailAccount);
        return Result.success();
    }

    public Result deleteEmailAccount(String username) {
        EmailAccount emailAccount = emailAccountMapper.findByUsername(username);
        if (emailAccount == null) {
            String errMsg = String.format("邮箱不存在");
            return Result.fail(errMsg);
        }

        emailAccountMapper.deleteById(emailAccount.getId());
        return Result.success();
    }

    public EmailAccount getEmailSender() {
        String username = "";
        EmailAccount emailAccount = emailAccountMapper.findByUsername(username);
        return emailAccount;
    }

    public List<EmailAccount> getEmailAccountList() {
        return emailAccountMapper.findAll();
    }

    public Result addWebhook(Webhook webhook) {
        if (webhookMapper.findAll().size() > maxSize) {
            String errMsg = String.format("可使用的 webhook 数量不能超过 %s 个, 请删除一些 webhook 后再尝试添加", maxSize);
            return Result.fail(errMsg);
        }

        String name = webhook.getName();
        Webhook webhook1 = webhookMapper.findByName(name);
        if (webhook1 != null) {
            String errMsg = String.format("webhook %s 已存在", name);
            return Result.fail(errMsg);
        }
        webhookMapper.save(webhook);
        return Result.success();
    }

    public Result deleteWebhook(String name) {
        webhookMapper.deleteByName(name);
        return Result.success();
    }

    public Webhook getWebhookReceiver() {
        String name = "";
        return webhookMapper.findByName(name);
    }

    public List<Webhook> getWebhookList() {
        return webhookMapper.findAll();
    }
}
