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

import org.junit.jupiter.api.Test;

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

class HealthStatusDTOTest {

    @Test
    void testBuilderAndAllFields() {
        HealthStatusDTO dto = HealthStatusDTO.builder()
                .timestamp(1699545600000L)
                .keycloakAccessible(true)
                .keycloakVersion("23.0.3")
                .realmsAccessible(true)
                .realmsCount(5)
                .overallHealthy(true)
                .errorMessage(null)
                .build();

        assertEquals(1699545600000L, dto.getTimestamp());
        assertTrue(dto.isKeycloakAccessible());
        assertEquals("23.0.3", dto.getKeycloakVersion());
        assertTrue(dto.isRealmsAccessible());
        assertEquals(5, dto.getRealmsCount());
        assertTrue(dto.isOverallHealthy());
        assertNull(dto.getErrorMessage());
    }

    @Test
    void testBuilderWithError() {
        HealthStatusDTO dto = HealthStatusDTO.builder()
                .timestamp(System.currentTimeMillis())
                .keycloakAccessible(false)
                .realmsAccessible(false)
                .overallHealthy(false)
                .errorMessage("Connection refused")
                .build();

        assertFalse(dto.isKeycloakAccessible());
        assertFalse(dto.isRealmsAccessible());
        assertFalse(dto.isOverallHealthy());
        assertEquals("Connection refused", dto.getErrorMessage());
    }

    @Test
    void testSettersAndGetters() {
        HealthStatusDTO dto = new HealthStatusDTO();
        dto.setTimestamp(12345L);
        dto.setKeycloakAccessible(true);
        dto.setKeycloakVersion("24.0.0");
        dto.setRealmsAccessible(true);
        dto.setRealmsCount(3);
        dto.setOverallHealthy(true);
        dto.setErrorMessage("warning");

        assertEquals(12345L, dto.getTimestamp());
        assertTrue(dto.isKeycloakAccessible());
        assertEquals("24.0.0", dto.getKeycloakVersion());
        assertTrue(dto.isRealmsAccessible());
        assertEquals(3, dto.getRealmsCount());
        assertTrue(dto.isOverallHealthy());
        assertEquals("warning", dto.getErrorMessage());
    }

    @Test
    void testEqualsAndHashCode() {
        HealthStatusDTO dto1 = HealthStatusDTO.builder()
                .timestamp(1000L)
                .keycloakVersion("23.0.3")
                .overallHealthy(true)
                .build();
        HealthStatusDTO dto2 = HealthStatusDTO.builder()
                .timestamp(1000L)
                .keycloakVersion("23.0.3")
                .overallHealthy(true)
                .build();
        HealthStatusDTO dto3 = HealthStatusDTO.builder()
                .timestamp(2000L)
                .keycloakVersion("24.0.0")
                .overallHealthy(false)
                .build();

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

    @Test
    void testToString() {
        HealthStatusDTO dto = HealthStatusDTO.builder()
                .keycloakVersion("23.0.3")
                .overallHealthy(true)
                .build();
        String str = dto.toString();
        assertNotNull(str);
        assertTrue(str.contains("23.0.3"));
    }

    @Test
    void testNoArgsConstructor() {
        HealthStatusDTO dto = new HealthStatusDTO();
        assertEquals(0L, dto.getTimestamp());
        assertFalse(dto.isKeycloakAccessible());
        assertNull(dto.getKeycloakVersion());
        assertFalse(dto.isRealmsAccessible());
        assertEquals(0, dto.getRealmsCount());
        assertFalse(dto.isOverallHealthy());
        assertNull(dto.getErrorMessage());
    }

    @Test
    void testAllArgsConstructor() {
        HealthStatusDTO dto = new HealthStatusDTO(
                1000L, true, "23.0.3", true, 5, true, null);

        assertEquals(1000L, dto.getTimestamp());
        assertTrue(dto.isKeycloakAccessible());
        assertEquals("23.0.3", dto.getKeycloakVersion());
        assertTrue(dto.isRealmsAccessible());
        assertEquals(5, dto.getRealmsCount());
        assertTrue(dto.isOverallHealthy());
        assertNull(dto.getErrorMessage());
    }
}
