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

import dev.lions.unionflow.server.api.enums.vote.ModeScrutin;
import dev.lions.unionflow.server.api.enums.vote.TypeVote;
import dev.lions.unionflow.server.entity.Organisation;
import dev.lions.unionflow.server.entity.vote.CampagneVote;
import dev.lions.unionflow.server.entity.vote.Candidat;
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 CandidatRepositoryTest {

    @Inject
    CandidatRepository candidatRepository;
    @Inject
    CampagneVoteRepository campagneVoteRepository;
    @Inject
    OrganisationRepository organisationRepository;

    private CampagneVote newCampagneVote() {
        Organisation o = new Organisation();
        o.setNom("Org Candidat");
        o.setTypeOrganisation("ASSOCIATION");
        o.setStatut("ACTIVE");
        o.setEmail("candidat-" + UUID.randomUUID() + "@test.com");
        o.setActif(true);
        organisationRepository.persist(o);
        CampagneVote c = CampagneVote.builder()
                .organisation(o)
                .titre("Campagne candidat")
                .typeVote(TypeVote.ELECTION_BUREAU)
                .modeScrutin(ModeScrutin.MAJORITAIRE_UN_TOUR)
                .build();
        campagneVoteRepository.persist(c);
        return c;
    }

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

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

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

    @Test
    @TestTransaction
    @DisplayName("persist puis findById retrouve le candidat")
    void persist_thenFindById_findsCandidat() {
        CampagneVote campagne = newCampagneVote();
        Candidat c = Candidat.builder()
                .campagneVote(campagne)
                .nomCandidatureOuChoix("Candidat test")
                .build();
        candidatRepository.persist(c);
        assertThat(c.getId()).isNotNull();
        Candidat found = candidatRepository.findById(c.getId());
        assertThat(found).isNotNull();
        assertThat(found.getNomCandidatureOuChoix()).isEqualTo("Candidat test");
    }
}
