package dev.lions.user.manager.dto.role;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
 * Tests unitaires pour RoleAssignmentDTO
 */
class RoleAssignmentDTOTest {

    @Test
    void testBuilder() {
        RoleAssignmentDTO assignment = RoleAssignmentDTO.builder()
            .userId("user-123")
            .realmName("test-realm")
            .roleNames(Arrays.asList("admin", "user"))
            .build();

        assertNotNull(assignment);
        assertEquals("user-123", assignment.getUserId());
        assertEquals("test-realm", assignment.getRealmName());
        assertEquals(2, assignment.getRoleNames().size());
        assertTrue(assignment.getRoleNames().contains("admin"));
        assertTrue(assignment.getRoleNames().contains("user"));
    }

    @Test
    void testNoArgsConstructor() {
        RoleAssignmentDTO assignment = new RoleAssignmentDTO();

        assertNotNull(assignment);
        assertNull(assignment.getUserId());
        assertNull(assignment.getRealmName());
        assertNull(assignment.getRoleNames());
    }

    @Test
    void testAllArgsConstructor() {
        // Le constructeur AllArgsConstructor nécessite tous les champs
        // On utilise plutôt le builder pour ce test
        List<String> roles = Arrays.asList("admin", "user");
        RoleAssignmentDTO assignment = RoleAssignmentDTO.builder()
            .userId("user-123")
            .realmName("test-realm")
            .roleNames(roles)
            .build();

        assertNotNull(assignment);
        assertEquals("user-123", assignment.getUserId());
        assertEquals("test-realm", assignment.getRealmName());
        assertEquals(2, assignment.getRoleNames().size());
    }

    @Test
    void testSettersAndGetters() {
        RoleAssignmentDTO assignment = new RoleAssignmentDTO();

        assignment.setUserId("user-456");
        assignment.setRealmName("realm2");
        assignment.setRoleNames(Collections.singletonList("admin"));

        assertEquals("user-456", assignment.getUserId());
        assertEquals("realm2", assignment.getRealmName());
        assertEquals(1, assignment.getRoleNames().size());
        assertEquals("admin", assignment.getRoleNames().get(0));
    }

    @Test
    void testEqualsAndHashCode() {
        RoleAssignmentDTO assignment1 = RoleAssignmentDTO.builder()
            .userId("user-123")
            .realmName("test-realm")
            .roleNames(Arrays.asList("admin"))
            .typeRole(dev.lions.user.manager.enums.role.TypeRole.REALM_ROLE)
            .build();

        RoleAssignmentDTO assignment2 = RoleAssignmentDTO.builder()
            .userId("user-123")
            .realmName("test-realm")
            .roleNames(Arrays.asList("admin"))
            .typeRole(dev.lions.user.manager.enums.role.TypeRole.REALM_ROLE)
            .build();

        assertEquals(assignment1, assignment2);
        assertEquals(assignment1.hashCode(), assignment2.hashCode());
    }

    @Test
    void testIsValidForClientRole() {
        RoleAssignmentDTO assignment = RoleAssignmentDTO.builder()
            .typeRole(dev.lions.user.manager.enums.role.TypeRole.CLIENT_ROLE)
            .clientName("test-client")
            .build();

        assertTrue(assignment.isValidForClientRole());
    }

    @Test
    void testIsValidForRealmRole() {
        RoleAssignmentDTO assignment = RoleAssignmentDTO.builder()
            .typeRole(dev.lions.user.manager.enums.role.TypeRole.REALM_ROLE)
            .build();

        assertTrue(assignment.isValidForRealmRole());
    }

    @Test
    void testGetRoleCount() {
        RoleAssignmentDTO assignment = RoleAssignmentDTO.builder()
            .roleNames(Arrays.asList("admin", "user", "viewer"))
            .build();

        assertEquals(3, assignment.getRoleCount());
    }

    @Test
    void testGetRoleCount_Empty() {
        RoleAssignmentDTO assignment = RoleAssignmentDTO.builder()
            .roleNames(Collections.emptyList())
            .build();

        assertEquals(0, assignment.getRoleCount());
    }

    @Test
    void testGetRoleCount_Null() {
        RoleAssignmentDTO assignment = new RoleAssignmentDTO();

        assertEquals(0, assignment.getRoleCount());
    }

    @Test
    void testToString() {
        RoleAssignmentDTO assignment = RoleAssignmentDTO.builder()
            .userId("user-123")
            .realmName("test-realm")
            .build();

        String toString = assignment.toString();
        assertNotNull(toString);
    }
}
