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

import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

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

/**
 * Tests unitaires pour RealmAssignmentDTO
 */
class RealmAssignmentDTOTest {

    @Test
    void testBuilder() {
        LocalDateTime now = LocalDateTime.now();
        RealmAssignmentDTO dto = RealmAssignmentDTO.builder()
            .id("assignment-1")
            .userId("user-1")
            .username("testuser")
            .email("test@example.com")
            .realmName("realm1")
            .isSuperAdmin(false)
            .active(true)
            .temporaire(false)
            .assignedAt(now)
            .assignedBy("admin")
            .raison("Test assignment")
            .commentaires("Test comments")
            .dateCreation(now)
            .dateModification(now)
            .dateExpiration(now.plusDays(30))
            .build();

        assertEquals("assignment-1", dto.getId());
        assertEquals("user-1", dto.getUserId());
        assertEquals("testuser", dto.getUsername());
        assertEquals("test@example.com", dto.getEmail());
        assertEquals("realm1", dto.getRealmName());
        assertFalse(dto.isSuperAdmin());
        assertTrue(dto.isActive());
        assertFalse(dto.getTemporaire());
        assertEquals(now, dto.getAssignedAt());
        assertEquals("admin", dto.getAssignedBy());
        assertEquals("Test assignment", dto.getRaison());
        assertEquals("Test comments", dto.getCommentaires());
    }

    @Test
    void testNoArgsConstructor() {
        RealmAssignmentDTO dto = new RealmAssignmentDTO();
        assertNotNull(dto);
    }

    @Test
    void testSettersAndGetters() {
        RealmAssignmentDTO dto = new RealmAssignmentDTO();
        LocalDateTime now = LocalDateTime.now();

        dto.setId("assignment-1");
        dto.setUserId("user-1");
        dto.setUsername("testuser");
        dto.setEmail("test@example.com");
        dto.setRealmName("realm1");
        dto.setIsSuperAdmin(true);
        dto.setActive(true);
        dto.setTemporaire(false);
        dto.setAssignedAt(now);
        dto.setAssignedBy("admin");
        dto.setRaison("Test");
        dto.setCommentaires("Comments");
        dto.setDateCreation(now);
        dto.setDateModification(now);
        dto.setDateExpiration(now.plusDays(30));

        assertEquals("assignment-1", dto.getId());
        assertEquals("user-1", dto.getUserId());
        assertEquals("testuser", dto.getUsername());
        assertEquals("test@example.com", dto.getEmail());
        assertEquals("realm1", dto.getRealmName());
        assertTrue(dto.isSuperAdmin());
        assertTrue(dto.isActive());
        assertFalse(dto.getTemporaire());
        assertEquals(now, dto.getAssignedAt());
        assertEquals("admin", dto.getAssignedBy());
    }

    @Test
    void testIsExpired_NotExpired() {
        RealmAssignmentDTO dto = RealmAssignmentDTO.builder()
            .temporaire(true)  // Doit être temporaire pour que isExpired() fonctionne
            .dateExpiration(LocalDateTime.now().plusDays(1))
            .build();

        assertFalse(dto.isExpired());
    }

    @Test
    void testIsExpired_Expired() {
        RealmAssignmentDTO dto = RealmAssignmentDTO.builder()
            .temporaire(true)  // Doit être temporaire pour que isExpired() fonctionne
            .dateExpiration(LocalDateTime.now().minusDays(1))
            .build();

        assertTrue(dto.isExpired());
    }

    @Test
    void testIsExpired_NoExpiration() {
        RealmAssignmentDTO dto = RealmAssignmentDTO.builder()
            .temporaire(true)
            .dateExpiration(null)
            .build();

        assertFalse(dto.isExpired());
    }

    @Test
    void testIsExpired_NotTemporary() {
        RealmAssignmentDTO dto = RealmAssignmentDTO.builder()
            .temporaire(false)  // Si pas temporaire, isExpired() retourne false
            .dateExpiration(LocalDateTime.now().minusDays(1))
            .build();

        assertFalse(dto.isExpired());
    }

    @Test
    void testIsTemporaire() {
        RealmAssignmentDTO dto = RealmAssignmentDTO.builder()
            .temporaire(true)
            .build();

        assertTrue(dto.isTemporaire());
    }

    @Test
    void testIsActive() {
        RealmAssignmentDTO dto = RealmAssignmentDTO.builder()
            .active(true)
            .build();

        assertTrue(dto.isActive());
    }

    @Test
    void testIsSuperAdmin() {
        RealmAssignmentDTO dto = RealmAssignmentDTO.builder()
            .isSuperAdmin(true)
            .build();

        assertTrue(dto.isSuperAdmin());
    }

    @Test
    void testGetSummary() {
        RealmAssignmentDTO dto = RealmAssignmentDTO.builder()
            .realmName("test-realm")
            .username("testuser")
            .temporaire(true)
            .build();

        String summary = dto.getSummary();
        assertNotNull(summary);
        assertTrue(summary.contains("test-realm"));
        assertTrue(summary.contains("testuser"));
        assertTrue(summary.contains("temporaire"));
    }

    @Test
    void testGetSummary_WithUserId() {
        RealmAssignmentDTO dto = RealmAssignmentDTO.builder()
            .realmName("test-realm")
            .userId("user-123")
            .temporaire(false)
            .build();

        String summary = dto.getSummary();
        assertNotNull(summary);
        assertTrue(summary.contains("test-realm"));
        assertTrue(summary.contains("user-123"));
    }
}

