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

import org.junit.jupiter.api.Test;

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

/**
 * Tests unitaires pour UserSearchCriteriaDTO
 */
class UserSearchCriteriaDTOTest {

    @Test
    void testBuilder() {
        UserSearchCriteriaDTO criteria = UserSearchCriteriaDTO.builder()
            .realmName("test-realm")
            .username("testuser")
            .email("test@example.com")
            .enabled(true)
            .page(0)
            .pageSize(20)
            .build();

        assertNotNull(criteria);
        assertEquals("test-realm", criteria.getRealmName());
        assertEquals("testuser", criteria.getUsername());
        assertEquals("test@example.com", criteria.getEmail());
        assertTrue(criteria.getEnabled());
        assertEquals(0, criteria.getPage());
        assertEquals(20, criteria.getPageSize());
    }

    @Test
    void testNoArgsConstructor() {
        UserSearchCriteriaDTO criteria = new UserSearchCriteriaDTO();

        assertNotNull(criteria);
        assertNull(criteria.getRealmName());
        assertNull(criteria.getUsername());
        assertNull(criteria.getEmail());
        assertNull(criteria.getEnabled());
    }

    @Test
    void testAllArgsConstructor() {
        // Le constructeur AllArgsConstructor nécessite tous les champs
        // On utilise plutôt le builder pour ce test
        UserSearchCriteriaDTO criteria = UserSearchCriteriaDTO.builder()
            .realmName("test-realm")
            .username("testuser")
            .email("test@example.com")
            .enabled(true)
            .page(0)
            .pageSize(20)
            .build();

        assertNotNull(criteria);
        assertEquals("test-realm", criteria.getRealmName());
        assertEquals("testuser", criteria.getUsername());
        assertEquals("test@example.com", criteria.getEmail());
        assertTrue(criteria.getEnabled());
        assertEquals(0, criteria.getPage());
        assertEquals(20, criteria.getPageSize());
    }

    @Test
    void testSettersAndGetters() {
        UserSearchCriteriaDTO criteria = new UserSearchCriteriaDTO();

        criteria.setRealmName("realm1");
        criteria.setUsername("user1");
        criteria.setEmail("user1@example.com");
        criteria.setEnabled(false);
        criteria.setPage(1);
        criteria.setPageSize(10);

        assertEquals("realm1", criteria.getRealmName());
        assertEquals("user1", criteria.getUsername());
        assertEquals("user1@example.com", criteria.getEmail());
        assertFalse(criteria.getEnabled());
        assertEquals(1, criteria.getPage());
        assertEquals(10, criteria.getPageSize());
    }

    @Test
    void testEqualsAndHashCode() {
        UserSearchCriteriaDTO criteria1 = UserSearchCriteriaDTO.builder()
            .realmName("test-realm")
            .username("testuser")
            .build();

        UserSearchCriteriaDTO criteria2 = UserSearchCriteriaDTO.builder()
            .realmName("test-realm")
            .username("testuser")
            .build();

        assertEquals(criteria1, criteria2);
        assertEquals(criteria1.hashCode(), criteria2.hashCode());
    }

    @Test
    void testToString() {
        UserSearchCriteriaDTO criteria = UserSearchCriteriaDTO.builder()
            .realmName("test-realm")
            .username("testuser")
            .build();

        String toString = criteria.toString();
        assertNotNull(toString);
        assertTrue(toString.contains("test-realm") || toString.contains("testuser"));
    }

    @Test
    void testHasFilters_WithFilters() {
        UserSearchCriteriaDTO criteria = UserSearchCriteriaDTO.builder()
            .username("testuser")
            .enabled(true)
            .build();

        assertTrue(criteria.hasFilters());
    }

    @Test
    void testHasFilters_NoFilters() {
        UserSearchCriteriaDTO criteria = new UserSearchCriteriaDTO();

        assertFalse(criteria.hasFilters());
    }

    @Test
    void testGetOffset() {
        UserSearchCriteriaDTO criteria = UserSearchCriteriaDTO.builder()
            .page(2)
            .pageSize(20)
            .build();

        assertEquals(40, criteria.getOffset());
    }

    @Test
    void testGetOffset_Default() {
        UserSearchCriteriaDTO criteria = new UserSearchCriteriaDTO();

        assertEquals(0, criteria.getOffset());
    }
}
