package dev.lions.unionflow.server.repository;

import dev.lions.unionflow.server.entity.Ticket;
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.Optional;
import java.util.UUID;

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

@QuarkusTest
class TicketRepositoryTest {

    @Inject
    TicketRepository ticketRepository;

    private static Ticket newTicket(UUID userId) {
        Ticket t = new Ticket();
        t.setUtilisateurId(userId);
        t.setSujet("Sujet test");
        t.setDescription("Description");
        t.setStatut("OUVERT");
        t.setNumeroTicket("TK-TEST-" + UUID.randomUUID());
        t.setActif(true);
        return t;
    }

    @Test
    @TestTransaction
    @DisplayName("persist puis findById retrouve le ticket")
    void persist_thenFindById_findsTicket() {
        UUID userId = UUID.randomUUID();
        Ticket t = newTicket(userId);
        ticketRepository.persist(t);
        assertThat(t.getId()).isNotNull();
        Ticket found = ticketRepository.findById(t.getId());
        assertThat(found).isNotNull();
        assertThat(found.getSujet()).isEqualTo("Sujet test");
    }

    @Test
    @TestTransaction
    @DisplayName("findByNumeroTicket retourne empty pour numéro inexistant")
    void findByNumeroTicket_inexistant_returnsEmpty() {
        Optional<Ticket> opt = ticketRepository.findByNumeroTicket("TK-INEXISTANT-" + UUID.randomUUID());
        assertThat(opt).isEmpty();
    }

    @Test
    @TestTransaction
    @DisplayName("findByNumeroTicket retrouve le ticket après persist")
    void findByNumeroTicket_findsAfterPersist() {
        String numero = "TK-PERSIST-" + UUID.randomUUID();
        Ticket t = newTicket(UUID.randomUUID());
        t.setNumeroTicket(numero);
        ticketRepository.persist(t);
        Optional<Ticket> found = ticketRepository.findByNumeroTicket(numero);
        assertThat(found).isPresent();
        assertThat(found.get().getNumeroTicket()).isEqualTo(numero);
    }

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

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

    @Test
    @TestTransaction
    @DisplayName("countByStatutAndUtilisateurId avec statut null compte tous les tickets utilisateur")
    void countByStatutAndUtilisateurId_statutNull_returnsCount() {
        long n = ticketRepository.countByStatutAndUtilisateurId(null, UUID.randomUUID());
        assertThat(n).isGreaterThanOrEqualTo(0L);
    }

    @Test
    @TestTransaction
    @DisplayName("countByStatutAndUtilisateurId avec statut retourne un nombre >= 0")
    void countByStatutAndUtilisateurId_withStatut_returnsNonNegative() {
        long n = ticketRepository.countByStatutAndUtilisateurId("OUVERT", UUID.randomUUID());
        assertThat(n).isGreaterThanOrEqualTo(0L);
    }

    @Test
    @TestTransaction
    @DisplayName("genererNumeroTicket retourne un numéro non vide")
    void genererNumeroTicket_returnsNonEmpty() {
        String numero = ticketRepository.genererNumeroTicket();
        assertThat(numero).isNotBlank();
        assertThat(numero).startsWith("TK-");
    }
}
