package com.banesco.module.legal_customer_product_directory.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.DomainConfig;
import com.banesco.common.domain.model.HttpRequest;
import com.banesco.common.infrastructure.config.RestClientConfig;
import com.banesco.module.legal_customer_product_directory.application.usecase.DomainUseCase;
import com.banesco.module.legal_customer_product_directory.domain.dto.request.LegalCustomerProductDirectoryRequest;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
@ApplicationScoped
public class DomLegalCustomerProductDirectoryClient implements DomainUseCase {
    private final HttpClientUseCase httpClientUseCase;
    private final DomainConfig domainConfig;

    @Inject
    public DomLegalCustomerProductDirectoryClient(
        HttpClientUseCase httpClientUseCase,
        RestClientConfig restClientConfig
    ) {
        this.httpClientUseCase = httpClientUseCase;
        this.domainConfig = restClientConfig.getDomLegalCustomerConfig();
        log.info("Configuracion cargada para bus-legal-customer-directory: {}", domainConfig);
    }

    @Override
    public <T> ApiResponse<T> execute(
        LegalCustomerProductDirectoryRequest params,
        Class<T> responseType
    ) {
        String customerId = params.getCustomerId();
        HttpRequest request = HttpRequest.forApiResponse(
                domainConfig.getUrl(),
                HttpRequest.HttpMethod.GET,
                responseType
            )
            .withPathParams(Map.of("customerId", customerId))
            .withQueryParams(params.toQueryString())
            .withTimeout(
                domainConfig.getTimeout().getConnect(),
                domainConfig.getTimeout().getResponse()
            );

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

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

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

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

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