package dev.lions.unionflow.server.repository;

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

    @Inject
    RoleRepository roleRepository;

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

    @Test
    @TestTransaction
    @DisplayName("findRoleById retourne empty pour UUID inexistant")
    void findRoleById_inexistant_returnsEmpty() {
        Optional<Role> opt = roleRepository.findRoleById(UUID.randomUUID());
        assertThat(opt).isEmpty();
    }

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

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

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

    @Test
    @TestTransaction
    @DisplayName("findRolesSysteme retourne une liste")
    void findRolesSysteme_returnsList() {
        List<Role> list = roleRepository.findRolesSysteme();
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("findByOrganisationId retourne une liste")
    void findByOrganisationId_returnsList() {
        List<Role> list = roleRepository.findByOrganisationId(UUID.randomUUID());
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("findAllActifs retourne une liste")
    void findAllActifs_returnsList() {
        List<Role> list = roleRepository.findAllActifs();
        assertThat(list).isNotNull();
    }

    @Test
    @TestTransaction
    @DisplayName("persist puis findById retrouve le rôle")
    void persist_thenFindById_findsRole() {
        String code = "ROLE-" + UUID.randomUUID().toString().substring(0, 8);
        Role r = Role.builder()
                .code(code)
                .libelle("Rôle test")
                .typeRole(Role.TypeRole.PERSONNALISE.name())
                .niveauHierarchique(100)
                .build();
        roleRepository.persist(r);
        assertThat(r.getId()).isNotNull();
        Role found = roleRepository.findById(r.getId());
        assertThat(found).isNotNull();
        assertThat(found.getCode()).isEqualTo(code);
    }

    @Test
    @TestTransaction
    @DisplayName("findByType retourne une liste de rôles")
    void findByType_returnsList() {
        // Persist a role with a specific type
        String code = "ROLE-TYPE-" + UUID.randomUUID().toString().substring(0, 8);
        Role r = Role.builder()
                .code(code)
                .libelle("Rôle type test")
                .typeRole(Role.TypeRole.SYSTEME.name())
                .niveauHierarchique(10)
                .build();
        roleRepository.persist(r);

        List<Role> list = roleRepository.findByType(Role.TypeRole.SYSTEME.name());
        assertThat(list).isNotNull();
        assertThat(list).hasSizeGreaterThanOrEqualTo(1);
        assertThat(list.stream().anyMatch(role -> code.equals(role.getCode()))).isTrue();
    }
}
