package dev.lions.user.manager.resource;

import dev.lions.user.manager.client.KeycloakAdminClient;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.keycloak.admin.client.Keycloak;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Map;

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

@ExtendWith(MockitoExtension.class)
class HealthResourceEndpointTest {

    @Mock
    KeycloakAdminClient keycloakAdminClient;

    @Mock
    Keycloak keycloak;

    @InjectMocks
    HealthResourceEndpoint healthResourceEndpoint;

    @Test
    void testGetKeycloakHealthConnected() {
        when(keycloakAdminClient.getInstance()).thenReturn(keycloak);

        Map<String, Object> result = healthResourceEndpoint.getKeycloakHealth();

        assertNotNull(result);
        assertEquals("UP", result.get("status"));
        assertEquals(true, result.get("connected"));
        assertNotNull(result.get("timestamp"));
    }

    @Test
    void testGetKeycloakHealthDisconnected() {
        when(keycloakAdminClient.getInstance()).thenReturn(null);

        Map<String, Object> result = healthResourceEndpoint.getKeycloakHealth();

        assertNotNull(result);
        assertEquals("DOWN", result.get("status"));
        assertEquals(false, result.get("connected"));
    }

    @Test
    void testGetKeycloakHealthError() {
        when(keycloakAdminClient.getInstance()).thenThrow(new RuntimeException("Connection error"));

        Map<String, Object> result = healthResourceEndpoint.getKeycloakHealth();

        assertNotNull(result);
        assertEquals("ERROR", result.get("status"));
        assertEquals(false, result.get("connected"));
        assertEquals("Connection error", result.get("error"));
    }

    @Test
    void testGetServiceStatusConnected() {
        when(keycloakAdminClient.isConnected()).thenReturn(true);

        Map<String, Object> result = healthResourceEndpoint.getServiceStatus();

        assertNotNull(result);
        assertEquals("lions-user-manager-server", result.get("service"));
        assertEquals("1.0.0", result.get("version"));
        assertEquals("UP", result.get("status"));
        assertEquals("CONNECTED", result.get("keycloak"));
        assertNotNull(result.get("timestamp"));
    }

    @Test
    void testGetServiceStatusDisconnected() {
        when(keycloakAdminClient.isConnected()).thenReturn(false);

        Map<String, Object> result = healthResourceEndpoint.getServiceStatus();

        assertNotNull(result);
        assertEquals("UP", result.get("status"));
        assertEquals("DISCONNECTED", result.get("keycloak"));
    }

    @Test
    void testGetServiceStatusKeycloakError() {
        when(keycloakAdminClient.isConnected()).thenThrow(new RuntimeException("Error"));

        Map<String, Object> result = healthResourceEndpoint.getServiceStatus();

        assertNotNull(result);
        assertEquals("UP", result.get("status"));
        assertEquals("ERROR", result.get("keycloak"));
        assertEquals("Error", result.get("keycloakError"));
    }
}
