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

import com.banesco.common.application.helper.MessageHelper;
import com.banesco.common.domain.exception.*;
import com.banesco.common.domain.model.*;
import com.banesco.module.legal_customer_product_directory.application.repository.PersistenceRepository;
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 io.netty.util.internal.StringUtil;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ApplicationScoped
public class LegalCustomerProductDirectoryService implements LegalCustomerProductDirectoryUseCase {

    private final MessageHelper messageHelper;
    private final PersistenceRepository persistenceRepository;

    @Inject
    public LegalCustomerProductDirectoryService(
        MessageHelper messageHelper,
        PersistenceRepository persistenceRepository
    ) {
        this.messageHelper = messageHelper;
        this.persistenceRepository = persistenceRepository;
    }

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

        Response response;

        try {
            if(StringUtil.isNullOrEmpty(request.getCustomerId())) {
                log.error("Error en el codigo del cliente vacio");
                throw HttpStatusCodeException.badRequest("VRN01");
            }

            ApiResponse<LegalCustomerProductDirectoryResponse> apiResponse = storeProcedure(request);

            response = messageHelper.handleSuccess(
                apiResponse.getData(),
                apiResponse.getStatusResponse().getStatusCode()
            );
        } catch (PersistenceException e) {
            log.warn("Excepcion de la persistencia del sp: {} -> {}", e.getErrorCode(), e.getMessage());
            response = messageHelper.handleException(HttpStatusCodeException.badRequest(e.getErrorCode()));
        } catch (HttpStatusCodeException e) {
            log.error("Excepcion HTTP del api privada: {} - {}", e.getStatusCode(), e.getErrorCode());
            response = messageHelper.handleException(e);
        } catch (Exception e) {
            log.error("Excepcion generica del api privada: {}", e.getMessage());
            response = messageHelper.handleGenericException(e);
        }

        return response;
    }

    private ApiResponse<LegalCustomerProductDirectoryResponse> storeProcedure(
        LegalCustomerProductDirectoryRequest request
    ) {
        log.info("Calling persistence repository for client: {}", request.getCustomerId());

        return new ApiResponse<>(
            LegalCustomerProductDirectoryResponse.builder()
                .customerProductAndServiceDirectory(persistenceRepository.execute(request))
                .build(),
            messageHelper.createStatusResponse("200")
        );
    }
}