package cn.axzo.workflow.starter.mq.monitor;

import cn.axzo.workflow.starter.handler.monitor.BroadcastDLQReporter;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import lombok.SneakyThrows;
import org.apache.rocketmq.common.admin.TopicOffset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;

/**
 * Starter 内置的 Broadcast DLQ 钉钉通知
 *
 * @author wangli
 * @since 2024/6/19 11:04
 */
public class AlertBroadcastDLQReporter implements BroadcastDLQReporter {

    private static final Logger log = LoggerFactory.getLogger(AlertBroadcastDLQReporter.class);
    private final String dingtalk_robot_webhook = "https://oapi.dingtalk.com/robot/send?access_token=341ee2907f3ebc15dc495fb7771a646230058710999fec7838066c109849878e";
    private final String profile;
    private final String applicationName;

    public AlertBroadcastDLQReporter(Environment environment) {
        this.profile = environment.getProperty("spring.profiles.active");
        this.applicationName = environment.getProperty("spring.application.name");
    }

    @Override
    public void process(TopicOffset v, String dlqName) {
        log.warn("found DLQ");
        long count = v.getMaxOffset() - v.getMinOffset();
        if (count > 0) {
            sendDingTalk(count, dlqName);
        }
    }

    @SneakyThrows
    public void sendDingTalk(Long count, String dlqName) {
        DingTalkClient client = new DefaultDingTalkClient(dingtalk_robot_webhook);
        OapiRobotSendRequest request = new OapiRobotSendRequest();

        request.setMsgtype("markdown");
        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        markdown.setTitle("Notice Env: " + profile);
        markdown.setText("#### [" + profile + "]环境[" + applicationName + "]发现“DLQ”数据(Rpc)\n" +
                "> ###### 1. 如需查看详情，[请点击这里](" + findDomain() + "m) \n" +
                "> ###### 2. 如需调整 MQ DLQ 的监控状态: \n" +
                "> ###### 3. DLQ名称：" + dlqName + " \n" +
                "[关闭监控](" + findDomain() + "m/set?status=false) \n | " +
                "[开启监控](" + findDomain() + "m/set?status=true) \n" +
                "> 积累条数：" + count + " \n");
        request.setMarkdown(markdown);
        log.warn(markdown.getText());
        OapiRobotSendResponse response = client.execute(request);
    }

    private String findDomain() {
        switch (profile) {
            case "dev":
                return "https://dev-app.axzo.cn/" + applicationName + "/web/we/s/";
            case "test":
                return "https://test-api.axzo.cn/" + applicationName + "/web/we/s/";
            case "pre":
                return "https://pre-api.axzo.cn/" + applicationName + "/web/we/s/";
            case "live":
                return "https://live-api.axzo.cn/" + applicationName + "/web/we/s/";
            default:
                return "https://api.axzo.cn/" + applicationName + "/web/we/s/";
        }
    }
}
