package cokr.xit.ens.core.aop;

import cokr.xit.ens.core.utils.CmmnUtil;
import cokr.xit.ens.core.monitor.Message;
import lombok.Builder;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Builder
public class AccessErrorMessage implements Message {

    private final String ACTIVE_PROFILE;
    private final AccessLog accessLog;
    private final String serverUrl;

    public String create() {
        StringBuilder sb = new StringBuilder();
        sb.append("###############################################################")
                .append(System.getProperty("line.separator"))
                .append(String.format("####### ENS unknown error info... [ -Dspring.profiles.active=%s ] #######", CmmnUtil.isEmpty(System.getProperty("spring.profiles.active")) ? ACTIVE_PROFILE : System.getProperty("spring.profiles.active")))
                .append(System.getProperty("line.separator"))
                .append("###############################################################")
                .append(System.getProperty("line.separator"))
                .append("[Server URL]: " + serverUrl)
                .append(System.getProperty("line.separator"))
                .append("[Occurrence time]: ")
                .append(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")))
                .append(System.getProperty("line.separator"))
                .append("[Client Ip]: " + accessLog.getIp())
                .append(System.getProperty("line.separator"))
                .append("[Request Url]: " + accessLog.getHttpMethod() + " " + accessLog.getUrl())
                .append(System.getProperty("line.separator"))
                .append("[Request Param]: " + accessLog.getParam())
                .append(System.getProperty("line.separator"))
//                .append("[Details]: ")
//                .append(accessLog.getErrorMsg())
                .append(System.getProperty("line.separator"))
                .append(System.getProperty("line.separator"))
                .append("[StackTrace]:")
                .append(System.getProperty("line.separator"))
                .append(accessLog.getErrorMsg());

        return sb.toString();
    }
}
