package com.cico.nest.lcdp.boot.web.holder;

import com.cico.nest.lcdp.boot.web.core.util.WebFrameworkUtils;
import com.cico.nest.lcdp.framework.common.context.LcdpGlobalContext;
import com.cico.nest.lcdp.framework.common.util.monitor.TracerUtils;
import jakarta.servlet.http.HttpServletRequest;

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

public class LcdpGlobalContextHolder extends LcdpGlobalContext {
    public static void init(HttpServletRequest request, LocalDateTime requestTime) {
        LcdpGlobalContextHolder.setTraceId(TracerUtils.getTraceId());
        LcdpGlobalContextHolder.setReqDatetime(requestTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        LcdpGlobalContextHolder.setReqDate(requestTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        LcdpGlobalContextHolder.setReqTimestamp(requestTime.toInstant(ZoneOffset.of("+8")).toEpochMilli());
        LcdpGlobalContextHolder.setReqPath(request.getRequestURI());
        LcdpGlobalContextHolder.setReqHeaderMap(WebFrameworkUtils.getHeadersMap(request));
        LcdpGlobalContextHolder.setReqParams(WebFrameworkUtils.getReqParamsMap(request));
    }

}
