package application.usecase;

import application.exception.GatewayServiceException;
import com.banesco.common.infraestructure.helpers.LoggerHelper;
import domain.dto.request.CustomerRequest;
import domain.model.ExecutionResult;
import infrastructure.client.accounts.adapter.CustomerProductAdapter;
import infrastructure.service.StatusDescriptionResolver;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ApplicationScoped
public class CustomerProductUseCase {
    private final CustomerProductAdapter customerProductAdapter;
    private final StatusDescriptionResolver statusDescriptionResolver;

    @Inject
    public CustomerProductUseCase(
            CustomerProductAdapter customerProductAdapter,
            StatusDescriptionResolver statusDescriptionResolver
    ) {
        this.customerProductAdapter = customerProductAdapter;
        this.statusDescriptionResolver = statusDescriptionResolver;
    }

    public ExecutionResult getCustomerProduct(
            CustomerRequest request,
            String requestId,
            String appId,
            String customerReferenceFintechId,
            String customerReferenceUser
    ) {
        log.debug(LoggerHelper.buildInfoPrivateRequest(requestId, "Iniciando caso de uso: CustomerProductUseCase"));
        try {
            return customerProductAdapter.getCustomerProduct(
                    request,
                    requestId,
                    appId,
                    customerReferenceFintechId,
                    customerReferenceUser
            );
        } catch (GatewayServiceException ex) {
            log.error(LoggerHelper.buildError(requestId, "Fallo la comunicacion con el servicio: " + ex.getMessage()));
            var error = statusDescriptionResolver.getCode("503-URL");
            return ExecutionResult.failure(error);
        } finally {
            log.debug(LoggerHelper.buildInfoPrivateRequest(requestId, "Finalizado caso de uso: CustomerProductUseCase"));
        }
    }
}