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

import com.banesco.common.domain.exception.HttpStatusCodeException;
import com.banesco.module.payment_file.application.usecase.PaymentFileUseCase;
import com.banesco.module.payment_file.domain.model.PaymentFile;
import com.banesco.module.docservice_file_audit_trace.application.repository.ApiPrivateRepository;
import com.banesco.module.docservice_file_audit_trace.domain.dto.request.DocserviceFileAuditTraceRequest;
import com.banesco.module.docservice_file_audit_trace.domain.model.DocumentService;
import com.banesco.module.docservice_file_audit_trace.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 PaymentFileUseCase paymentFileUseCase;

    @Inject
    public ApiPrivateAdapter(
        PaymentFileUseCase paymentFileUseCase
    ) {
        this.paymentFileUseCase = paymentFileUseCase;
    }

    @Override
    public DocumentService execute(
        DocserviceFileAuditTraceRequest request
    ) {
        PaymentFile response = paymentFileUseCase.execute(
            ServicingIssueMapper.toPaymentRequest(request),
            PaymentFile.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);
    }
}
