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.entity.tontine.TourTontine;
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.time.LocalDate;
import java.util.List;
import java.util.UUID;

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

@QuarkusTest
class TourTontineRepositoryTest {

    @Inject
    TourTontineRepository tourTontineRepository;
    @Inject
    TontineRepository tontineRepository;
    @Inject
    OrganisationRepository organisationRepository;

    private Tontine newTontine() {
        Organisation o = new Organisation();
        o.setNom("Org Tour Tontine");
        o.setTypeOrganisation("ASSOCIATION");
        o.setStatut("ACTIVE");
        o.setEmail("tour-tontine-" + UUID.randomUUID() + "@test.com");
        o.setActif(true);
        organisationRepository.persist(o);
        Tontine t = Tontine.builder()
                .nom("Tontine pour tour")
                .organisation(o)
                .typeTontine(TypeTontine.ROTATIVE_CLASSIQUE)
                .frequence(FrequenceTour.MENSUELLE)
                .statut(StatutTontine.PLANIFIEE)
                .build();
        tontineRepository.persist(t);
        return t;
    }

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

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

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

    @Test
    @TestTransaction
    @DisplayName("persist puis findById retrouve le tour")
    void persist_thenFindById_findsTour() {
        Tontine tontine = newTontine();
        TourTontine tour = TourTontine.builder()
                .tontine(tontine)
                .ordreTour(1)
                .dateOuvertureCotisations(LocalDate.now())
                .montantCible(BigDecimal.valueOf(10000))
                .cagnotteCollectee(BigDecimal.ZERO)
                .build();
        tourTontineRepository.persist(tour);
        assertThat(tour.getId()).isNotNull();
        TourTontine found = tourTontineRepository.findById(tour.getId());
        assertThat(found).isNotNull();
        assertThat(found.getOrdreTour()).isEqualTo(1);
    }
}
