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 paymentStatusUseCase;

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

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

        if(Objects.isNull(response) || Objects.isNull(response.getId())) {
            throw HttpStatusCodeException.badRequest("400");
        }

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

        return ServicingIssueMapper.toModel(response);
    }
}
