package dev.lions.unionflow.server.repository;

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

    @Inject
    PermissionRepository permissionRepository;

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

    @Test
    @TestTransaction
    @DisplayName("findPermissionById retourne empty pour UUID inexistant")
    void findPermissionById_inexistant_returnsEmpty() {
        Optional<Permission> opt = permissionRepository.findPermissionById(UUID.randomUUID());
        assertThat(opt).isEmpty();
    }

    @Test
    @TestTransaction
    @DisplayName("findByCode retourne empty pour code inexistant")
    void findByCode_inexistant_returnsEmpty() {
        Optional<Permission> opt = permissionRepository.findByCode("CODE_" + UUID.randomUUID());
        assertThat(opt).isEmpty();
    }

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

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

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

    @Test
    @TestTransaction
    @DisplayName("findByModuleAndRessource retourne une liste vide pour module/ressource inexistants")
    void findByModuleAndRessource_inexistant_returnsEmpty() {
        List<Permission> list = permissionRepository.findByModuleAndRessource("MODULE_INEXISTANT", "RESSOURCE_INEXISTANTE");
        assertThat(list).isNotNull().isEmpty();
    }
}
