package dev.lions.user.manager.resource;

import dev.lions.user.manager.client.KeycloakAdminClient;
import io.quarkus.security.identity.SecurityIdentity;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

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

/**
 * Tests unitaires pour RealmResource
 */
@ExtendWith(MockitoExtension.class)
class RealmResourceTest {

    @Mock
    private KeycloakAdminClient keycloakAdminClient;

    @Mock
    private SecurityIdentity securityIdentity;

    @InjectMocks
    private RealmResource realmResource;

    @Test
    void testGetAllRealms_Success() {
        List<String> realms = Arrays.asList("master", "lions-user-manager", "btpxpress");
        when(keycloakAdminClient.getAllRealms()).thenReturn(realms);

        List<String> result = realmResource.getAllRealms();

        assertNotNull(result);
        assertEquals(3, result.size());
        assertEquals("master", result.get(0));
        verify(keycloakAdminClient).getAllRealms();
    }

    @Test
    void testGetAllRealms_EmptyList() {
        when(keycloakAdminClient.getAllRealms()).thenReturn(Collections.emptyList());

        List<String> result = realmResource.getAllRealms();

        assertNotNull(result);
        assertTrue(result.isEmpty());
    }

    @Test
    void testGetAllRealms_Exception() {
        when(keycloakAdminClient.getAllRealms()).thenThrow(new RuntimeException("Keycloak connection error"));

        assertThrows(RuntimeException.class, () -> realmResource.getAllRealms());
    }
}
