package dev.lions.user.manager.resource;

import dev.lions.user.manager.client.KeycloakAdminClient;
import io.quarkus.security.identity.SecurityIdentity;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
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.List;

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

/**
 * Tests supplémentaires pour RealmResource pour améliorer la couverture
 */
@ExtendWith(MockitoExtension.class)
class RealmResourceAdditionalTest {

    @Mock
    private KeycloakAdminClient keycloakAdminClient;

    @Mock
    private SecurityIdentity securityIdentity;

    @InjectMocks
    private RealmResource realmResource;

    @BeforeEach
    void setUp() {
        // Setup
    }

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

        Response response = realmResource.getAllRealms();

        assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
        @SuppressWarnings("unchecked")
        List<String> responseRealms = (List<String>) response.getEntity();
        assertEquals(3, responseRealms.size());
    }

    @Test
    void testGetAllRealms_Empty() {
        when(keycloakAdminClient.getAllRealms()).thenReturn(List.of());

        Response response = realmResource.getAllRealms();

        assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
        @SuppressWarnings("unchecked")
        List<String> responseRealms = (List<String>) response.getEntity();
        assertTrue(responseRealms.isEmpty());
    }

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

        Response response = realmResource.getAllRealms();

        assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
    }
}

