package de.htwsaar.webshop.util;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * Class for often used logging requests.
 */
@Slf4j
public class LoggerUtil {

    public static Map<String, String> getRequestHeaders(HttpServletRequest request) {
        Map<String, String> headers = new HashMap<>();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            headers.put(headerName, request.getHeader(headerName));
        }
        return headers;
    }

    public static void logRequest(HttpServletRequest request) {
        log.info("Request {} on URI {} from {} with {}",
                request.getMethod(),
                request.getRequestURI(),
                request.getRemoteAddr(),
                getRequestHeaders(request)); //TODO: sanitize content when there is sensitive content
    }
}
