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

import dev.lions.unionflow.server.api.enums.tontine.FrequenceTour;
import dev.lions.unionflow.server.api.enums.tontine.StatutTontine;
import dev.lions.unionflow.server.api.enums.tontine.TypeTontine;
import dev.lions.unionflow.server.entity.Organisation;
import dev.lions.unionflow.server.entity.tontine.Tontine;
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.util.List;
import java.util.UUID;

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

@QuarkusTest
class TontineRepositoryTest {

    @Inject
    TontineRepository tontineRepository;
    @Inject
    OrganisationRepository organisationRepository;

    private Organisation newOrganisation() {
        Organisation o = new Organisation();
        o.setNom("Org Tontine");
        o.setTypeOrganisation("ASSOCIATION");
        o.setStatut("ACTIVE");
        o.setEmail("tontine-" + 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(tontineRepository.findById(UUID.randomUUID())).isNull();
    }

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

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

    @Test
    @TestTransaction
    @DisplayName("persist puis findById retrouve la tontine")
    void persist_thenFindById_findsTontine() {
        Organisation org = newOrganisation();
        Tontine t = Tontine.builder()
                .nom("Tontine test")
                .organisation(org)
                .typeTontine(TypeTontine.ROTATIVE_CLASSIQUE)
                .frequence(FrequenceTour.MENSUELLE)
                .statut(StatutTontine.PLANIFIEE)
                .build();
        tontineRepository.persist(t);
        assertThat(t.getId()).isNotNull();
        Tontine found = tontineRepository.findById(t.getId());
        assertThat(found).isNotNull();
        assertThat(found.getNom()).isEqualTo("Tontine test");
    }
}
