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

import com.banesco.common.domain.exception.HttpStatusCodeException;
import com.banesco.module.payment_status.application.usecase.PaymentStatusUseCase;
import com.banesco.module.payment_status.domain.model.PaymentStatus;
import com.banesco.module.service_issue_payment_status.application.repository.ApiPrivateRepository;
import com.banesco.module.service_issue_payment_status.domain.dto.request.ServiceIssuePaymentStatusRequest;
import com.banesco.module.service_issue_payment_status.domain.model.ServicingIssueProcedure;
import com.banesco.module.service_issue_payment_status.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 PaymentStatusUseCase paymentStatusUseCase;

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

    @Override
    public ServicingIssueProcedure execute(
        ServiceIssuePaymentStatusRequest request
    ) {
        PaymentStatus response = paymentStatusUseCase.execute(
            ServicingIssueMapper.toPaymentRequest(request),
            PaymentStatus.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);
    }
}
