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

import com.banesco.common.domain.exception.HttpStatusCodeException;
import com.banesco.module.correspondence.domain.model.Correspondence;
import com.banesco.module.security_notification.application.usecase.SecurityNotificationUseCase;
import com.banesco.module.security_notification.domain.model.SecurityNotification;
import com.banesco.module.correspondence.application.repository.ApiPrivateRepository;
import com.banesco.module.correspondence.domain.dto.request.CorrespondenceRequest;
import com.banesco.module.correspondence.infrastructure.mapper.CorrespondenceMapper;
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 SecurityNotificationUseCase securityNotificationUseCase;

    @Inject
    public ApiPrivateAdapter(
        SecurityNotificationUseCase securityNotificationUseCase
    ) {
        this.securityNotificationUseCase = securityNotificationUseCase;
    }

    @Override
    public List<Correspondence> execute(
        CorrespondenceRequest request
    ) {
        List<SecurityNotification> response = securityNotificationUseCase.execute(
            CorrespondenceMapper.toPaymentRequest(request),
            SecurityNotification.class
        );

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

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

        return CorrespondenceMapper.toModel(response);
    }
}
