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

import org.junit.jupiter.api.Test;

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

class SyncResultDTOTest {

    @Test
    void testGetDurationMs() {
        SyncResultDTO dto = SyncResultDTO.builder()
                .startTime(1000L)
                .endTime(5000L)
                .build();
        assertEquals(4000L, dto.getDurationMs());
    }

    @Test
    void testGetDurationMs_sameTime() {
        SyncResultDTO dto = SyncResultDTO.builder()
                .startTime(1000L)
                .endTime(1000L)
                .build();
        assertEquals(0L, dto.getDurationMs());
    }

    @Test
    void testBuilderAndAllFields() {
        SyncResultDTO dto = SyncResultDTO.builder()
                .realmName("btpxpress")
                .usersCount(150)
                .realmRolesCount(25)
                .clientRolesCount(50)
                .success(true)
                .errorMessage(null)
                .startTime(1699545600000L)
                .endTime(1699545615000L)
                .build();

        assertEquals("btpxpress", dto.getRealmName());
        assertEquals(150, dto.getUsersCount());
        assertEquals(25, dto.getRealmRolesCount());
        assertEquals(50, dto.getClientRolesCount());
        assertTrue(dto.isSuccess());
        assertNull(dto.getErrorMessage());
        assertEquals(1699545600000L, dto.getStartTime());
        assertEquals(1699545615000L, dto.getEndTime());
        assertEquals(15000L, dto.getDurationMs());
    }

    @Test
    void testBuilderWithError() {
        SyncResultDTO dto = SyncResultDTO.builder()
                .realmName("btpxpress")
                .success(false)
                .errorMessage("Connection failed")
                .build();

        assertEquals("btpxpress", dto.getRealmName());
        assertFalse(dto.isSuccess());
        assertEquals("Connection failed", dto.getErrorMessage());
    }

    @Test
    void testSettersAndGetters() {
        SyncResultDTO dto = new SyncResultDTO();
        dto.setRealmName("realm");
        dto.setUsersCount(10);
        dto.setRealmRolesCount(5);
        dto.setClientRolesCount(3);
        dto.setSuccess(true);
        dto.setErrorMessage("error");
        dto.setStartTime(100L);
        dto.setEndTime(200L);

        assertEquals("realm", dto.getRealmName());
        assertEquals(10, dto.getUsersCount());
        assertEquals(5, dto.getRealmRolesCount());
        assertEquals(3, dto.getClientRolesCount());
        assertTrue(dto.isSuccess());
        assertEquals("error", dto.getErrorMessage());
        assertEquals(100L, dto.getStartTime());
        assertEquals(200L, dto.getEndTime());
    }

    @Test
    void testEqualsAndHashCode() {
        SyncResultDTO dto1 = SyncResultDTO.builder().realmName("realm").usersCount(10).build();
        SyncResultDTO dto2 = SyncResultDTO.builder().realmName("realm").usersCount(10).build();
        SyncResultDTO dto3 = SyncResultDTO.builder().realmName("other").usersCount(5).build();

        assertEquals(dto1, dto2);
        assertEquals(dto1.hashCode(), dto2.hashCode());
        assertNotEquals(dto1, dto3);
    }

    @Test
    void testToString() {
        SyncResultDTO dto = SyncResultDTO.builder().realmName("realm").success(true).build();
        String str = dto.toString();
        assertNotNull(str);
        assertTrue(str.contains("realm"));
    }

    @Test
    void testNoArgsConstructor() {
        SyncResultDTO dto = new SyncResultDTO();
        assertNull(dto.getRealmName());
        assertEquals(0, dto.getUsersCount());
        assertFalse(dto.isSuccess());
    }

    @Test
    void testAllArgsConstructor() {
        SyncResultDTO dto = new SyncResultDTO(
                "realm", 100, 20, 30, true, null, 1000L, 2000L);

        assertEquals("realm", dto.getRealmName());
        assertEquals(100, dto.getUsersCount());
        assertEquals(20, dto.getRealmRolesCount());
        assertEquals(30, dto.getClientRolesCount());
        assertTrue(dto.isSuccess());
        assertNull(dto.getErrorMessage());
        assertEquals(1000L, dto.getStartTime());
        assertEquals(2000L, dto.getEndTime());
    }
}
