package dev.lions.unionflow.server.entity.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.entity.Membre;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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

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

@DisplayName("EcheanceCredit")
class EcheanceCreditTest {

    private static DemandeCredit newDemandeCredit() {
        Membre m = new Membre();
        m.setId(UUID.randomUUID());
        m.setNumeroMembre("M1");
        m.setPrenom("A");
        m.setNom("B");
        m.setEmail("a@test.com");
        m.setDateNaissance(LocalDate.now());
        DemandeCredit d = new DemandeCredit();
        d.setId(UUID.randomUUID());
        d.setNumeroDossier("DC-1");
        d.setMembre(m);
        d.setTypeCredit(dev.lions.unionflow.server.api.enums.mutuelle.credit.TypeCredit.CONSOMMATION);
        d.setMontantDemande(BigDecimal.valueOf(100000));
        d.setDureeMoisDemande(12);
        d.setStatut(StatutDemandeCredit.SOUMISE);
        d.setDateSoumission(LocalDate.now());
        return d;
    }

    @Test
    @DisplayName("getters/setters")
    void gettersSetters() {
        EcheanceCredit e = new EcheanceCredit();
        e.setDemandeCredit(newDemandeCredit());
        e.setOrdre(1);
        e.setDateEcheancePrevue(LocalDate.now().plusMonths(1));
        e.setCapitalAmorti(new BigDecimal("10000"));
        e.setInteretsDeLaPeriode(new BigDecimal("500"));
        e.setMontantTotalExigible(new BigDecimal("10500"));
        e.setCapitalRestantDu(new BigDecimal("90000"));
        e.setStatut(StatutEcheanceCredit.A_VENIR);

        assertThat(e.getOrdre()).isEqualTo(1);
        assertThat(e.getStatut()).isEqualTo(StatutEcheanceCredit.A_VENIR);
    }

    @Test
    @DisplayName("equals et hashCode")
    void equalsHashCode() {
        UUID id = UUID.randomUUID();
        DemandeCredit d = newDemandeCredit();
        EcheanceCredit a = new EcheanceCredit();
        a.setId(id);
        a.setDemandeCredit(d);
        a.setOrdre(1);
        a.setDateEcheancePrevue(LocalDate.now());
        a.setCapitalAmorti(BigDecimal.ONE);
        a.setInteretsDeLaPeriode(BigDecimal.ZERO);
        a.setMontantTotalExigible(BigDecimal.ONE);
        a.setCapitalRestantDu(BigDecimal.ONE);
        a.setStatut(StatutEcheanceCredit.A_VENIR);
        EcheanceCredit b = new EcheanceCredit();
        b.setId(id);
        b.setDemandeCredit(d);
        b.setOrdre(1);
        b.setDateEcheancePrevue(a.getDateEcheancePrevue());
        b.setCapitalAmorti(BigDecimal.ONE);
        b.setInteretsDeLaPeriode(BigDecimal.ZERO);
        b.setMontantTotalExigible(BigDecimal.ONE);
        b.setCapitalRestantDu(BigDecimal.ONE);
        b.setStatut(StatutEcheanceCredit.A_VENIR);
        assertThat(a).isEqualTo(b);
        assertThat(a.hashCode()).isEqualTo(b.hashCode());
    }

    @Test
    @DisplayName("toString non null")
    void toString_nonNull() {
        EcheanceCredit e = new EcheanceCredit();
        e.setDemandeCredit(newDemandeCredit());
        e.setOrdre(1);
        e.setDateEcheancePrevue(LocalDate.now());
        e.setCapitalAmorti(BigDecimal.ONE);
        e.setInteretsDeLaPeriode(BigDecimal.ZERO);
        e.setMontantTotalExigible(BigDecimal.ONE);
        e.setCapitalRestantDu(BigDecimal.ONE);
        e.setStatut(StatutEcheanceCredit.A_VENIR);
        assertThat(e.toString()).isNotNull().isNotEmpty();
    }
}
