package dev.lions.unionflow.server.repository.culte;

import dev.lions.unionflow.server.api.enums.culte.TypeDonReligieux;
import dev.lions.unionflow.server.entity.Membre;
import dev.lions.unionflow.server.entity.Organisation;
import dev.lions.unionflow.server.entity.culte.DonReligieux;
import dev.lions.unionflow.server.repository.MembreRepository;
import dev.lions.unionflow.server.repository.OrganisationRepository;
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.util.List;
import java.util.UUID;

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

@QuarkusTest
class DonReligieuxRepositoryTest {

    @Inject
    DonReligieuxRepository donReligieuxRepository;
    @Inject
    OrganisationRepository organisationRepository;
    @Inject
    MembreRepository membreRepository;

    private Organisation newOrganisation() {
        Organisation o = new Organisation();
        o.setNom("Org Culte");
        o.setTypeOrganisation("ASSOCIATION");
        o.setStatut("ACTIVE");
        o.setEmail("culte-" + UUID.randomUUID() + "@test.com");
        o.setActif(true);
        organisationRepository.persist(o);
        return o;
    }

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

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

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

    @Test
    @TestTransaction
    @DisplayName("persist puis findById retrouve le don")
    void persist_thenFindById_findsDon() {
        Organisation org = newOrganisation();
        DonReligieux d = DonReligieux.builder()
                .institution(org)
                .fidele(null)
                .typeDon(TypeDonReligieux.QUETE_ORDINAIRE)
                .montant(BigDecimal.TEN)
                .build();
        donReligieuxRepository.persist(d);
        assertThat(d.getId()).isNotNull();
        DonReligieux found = donReligieuxRepository.findById(d.getId());
        assertThat(found).isNotNull();
        assertThat(found.getMontant()).isEqualByComparingTo(BigDecimal.TEN);
    }
}
