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

import dev.lions.user.manager.enums.audit.TypeActionAudit;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

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

/**
 * Tests unitaires pour AuditLogDTO
 */
class AuditLogDTOTest {

    @Test
    void testBuilder() {
        LocalDateTime now = LocalDateTime.now();
        AuditLogDTO log = AuditLogDTO.builder()
            .id("log-123")
            .typeAction(TypeActionAudit.USER_CREATE)
            .acteurUsername("admin")
            .acteurUserId("admin-123")
            .ressourceType("USER")
            .ressourceId("user-123")
            .ressourceName("testuser")
            .realmName("test-realm")
            .success(true)
            .description("User created")
            .dateAction(now)
            .ipAddress("192.168.1.1")
            .build();

        assertNotNull(log);
        assertEquals("log-123", log.getId());
        assertEquals(TypeActionAudit.USER_CREATE, log.getTypeAction());
        assertEquals("admin", log.getActeurUsername());
        assertEquals("admin-123", log.getActeurUserId());
        assertEquals("USER", log.getRessourceType());
        assertEquals("user-123", log.getRessourceId());
        assertEquals("testuser", log.getRessourceName());
        assertEquals("test-realm", log.getRealmName());
        assertTrue(log.isSuccessful());
        assertEquals("User created", log.getDescription());
        assertEquals(now, log.getDateAction());
        assertEquals("192.168.1.1", log.getIpAddress());
    }

    @Test
    void testNoArgsConstructor() {
        AuditLogDTO log = new AuditLogDTO();

        assertNotNull(log);
        assertNull(log.getId());
        assertNull(log.getTypeAction());
        assertNull(log.getActeurUsername());
    }

    @Test
    void testSettersAndGetters() {
        AuditLogDTO log = new AuditLogDTO();
        LocalDateTime now = LocalDateTime.now();

        log.setId("log-456");
        log.setTypeAction(TypeActionAudit.USER_UPDATE);
        log.setActeurUsername("user1");
        log.setActeurUserId("user1-123");
        log.setRessourceType("USER");
        log.setRessourceId("user-456");
        log.setRessourceName("user2");
        log.setRealmName("realm2");
        log.setSuccess(false);
        log.setDescription("Update failed");
        log.setDateAction(now);
        log.setIpAddress("10.0.0.1");

        assertEquals("log-456", log.getId());
        assertEquals(TypeActionAudit.USER_UPDATE, log.getTypeAction());
        assertEquals("user1", log.getActeurUsername());
        assertEquals("user1-123", log.getActeurUserId());
        assertEquals("USER", log.getRessourceType());
        assertEquals("user-456", log.getRessourceId());
        assertEquals("user2", log.getRessourceName());
        assertEquals("realm2", log.getRealmName());
        assertFalse(log.isSuccessful());
        assertEquals("Update failed", log.getDescription());
        assertEquals(now, log.getDateAction());
        assertEquals("10.0.0.1", log.getIpAddress());
    }

    @Test
    void testEqualsAndHashCode() {
        LocalDateTime now = LocalDateTime.now();
        AuditLogDTO log1 = AuditLogDTO.builder()
            .id("log-123")
            .typeAction(TypeActionAudit.USER_CREATE)
            .dateAction(now)
            .build();

        AuditLogDTO log2 = AuditLogDTO.builder()
            .id("log-123")
            .typeAction(TypeActionAudit.USER_CREATE)
            .dateAction(now)
            .build();

        assertEquals(log1, log2);
        assertEquals(log1.hashCode(), log2.hashCode());
    }

    @Test
    void testToString() {
        AuditLogDTO log = AuditLogDTO.builder()
            .id("log-123")
            .typeAction(TypeActionAudit.USER_CREATE)
            .build();

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