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

import com.banesco.common.domain.exception.HttpStatusCodeException;
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.domain.model.CorrespondenceTransaction;
import com.banesco.module.correspondence.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 SecurityNotificationUseCase securityNotificationUseCase;

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

    @Override
    public CorrespondenceTransaction execute(
        CorrespondenceRequest request
    ) {
        SecurityNotification response = securityNotificationUseCase.execute(
            ServicingIssueMapper.toSecurityNotification(request),
            SecurityNotification.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);
    }
}
