package com.banesco.module.service_issue_payment_status.infrastructure.client;

import com.banesco.common.application.usecase.HttpClientUseCase;
import com.banesco.common.domain.exception.HttpApiResponseException;
import com.banesco.common.domain.exception.HttpStatusCodeException;
import com.banesco.common.domain.model.ApiResponse;
import com.banesco.common.domain.model.BusinessConfig;
import com.banesco.common.domain.model.HttpRequest;
import com.banesco.common.infrastructure.config.RestClientConfig;
import com.banesco.module.service_issue_payment_status.application.usecase.BusinessUseCase;
import com.banesco.module.service_issue_payment_status.domain.dto.request.ServiceIssuePaymentStatusRequest;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ApplicationScoped
public class BusServiceIssuePaymentStatusClient implements BusinessUseCase {
    private final HttpClientUseCase httpClientUseCase;
    private final BusinessConfig businessConfig;

    @Inject
    public BusServiceIssuePaymentStatusClient(
        HttpClientUseCase httpClientUseCase,
        RestClientConfig restClientConfig
    ) {
        this.httpClientUseCase = httpClientUseCase;
        this.businessConfig = restClientConfig.getBusLegalCustomerConfig();
        log.info("Configuracion cargada para bus-service-issue-payment-status: {}", businessConfig);
    }

    @Override
    public <T> ApiResponse<T> execute(
        ServiceIssuePaymentStatusRequest params,
        Class<T> responseType
    ) {
        String paymentStatusId = params.getId();
        HttpRequest request = HttpRequest.forApiResponse(
                businessConfig.getUrl(),
                HttpRequest.HttpMethod.GET,
                responseType
            )
            .withPathParams(params.toParams())
            .withQueryParams(params.toQueryString())
            .withTimeout(
                businessConfig.getTimeout().getConnect(),
                businessConfig.getTimeout().getResponse()
            );

        try {
            ApiResponse<T> response = httpClientUseCase.executeApiResponse(request);

            log.info(
                "Solicitud del api de negocio exitoso: {}",
                response.getStatusResponse()
            );

            return response;
        } catch (HttpApiResponseException e) {
            log.error(
                "Error HTTP con ApiResponse consultando transaccion {}: {}",
                paymentStatusId,
                e.getApiResponse()
            );

            throw new HttpStatusCodeException(
                e.getStatusCode(),
                e.getApiResponse().getStatusResponse().getStatusCode()
            );
        } catch (HttpStatusCodeException e) {
            log.error(
                "Error HTTP consultando transaccion {}: {} - {}",
                paymentStatusId,
                e.getStatusCode(),
                e.getMessage()
            );

            throw e;
        } catch (Exception e) {
            log.error("Error consultando transaccion {}: {}", paymentStatusId, e.getMessage());
            throw HttpStatusCodeException.serviceUnavailable("503");
        }
    }
}