package com.banesco.module.legal_customer_product_directory.application.service;

import com.banesco.common.application.helper.MessageHelper;
import com.banesco.common.application.helper.RequestValidatorHelper;
import com.banesco.common.domain.exception.HttpStatusCodeException;
import com.banesco.common.domain.model.*;
import com.banesco.module.legal_customer_product_directory.application.usecase.BusinessUseCase;
import com.banesco.module.legal_customer_product_directory.application.usecase.LegalCustomerProductDirectoryUseCase;
import com.banesco.module.legal_customer_product_directory.domain.dto.request.LegalCustomerProductDirectoryRequest;
import com.banesco.module.legal_customer_product_directory.domain.dto.response.LegalCustomerProductDirectoryResponse;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

@Slf4j
@ApplicationScoped
public class LegalCustomerProductDirectoryService implements LegalCustomerProductDirectoryUseCase {

    private final MessageHelper messageHelper;
    private final RequestValidatorHelper requestValidatorHelper;
    private final BusinessUseCase businessUseCase;

    @Inject
    public LegalCustomerProductDirectoryService(
        MessageHelper messageHelper,
        RequestValidatorHelper requestValidatorHelper,
        BusinessUseCase businessUseCase
    ) {
        this.messageHelper = messageHelper;
        this.requestValidatorHelper = requestValidatorHelper;
        this.businessUseCase = businessUseCase;
    }

    @Override
    public ApiResponse<LegalCustomerProductDirectoryResponse> execute(
        LegalCustomerProductDirectoryRequest request
    ) {
        log.info("Iniciando ejecucion para el cliente: {}", request.getCustomerId());

        validate(request);

        try {
            ApiResponse<LegalCustomerProductDirectoryResponse> response = business(request);

            if (
                !Objects.isNull(response.getData()) &&
                messageHelper.isSuccessStatusCode(response.getStatusResponse())
            ) {
                return new ApiResponse<>(response.getData(), messageHelper.createStatusResponse(
                    response.getStatusResponse().getStatusCode()
                ));
            }

            throw HttpStatusCodeException.serviceUnavailable("503");
        } catch (HttpStatusCodeException e) {
            log.error("Excepcion HTTP del api de negocio: {} - {}", e.getStatusCode(), e.getErrorCode());
            throw e;
        } catch (Exception e) {
            log.error("Excepcion generica del api de negocio: {}", e.getMessage());
            throw e;
        }
    }

    private void validate(
        LegalCustomerProductDirectoryRequest request
    ) {
        requestValidatorHelper.validateRequired(request);
    }

    private ApiResponse<LegalCustomerProductDirectoryResponse> business(
        LegalCustomerProductDirectoryRequest request
    ) {
        log.info("Ejecutando llamada al api de negocio: {}", request.getCustomerId());
        return businessUseCase.execute(request, LegalCustomerProductDirectoryResponse.class);
    }
}