package dev.lions.unionflow.server.repository;

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

    @Inject
    TypeReferenceRepository typeReferenceRepository;

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

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

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

    @Test
    @TestTransaction
    @DisplayName("findByDomaine retourne une liste")
    void findByDomaine_returnsList() {
        List<TypeReference> list = typeReferenceRepository.findByDomaine("TEST_DOMAIN", UUID.randomUUID());
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("findByDomaineAndCode retourne empty pour code inexistant")
    void findByDomaineAndCode_inexistant_returnsEmpty() {
        Optional<TypeReference> opt = typeReferenceRepository.findByDomaineAndCode("TEST", "CODE_" + UUID.randomUUID());
        assertThat(opt).isEmpty();
    }

    @Test
    @TestTransaction
    @DisplayName("listDomaines retourne une liste")
    void listDomaines_returnsList() {
        List<String> list = typeReferenceRepository.listDomaines();
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("existsByDomaineAndCode retourne false pour domaine/code inexistant")
    void existsByDomaineAndCode_inexistant_returnsFalse() {
        boolean exists = typeReferenceRepository.existsByDomaineAndCode("TEST", "X" + UUID.randomUUID(), UUID.randomUUID());
        assertThat(exists).isFalse();
    }

    @Test
    @TestTransaction
    @DisplayName("findLibelleByDomaineAndCode avec code null retourne null")
    void findLibelleByDomaineAndCode_null_returnsNull() {
        assertThat(typeReferenceRepository.findLibelleByDomaineAndCode("TEST", null)).isNull();
    }

    @Test
    @TestTransaction
    @DisplayName("persist puis findById retrouve la référence")
    void persist_thenFindById_findsTypeReference() {
        String domaine = "TEST_" + UUID.randomUUID().toString().substring(0, 8);
        String code = "CODE_" + UUID.randomUUID().toString().substring(0, 8);
        TypeReference t = TypeReference.builder()
                .domaine(domaine)
                .code(code)
                .libelle("Libellé test")
                .build();
        typeReferenceRepository.persist(t);
        assertThat(t.getId()).isNotNull();
        TypeReference found = typeReferenceRepository.findById(t.getId());
        assertThat(found).isNotNull();
        // Le @PrePersist de TypeReference normalise le code en majuscules
        assertThat(found.getCode()).isEqualTo(code.toUpperCase());
    }

    @Test
    @TestTransaction
    @DisplayName("findSeverityByDomaineAndCode avec code null retourne null")
    void findSeverityByDomaineAndCode_nullCode_returnsNull() {
        // code null → retourne null immédiatement
        String result = typeReferenceRepository.findSeverityByDomaineAndCode("STATUT_PAIEMENT", null);
        assertThat(result).isNull();
    }

    @Test
    @TestTransaction
    @DisplayName("findSeverityByDomaineAndCode avec code valide retourne null quand non trouvé")
    void findSeverityByDomaineAndCode_validCode_notFound_returnsNull() {
        // code != null → exécute la requête → orElse(null) car non trouvé
        String result = typeReferenceRepository.findSeverityByDomaineAndCode("DOMAINE_INEXISTANT", "CODE_XYZ");
        assertThat(result).isNull();
    }

    @Test
    @TestTransaction
    @DisplayName("findSeverityByDomaineAndCode avec code trouvé retourne la severity")
    void findSeverityByDomaineAndCode_found_returnsSeverity() {
        // Crée une TypeReference avec severity pour couvrir la branche map(getSeverity) → present
        String domaine = "SEVERITY_TEST_" + UUID.randomUUID().toString().substring(0, 8);
        String code = "SVR_" + UUID.randomUUID().toString().substring(0, 8);
        TypeReference t = TypeReference.builder()
                .domaine(domaine)
                .code(code)
                .libelle("Test severity")
                .severity("warning")
                .build();
        typeReferenceRepository.persist(t);

        // code != null → findByDomaineAndCode → trouvé → map(getSeverity) → "warning"
        String result = typeReferenceRepository.findSeverityByDomaineAndCode(domaine, code.toUpperCase());
        assertThat(result).isEqualTo("warning");
    }
}
