package dev.lions.unionflow.server.entity;

import dev.lions.unionflow.server.api.enums.comptabilite.TypeJournalComptable;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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

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

@DisplayName("JournalComptable")
class JournalComptableTest {

    @Test
    @DisplayName("getters/setters")
    void gettersSetters() {
        JournalComptable j = new JournalComptable();
        j.setCode("BQ");
        j.setLibelle("Banque");
        j.setTypeJournal(TypeJournalComptable.BANQUE);
        j.setDateDebut(LocalDate.of(2025, 1, 1));
        j.setDateFin(LocalDate.of(2025, 12, 31));
        j.setStatut("OUVERT");
        j.setDescription("Journal banque");

        assertThat(j.getCode()).isEqualTo("BQ");
        assertThat(j.getLibelle()).isEqualTo("Banque");
        assertThat(j.getTypeJournal()).isEqualTo(TypeJournalComptable.BANQUE);
        assertThat(j.getStatut()).isEqualTo("OUVERT");
    }

    @Test
    @DisplayName("isOuvert")
    void isOuvert() {
        JournalComptable j = new JournalComptable();
        j.setCode("BQ");
        j.setLibelle("B");
        j.setTypeJournal(TypeJournalComptable.BANQUE);
        j.setStatut("OUVERT");
        assertThat(j.isOuvert()).isTrue();
        j.setStatut("FERME");
        assertThat(j.isOuvert()).isFalse();
    }

    @Test
    @DisplayName("estDansPeriode")
    void estDansPeriode() {
        JournalComptable j = new JournalComptable();
        j.setCode("BQ");
        j.setLibelle("B");
        j.setTypeJournal(TypeJournalComptable.BANQUE);
        j.setDateDebut(LocalDate.of(2025, 1, 1));
        j.setDateFin(LocalDate.of(2025, 12, 31));
        assertThat(j.estDansPeriode(LocalDate.of(2025, 6, 15))).isTrue();
        assertThat(j.estDansPeriode(LocalDate.of(2024, 12, 31))).isFalse();
        assertThat(j.estDansPeriode(LocalDate.of(2026, 1, 1))).isFalse();
        j.setDateDebut(null);
        j.setDateFin(null);
        assertThat(j.estDansPeriode(LocalDate.now())).isTrue();
    }

    @Test
    @DisplayName("statut par défaut OUVERT")
    void statutDefaut() {
        JournalComptable j = new JournalComptable();
        j.setCode("BQ");
        j.setLibelle("B");
        j.setTypeJournal(TypeJournalComptable.BANQUE);
        assertThat(j.getStatut()).isEqualTo("OUVERT");
    }

    @Test
    @DisplayName("equals et hashCode")
    void equalsHashCode() {
        UUID id = UUID.randomUUID();
        JournalComptable a = new JournalComptable();
        a.setId(id);
        a.setCode("BQ");
        a.setLibelle("Banque");
        a.setTypeJournal(TypeJournalComptable.BANQUE);
        JournalComptable b = new JournalComptable();
        b.setId(id);
        b.setCode("BQ");
        b.setLibelle("Banque");
        b.setTypeJournal(TypeJournalComptable.BANQUE);
        assertThat(a).isEqualTo(b);
        assertThat(a.hashCode()).isEqualTo(b.hashCode());
    }

    @Test
    @DisplayName("toString non null")
    void toString_nonNull() {
        JournalComptable j = new JournalComptable();
        j.setCode("BQ");
        j.setLibelle("Banque");
        j.setTypeJournal(TypeJournalComptable.BANQUE);
        assertThat(j.toString()).isNotNull().isNotEmpty();
    }
}
