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

import com.banesco.common.application.usecase.HttpClientUseCase;
import com.banesco.common.domain.exception.HttpStatusCodeException;
import com.banesco.common.domain.model.Either;
import com.banesco.common.domain.model.HttpRequest;
import com.banesco.common.infrastructure.config.RestClientConfig;
import com.banesco.module.service_order_status.application.usecase.ServiceOrderStatusUseCase;
import com.banesco.module.service_order_status.domain.dto.request.ServiceOrderStatusRequest;
import com.banesco.common.domain.model.ServiceOrderStatusConfig;
import com.banesco.module.service_order_status.domain.model.Account;
import com.banesco.module.service_order_status.domain.model.Bankinfo;
import com.banesco.module.msg_request_header.domain.model.MsgRqHdr;
import com.banesco.module.service_order_status.domain.model.Service;
import com.banesco.module.service_order_status_control.domain.dto.request.ServiceOrderStatusControlRequest;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ApplicationScoped
public class ServiceOrderStatusClient implements ServiceOrderStatusUseCase {
    private final HttpClientUseCase httpClientUseCase;
    private final ServiceOrderStatusConfig serviceOrderStatusConfig;

    @Inject
    public ServiceOrderStatusClient(
        HttpClientUseCase httpClientUseCase,
        RestClientConfig restClientConfig
    ) {
        this.httpClientUseCase = httpClientUseCase;
        this.serviceOrderStatusConfig = restClientConfig.getServiceOrderStatusConfig();
        log.info("Configuracion cargada para security-trace: {}", serviceOrderStatusConfig);
    }

    @Override
    public <T, R> Either<T, R> execute(
        ServiceOrderStatusControlRequest apiRequest,
        Class<T> responseType,
        Class<R> errorType
    ) {
        ServiceOrderStatusRequest body = getRequest(apiRequest);
        HttpRequest request = HttpRequest.forDirectResponse(
                serviceOrderStatusConfig.getUrl(),
                HttpRequest.HttpMethod.POST,
                responseType,
                errorType
            )
            .withBody(body)
            .withTimeout(
                serviceOrderStatusConfig.getTimeout().getConnect(),
                serviceOrderStatusConfig.getTimeout().getResponse()
            );

        try {
            Either<T, R> response = httpClientUseCase.executeEither(request);

            log.info(
                "Solicitud de lista negra exitosa: {}",
                response.isLeft() && response.getLeft().toString().contains("msgRsHdr=")
            );

            return response;
        } catch (HttpStatusCodeException e) {
            log.error(
                "Error HTTP ejecutando lista negra: {} - {}",
                e.getStatusCode(),
                e.getMessage()
            );

            throw e;
        } catch (Exception e) {
            log.error("Error ejecutando lista negra: {}", e.getMessage());
            throw HttpStatusCodeException.serviceUnavailable("503");
        }
    }

    private ServiceOrderStatusRequest getRequest(
        ServiceOrderStatusControlRequest apiRequest
    ) {
        MsgRqHdr msgRqHdr = serviceOrderStatusConfig
            .getRequest()
            .getMsgRqHdr()
            .withOptions(apiRequest.getDevice());

        return ServiceOrderStatusRequest.builder()
            .MsgRqHdr(msgRqHdr)
            .Account(
                Account.builder()
                    .Bankinfo(
                        Bankinfo.builder()
                            .BankId(apiRequest.getBankId())
                            .build()
                    )
                    .Service(
                        Service.builder()
                            .SvcCode(apiRequest.getServiceCode())
                            .SvcType(apiRequest.getServiceType())
                            .SvcDesc(apiRequest.getServiceDescription())
                            .SvcObser(apiRequest.getServiceObserver())
                            .build()
                    )
                    .build()
            )
            .build();
    }
}