package com.banesco.commons.exceptions;

import com.banesco.common.domain.dto.bian.response.StatusResponse;
import com.banesco.commons.config.StatusCodes;
import com.banesco.domain.bian.response.RecUpdateCardStatusResponse;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.RoutingContext;
import jakarta.inject.Inject;
import jakarta.json.bind.JsonbException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
import lombok.extern.slf4j.Slf4j;

@Provider
@Slf4j
public class MalformedPayloadExceptionMapper implements ExceptionMapper<JsonbException> {

    @Inject
    RoutingContext routingContext; // Injecting the RoutingContext

    @Override
    public Response toResponse(JsonbException exception) {

        HttpServerRequest headers = routingContext.request();
        log.error("INICIO ERROR => {}", exception.getMessage());
        log.error("[HEADERS {}]: [\nfintechId: {}\nappId: {}]",
                headers.getHeader("fintechId"),
                headers.getHeader("appId"));


        String jsonResult = (new RecUpdateCardStatusResponse( new StatusResponse(
                "error",
                StatusCodes.INVALID_FORMAT.getStatusCode(),
                StatusCodes.INVALID_FORMAT.getStatusDesc().formatted("body"),
                null
        ))).toString();
        
        log.error("[RESPONSE]: [{}]", jsonResult);
        return Response.status(Response.Status.BAD_REQUEST)
                .entity(
                        jsonResult
                )
                .build();
    }
}