package dev.lions.user.manager.mapper;

import dev.lions.user.manager.dto.role.RoleDTO;
import dev.lions.user.manager.enums.role.TypeRole;
import org.junit.jupiter.api.Test;
import org.keycloak.representations.idm.RoleRepresentation;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;

class RoleMapperTest {

    @Test
    void testToDTO() {
        RoleRepresentation rep = new RoleRepresentation();
        rep.setId("1");
        rep.setName("role");
        rep.setDescription("desc");
        rep.setComposite(true);

        RoleDTO dto = RoleMapper.toDTO(rep, "realm", TypeRole.REALM_ROLE);

        assertNotNull(dto);
        assertEquals("1", dto.getId());
        assertEquals("role", dto.getName());
        assertEquals("desc", dto.getDescription());
        assertEquals(TypeRole.REALM_ROLE, dto.getTypeRole());
        assertEquals("realm", dto.getRealmName());
        assertTrue(dto.getComposite());

        assertNull(RoleMapper.toDTO(null, "realm", TypeRole.REALM_ROLE));
    }

    @Test
    void testToRepresentation() {
        RoleDTO dto = RoleDTO.builder()
                .id("1")
                .name("role")
                .description("desc")
                .composite(true)
                .compositeRoles(Collections.singletonList("subrole"))
                .typeRole(TypeRole.CLIENT_ROLE) // Should setClientRole(true)
                .build();

        RoleRepresentation rep = RoleMapper.toRepresentation(dto);

        assertNotNull(rep);
        assertEquals("1", rep.getId());
        assertEquals("role", rep.getName());
        assertEquals("desc", rep.getDescription());
        assertTrue(rep.isComposite());
        assertTrue(rep.getClientRole());

        assertNull(RoleMapper.toRepresentation(null));
    }

    // New test case to cover full branch logic
    @Test
    void testToRepresentationRealmRole() {
        RoleDTO dto = RoleDTO.builder()
                .typeRole(TypeRole.REALM_ROLE)
                .build();
        RoleRepresentation rep = RoleMapper.toRepresentation(dto);
        assertFalse(rep.getClientRole());
    }

    @Test
    void testToDTOList() {
        RoleRepresentation rep = new RoleRepresentation();
        rep.setName("role");
        List<RoleRepresentation> reps = Collections.singletonList(rep);

        List<RoleDTO> dtos = RoleMapper.toDTOList(reps, "realm", TypeRole.REALM_ROLE);
        assertEquals(1, dtos.size());
        assertEquals("role", dtos.get(0).getName());

        assertTrue(RoleMapper.toDTOList(null, "realm", TypeRole.REALM_ROLE).isEmpty());
    }

    @Test
    void testToRepresentationList() {
        RoleDTO dto = RoleDTO.builder().name("role").typeRole(TypeRole.REALM_ROLE).build();
        List<RoleDTO> dtos = Collections.singletonList(dto);

        List<RoleRepresentation> reps = RoleMapper.toRepresentationList(dtos);
        assertEquals(1, reps.size());
        assertEquals("role", reps.get(0).getName());

        assertTrue(RoleMapper.toRepresentationList(null).isEmpty());
    }
}
