package com.banesco.module.docservice_file_audit_trace.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.docservice_file_audit_trace.application.usecase.DomainUseCase;
import com.banesco.module.docservice_file_audit_trace.domain.dto.request.DocserviceFileAuditTraceRequest;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;

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

    @Inject
    public DomDocserviceFileAuditTraceClient(
        HttpClientUseCase httpClientUseCase,
        RestClientConfig restClientConfig
    ) {
        this.httpClientUseCase = httpClientUseCase;
        this.domainConfig = restClientConfig.getDomDocserviceFileConfig();
        log.info("Configuracion cargada para bus-docservice-file-audit-trace: {}", domainConfig);
    }

    @Override
    public <T> ApiResponse<T> execute(
        DocserviceFileAuditTraceRequest params,
        Class<T> responseType
    ) {
        String documentName = params.getDocumentName();
        HttpRequest request = HttpRequest.forApiResponse(
                domainConfig.getUrl(),
                HttpRequest.HttpMethod.POST,
                responseType
            )
            .withBody(params)
            .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 transaccion {}: {}",
                documentName,
                e.getApiResponse()
            );

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

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