package dev.lions.unionflow.server.repository;

import dev.lions.unionflow.server.entity.Suggestion;
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.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

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

@QuarkusTest
class SuggestionRepositoryTest {

    @Inject
    SuggestionRepository suggestionRepository;

    private static Suggestion newSuggestion(UUID userId) {
        Suggestion s = Suggestion.builder()
                .utilisateurId(userId)
                .utilisateurNom("Test")
                .titre("Suggestion test")
                .description("Desc")
                .statut("NOUVELLE")
                .nbVotes(0)
                .build();
        s.setDateCreation(LocalDateTime.now());
        s.setDateSoumission(LocalDateTime.now());
        s.setActif(true);
        return s;
    }

    @Test
    @TestTransaction
    @DisplayName("persist puis findById retrouve la suggestion")
    void persist_thenFindById_findsSuggestion() {
        Suggestion s = newSuggestion(UUID.randomUUID());
        suggestionRepository.persist(s);
        assertThat(s.getId()).isNotNull();
        Suggestion found = suggestionRepository.findById(s.getId());
        assertThat(found).isNotNull();
        assertThat(found.getTitre()).isEqualTo("Suggestion test");
    }

    @Test
    @TestTransaction
    @DisplayName("findAllActivesOrderByVotes retourne une liste")
    void findAllActivesOrderByVotes_returnsList() {
        List<Suggestion> list = suggestionRepository.findAllActivesOrderByVotes();
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("findByUtilisateurId retourne une liste")
    void findByUtilisateurId_returnsList() {
        List<Suggestion> list = suggestionRepository.findByUtilisateurId(UUID.randomUUID());
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("findByStatut retourne une liste")
    void findByStatut_returnsList() {
        List<Suggestion> list = suggestionRepository.findByStatut("NOUVELLE");
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("countByStatut retourne un nombre >= 0")
    void countByStatut_returnsNonNegative() {
        assertThat(suggestionRepository.countByStatut("NOUVELLE")).isGreaterThanOrEqualTo(0L);
    }

    @Test
    @TestTransaction
    @DisplayName("findTopByVotes retourne au plus limit éléments")
    void findTopByVotes_returnsLimitedList() {
        List<Suggestion> list = suggestionRepository.findTopByVotes(5);
        assertThat(list).isNotNull();
        assertThat(list.size()).isLessThanOrEqualTo(5);
    }
}
