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

import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.message.app.model.po.EmailAccount;
import cn.reghao.tnb.message.app.model.po.Webhook;
import cn.reghao.tnb.message.app.service.EmailAccountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2023-10-30 21:32:37
 */
@Tag(name = "message 管理接口")
@RestController
@RequestMapping("/api/admin/message")
public class AdminMessageController {
    private final EmailAccountService emailAccountService;

    public AdminMessageController(EmailAccountService emailAccountService) {
        this.emailAccountService = emailAccountService;
    }

    @Operation(summary = "添加邮件帐号", description = "N")
    @PostMapping(value = "/notify/email", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addEmailAccount(@RequestBody @Validated EmailAccount emailAccount) {
        Result result = emailAccountService.addEmailAccount(emailAccount);
        return WebResult.result(result);
    }

    @Operation(summary = "删除邮件帐号", description = "N")
    @DeleteMapping(value = "/notify/email", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteEmailAccount() {
        Result result = emailAccountService.deleteEmailAccount("");
        return WebResult.result(result);
    }

    @Operation(summary = "邮件帐号列表", description = "N")
    @GetMapping(value = "/notify/email", produces = MediaType.APPLICATION_JSON_VALUE)
    public String emailAccountList() {
        List<EmailAccount> list = emailAccountService.getEmailAccountList();
        return WebResult.success(list);
    }

    @Operation(summary = "添加 webhook", description = "N")
    @PostMapping(value = "/notify/webhook", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addWebhook(@RequestBody @Validated Webhook webhook) {
        Result result = emailAccountService.addWebhook(webhook);
        return WebResult.result(result);
    }

    @Operation(summary = "删除 webhook", description = "N")
    @DeleteMapping(value = "/notify/webhook", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteWebhook() {
        Result result = emailAccountService.deleteWebhook("");
        return WebResult.result(result);
    }

    @Operation(summary = "webhook 列表", description = "N")
    @GetMapping(value = "/notify/webhook", produces = MediaType.APPLICATION_JSON_VALUE)
    public String webhookList() {
        List<Webhook> list = emailAccountService.getWebhookList();
        return WebResult.success(list);
    }
}
