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

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

import java.util.Objects;

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

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

    @Override
    public PaymentInitiationTransaction execute(
        PaymentInitiationRequestRequest request
    ) {
        PaymentRequest response = paymentRequestUseCase.execute(
            ServicingIssueMapper.toPaymentRequest(request),
            PaymentRequest.class
        );

        if(Objects.isNull(response) || Objects.isNull(response.getId())) {
            throw HttpStatusCodeException.conflict("PIBE-00");
        }

        log.info("Resultado de la transaccion obtenida: {}", response.getId());

        return ServicingIssueMapper.toModel(response);
    }
}
