package com.banesco.module.payment_instruction_request.infrastructure.adapter;

import com.banesco.common.domain.exception.HttpStatusCodeException;
import com.banesco.module.payment_instruction_request.application.repository.ApiPrivateRepository;
import com.banesco.module.payment_instruction_request.domain.dto.request.PaymentInstructionRequestRequest;
import com.banesco.module.payment_instruction_request.domain.model.PaymentInstructionTransaction;
import com.banesco.module.payment_instruction_request.infrastructure.mapper.ServicingIssueMapper;
import com.banesco.module.payment_request.application.usecase.PaymentRequestUseCase;
import com.banesco.module.payment_request.domain.model.PaymentRequest;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
@ApplicationScoped
public class ApiPrivateAdapter implements ApiPrivateRepository {
    private final PaymentRequestUseCase paymentRequestUseCase;

    @Inject
    public ApiPrivateAdapter(
        PaymentRequestUseCase paymentRequestUseCase
    ) {
        this.paymentRequestUseCase = paymentRequestUseCase;
    }

    @Override
    public PaymentInstructionTransaction execute(
        PaymentInstructionRequestRequest request
    ) {
        List<PaymentRequest> response = paymentRequestUseCase.execute(
            ServicingIssueMapper.toPaymentRequest(request),
            PaymentRequest.class
        );

        if(response.isEmpty()) {
            throw HttpStatusCodeException.conflict("PIBE-00");
        }

        log.info("Resultado de las transacciones obtenidas: {}", response.size());

        return ServicingIssueMapper.toModel(response);
    }
}
