package dev.lions.unionflow.server.service;

import dev.lions.unionflow.server.api.dto.ticket.request.CreateTicketRequest;
import dev.lions.unionflow.server.api.dto.ticket.response.TicketResponse;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.TestTransaction;
import jakarta.inject.Inject;
import jakarta.ws.rs.NotFoundException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;
import java.util.UUID;

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

@QuarkusTest
class TicketServiceTest {

    @Inject
    TicketService ticketService;

    @Test
    @TestTransaction
    @DisplayName("listerTickets retourne une liste")
    void listerTickets_returnsList() {
        List<TicketResponse> list = ticketService.listerTickets(UUID.randomUUID());
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("obtenirTicket avec ID inexistant lance NotFoundException")
    void obtenirTicket_inexistant_throws() {
        assertThatThrownBy(() -> ticketService.obtenirTicket(UUID.randomUUID()))
                .isInstanceOf(NotFoundException.class);
    }

    @Test
    @TestTransaction
    @DisplayName("obtenirStatistiques retourne les clés attendues")
    void obtenirStatistiques_returnsMap() {
        Map<String, Object> stats = ticketService.obtenirStatistiques(UUID.randomUUID());
        assertThat(stats).containsKeys("totalTickets", "ticketsEnAttente", "ticketsResolus", "ticketsFermes");
    }

    @Test
    @TestTransaction
    @DisplayName("creerTicket crée un ticket et retourne un DTO")
    void creerTicket_createsAndReturnsDto() {
        CreateTicketRequest request = CreateTicketRequest.builder()
                .utilisateurId(UUID.randomUUID())
                .sujet("Sujet test")
                .description("Description test ticket service")
                .categorie("SUPPORT")
                .priorite("NORMALE")
                .build();
        TicketResponse response = ticketService.creerTicket(request);
        assertThat(response).isNotNull();
        assertThat(response.getId()).isNotNull();
        assertThat(response.getNumeroTicket()).isNotNull();
        assertThat(response.getSujet()).isEqualTo("Sujet test");
    }
}
