package dev.lions.unionflow.server.repository;

import dev.lions.unionflow.server.api.enums.wave.StatutWebhook;
import dev.lions.unionflow.server.api.enums.wave.TypeEvenementWebhook;
import dev.lions.unionflow.server.entity.WebhookWave;
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 WebhookWaveRepositoryTest {

    @Inject
    WebhookWaveRepository webhookWaveRepository;

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

    @Test
    @TestTransaction
    @DisplayName("findWebhookWaveById retourne empty pour UUID inexistant")
    void findWebhookWaveById_inexistant_returnsEmpty() {
        Optional<WebhookWave> opt = webhookWaveRepository.findWebhookWaveById(UUID.randomUUID());
        assertThat(opt).isEmpty();
    }

    @Test
    @TestTransaction
    @DisplayName("findByWaveEventId retourne empty pour id inexistant")
    void findByWaveEventId_inexistant_returnsEmpty() {
        Optional<WebhookWave> opt = webhookWaveRepository.findByWaveEventId("evt-" + UUID.randomUUID());
        assertThat(opt).isEmpty();
    }

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

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

    @Test
    @TestTransaction
    @DisplayName("findByTransactionWaveId retourne une liste")
    void findByTransactionWaveId_returnsList() {
        List<WebhookWave> list = webhookWaveRepository.findByTransactionWaveId(UUID.randomUUID());
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("findEnAttente retourne une liste")
    void findEnAttente_returnsList() {
        List<WebhookWave> list = webhookWaveRepository.findEnAttente();
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("persist puis findById retrouve le webhook")
    void persist_thenFindById_findsWebhook() {
        String eventId = "evt-" + UUID.randomUUID();
        WebhookWave w = WebhookWave.builder()
                .waveEventId(eventId)
                .build();
        webhookWaveRepository.persist(w);
        assertThat(w.getId()).isNotNull();
        WebhookWave found = webhookWaveRepository.findById(w.getId());
        assertThat(found).isNotNull();
        assertThat(found.getWaveEventId()).isEqualTo(eventId);
    }

    @Test
    @TestTransaction
    @DisplayName("findByPaiementId retourne une liste vide pour UUID inexistant")
    void findByPaiementId_inexistant_returnsEmpty() {
        List<WebhookWave> list = webhookWaveRepository.findByPaiementId(UUID.randomUUID());
        assertThat(list).isNotNull().isEmpty();
    }

    @Test
    @TestTransaction
    @DisplayName("findByStatut retourne une liste")
    void findByStatut_returnsList() {
        List<WebhookWave> list = webhookWaveRepository.findByStatut(StatutWebhook.EN_ATTENTE);
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("findByType retourne une liste")
    void findByType_returnsList() {
        List<WebhookWave> list = webhookWaveRepository.findByType(TypeEvenementWebhook.TRANSACTION_COMPLETED);
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("findEchouesRetentables retourne une liste")
    void findEchouesRetentables_returnsList() {
        List<WebhookWave> list = webhookWaveRepository.findEchouesRetentables();
        assertThat(list).isNotNull();
    }
}
