package dev.lions.unionflow.server.repository.mutuelle.credit;

import dev.lions.unionflow.server.api.enums.mutuelle.credit.StatutDemandeCredit;
import dev.lions.unionflow.server.api.enums.mutuelle.credit.StatutEcheanceCredit;
import dev.lions.unionflow.server.api.enums.mutuelle.credit.TypeCredit;
import dev.lions.unionflow.server.entity.Membre;
import dev.lions.unionflow.server.entity.mutuelle.credit.DemandeCredit;
import dev.lions.unionflow.server.entity.mutuelle.credit.EcheanceCredit;
import dev.lions.unionflow.server.repository.MembreRepository;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.TestTransaction;
import jakarta.inject.Inject;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;

@QuarkusTest
class EcheanceCreditRepositoryTest {

    @Inject
    EcheanceCreditRepository echeanceCreditRepository;
    @Inject
    DemandeCreditRepository demandeCreditRepository;
    @Inject
    MembreRepository membreRepository;

    private DemandeCredit newDemandeCredit() {
        Membre m = new Membre();
        m.setNumeroMembre("ECH-" + UUID.randomUUID().toString().substring(0, 8));
        m.setPrenom("Test");
        m.setNom("User");
        m.setEmail("echeance-m-" + UUID.randomUUID() + "@test.com");
        m.setDateNaissance(java.time.LocalDate.of(1990, 1, 1));
        m.setActif(true);
        membreRepository.persist(m);
        DemandeCredit d = DemandeCredit.builder()
                .numeroDossier("ECH-D-" + UUID.randomUUID().toString().substring(0, 8))
                .membre(m)
                .typeCredit(TypeCredit.CONSOMMATION)
                .montantDemande(BigDecimal.valueOf(50000))
                .dureeMoisDemande(6)
                .statut(StatutDemandeCredit.BROUILLON)
                .build();
        demandeCreditRepository.persist(d);
        return d;
    }

    @Test
    @TestTransaction
    @DisplayName("findById retourne null pour UUID inexistant")
    void findById_inexistant_returnsNull() {
        assertThat(echeanceCreditRepository.findById(UUID.randomUUID())).isNull();
    }

    @Test
    @TestTransaction
    @DisplayName("listAll retourne une liste")
    void listAll_returnsList() {
        List<EcheanceCredit> list = echeanceCreditRepository.listAll();
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("count retourne un nombre >= 0")
    void count_returnsNonNegative() {
        assertThat(echeanceCreditRepository.count()).isGreaterThanOrEqualTo(0L);
    }

    @Test
    @TestTransaction
    @DisplayName("persist puis findById retrouve l'échéance")
    void persist_thenFindById_findsEcheance() {
        DemandeCredit demande = newDemandeCredit();
        EcheanceCredit e = EcheanceCredit.builder()
                .demandeCredit(demande)
                .ordre(1)
                .dateEcheancePrevue(LocalDate.now().plusMonths(1))
                .capitalAmorti(BigDecimal.valueOf(8000))
                .interetsDeLaPeriode(BigDecimal.valueOf(500))
                .montantTotalExigible(BigDecimal.valueOf(8500))
                .capitalRestantDu(BigDecimal.valueOf(42000))
                .statut(StatutEcheanceCredit.A_VENIR)
                .build();
        echeanceCreditRepository.persist(e);
        assertThat(e.getId()).isNotNull();
        EcheanceCredit found = echeanceCreditRepository.findById(e.getId());
        assertThat(found).isNotNull();
        assertThat(found.getOrdre()).isEqualTo(1);
    }
}
