package cn.reghao.devops.web.admin.sys.controller;

import cn.reghao.devops.web.admin.sys.db.repository.EmailAccountRepository;
import cn.reghao.devops.web.admin.sys.db.repository.NotifyReceiverRepository;
import cn.reghao.devops.web.admin.sys.model.vo.NotifyType;
import cn.reghao.devops.web.admin.sys.model.po.EmailAccount;
import cn.reghao.devops.web.admin.sys.model.po.NotifyReceiver;
import cn.reghao.jutil.jdk.result.WebResult;
import cn.reghao.jutil.jdk.string.StringRegexp;
import io.swagger.annotations.Api;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2024-01-26 20:32:24
 */
@Api(tags = "通知接收接口")
@RestController
@RequestMapping("/api/sys/notify/receiver")
public class ReceiverController {
    private final NotifyReceiverRepository notifyReceiverRepository;
    private final EmailAccountRepository emailAccountRepository;

    public ReceiverController(NotifyReceiverRepository notifyReceiverRepository,
                              EmailAccountRepository emailAccountRepository) {
        this.notifyReceiverRepository = notifyReceiverRepository;
        this.emailAccountRepository = emailAccountRepository;
    }

    @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_VALUE)
    public String saveReceiver(@Validated NotifyReceiver notifyReceiver) {
        String type = notifyReceiver.getType();
        NotifyReceiver notifyReceiver1 = notifyReceiverRepository.findByType(type);
        if (notifyReceiver1 != null) {
            String errMsg = String.format("NotifyType %s already exist", type);
            return WebResult.failWithMsg(errMsg);
        }

        if (type.equals(NotifyType.email.name())) {
            List<EmailAccount> list = emailAccountRepository.findAll();
            if (list.isEmpty()) {
                return WebResult.failWithMsg("没有可以发送通知的邮箱, 请先配置");
            }

            String email = notifyReceiver.getUrl();
            boolean matched = StringRegexp.matchEmail(email);
            if (!matched) {
                return WebResult.failWithMsg("邮箱地址格式不正确, 请检查");
            }
        }

        notifyReceiverRepository.save(notifyReceiver);
        return WebResult.success();
    }

    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    public String editReceiver(@Validated NotifyReceiver notifyReceiver) {
        String name = notifyReceiver.getName();
        NotifyReceiver notifyReceiver1 = notifyReceiverRepository.findByName(name);
        if (notifyReceiver1 == null) {
            String errMsg = String.format("NotifyReceiver %s not exist", name);
            return WebResult.failWithMsg(errMsg);
        }

        notifyReceiver1.setUrl(notifyReceiver.getUrl());
        notifyReceiverRepository.save(notifyReceiver1);
        return WebResult.success();
    }

    @DeleteMapping(value = "/delete/{name}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteReceiver(@PathVariable("name") String name) {
        NotifyReceiver notifyReceiver = notifyReceiverRepository.findByName(name);
        if (notifyReceiver != null) {
            notifyReceiverRepository.delete(notifyReceiver);
        }

        return WebResult.success();
    }
}
