package dev.lions.unionflow.server.repository;

import dev.lions.unionflow.server.entity.Configuration;
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 ConfigurationRepositoryTest {

    @Inject
    ConfigurationRepository configurationRepository;

    private static Configuration newConfig(String cle, String categorie) {
        Configuration c = new Configuration();
        c.setCle(cle);
        c.setValeur("valeur");
        c.setCategorie(categorie);
        c.setActif(true);
        c.setVisible(true);
        return c;
    }

    @Test
    @TestTransaction
    @DisplayName("persist puis findById retrouve la configuration")
    void persist_thenFindById_findsConfig() {
        String cle = "test.cle." + UUID.randomUUID();
        Configuration c = newConfig(cle, "SYSTEME");
        configurationRepository.persist(c);
        assertThat(c.getId()).isNotNull();
        Configuration found = configurationRepository.findById(c.getId());
        assertThat(found).isNotNull();
        assertThat(found.getCle()).isEqualTo(cle);
    }

    @Test
    @TestTransaction
    @DisplayName("findByCle retrouve par clé")
    void findByCle_findsByKey() {
        String cle = "test.bycle." + UUID.randomUUID();
        Configuration c = newConfig(cle, "SYSTEME");
        configurationRepository.persist(c);
        Optional<Configuration> opt = configurationRepository.findByCle(cle);
        assertThat(opt).isPresent();
        assertThat(opt.get().getCle()).isEqualTo(cle);
    }

    @Test
    @TestTransaction
    @DisplayName("findByCle retourne empty pour clé inexistante")
    void findByCle_inexistant_returnsEmpty() {
        Optional<Configuration> opt = configurationRepository.findByCle("cle.inexistante." + UUID.randomUUID());
        assertThat(opt).isEmpty();
    }

    @Test
    @TestTransaction
    @DisplayName("findAllActives retourne une liste")
    void findAllActives_returnsList() {
        List<Configuration> list = configurationRepository.findAllActives();
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("findByCategorie retourne une liste (éventuellement vide)")
    void findByCategorie_returnsList() {
        List<Configuration> list = configurationRepository.findByCategorie("SYSTEME");
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("findVisibles retourne une liste")
    void findVisibles_returnsList() {
        List<Configuration> list = configurationRepository.findVisibles();
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("listAll et count cohérents")
    void listAll_count_consistent() {
        List<Configuration> all = configurationRepository.listAll();
        long count = configurationRepository.count();
        assertThat((long) all.size()).isEqualTo(count);
    }
}
