package infrastructure.filter;

import com.banesco.common.infraestructure.helpers.LoggerHelper;
import infrastructure.config.AppConfig;
import infrastructure.context.TraceIdContext;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.ext.Provider;
import lombok.extern.slf4j.Slf4j;

import java.util.stream.Collectors;


@Slf4j
@Provider
@Priority(200)
@ApplicationScoped
public class HeadersFilter implements ContainerRequestFilter {

    @Inject
    AppConfig appConfig;

    @Inject
    TraceIdContext traceIdContext;

    @Override
    public void filter(ContainerRequestContext requestContext) {
        var requestId = traceIdContext.getTraceId();

        var multiMappedHeaders = requestContext.getHeaders();

        var headersMap = multiMappedHeaders.keySet().stream()
                .collect(Collectors.toMap(
                        key -> key,
                        multiMappedHeaders::getFirst
                ));

        log.info(LoggerHelper.buildInfoPrivateRequest(requestId, LoggerHelper.formatHeaders(headersMap)));
    }
}