package dev.lions.user.manager.client.filter;

import io.quarkus.oidc.AccessTokenCredential;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
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 static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
 * Tests unitaires pour AuthHeaderFactory.
 *
 * L'impl utilise {@link AccessTokenCredential} (io.quarkus.oidc) injecté,
 * pas {@link org.eclipse.microprofile.jwt.JsonWebToken}.
 */
@ExtendWith(MockitoExtension.class)
class AuthHeaderFactoryTest {

    @Mock
    private AccessTokenCredential accessTokenCredential;

    @InjectMocks
    private AuthHeaderFactory authHeaderFactory;

    @Test
    void testUpdate_WithToken() {
        when(accessTokenCredential.getToken()).thenReturn("test-token-123");

        MultivaluedMap<String, String> incomingHeaders = new MultivaluedHashMap<>();
        MultivaluedMap<String, String> clientOutgoingHeaders = new MultivaluedHashMap<>();

        MultivaluedMap<String, String> result =
                authHeaderFactory.update(incomingHeaders, clientOutgoingHeaders);

        assertEquals("Bearer test-token-123", result.getFirst("Authorization"));
    }

    @Test
    void testUpdate_WithoutToken() {
        when(accessTokenCredential.getToken()).thenReturn(null);

        MultivaluedMap<String, String> incomingHeaders = new MultivaluedHashMap<>();
        MultivaluedMap<String, String> clientOutgoingHeaders = new MultivaluedHashMap<>();

        MultivaluedMap<String, String> result =
                authHeaderFactory.update(incomingHeaders, clientOutgoingHeaders);

        assertNull(result.getFirst("Authorization"));
    }

    @Test
    void testUpdate_WithEmptyToken() {
        when(accessTokenCredential.getToken()).thenReturn("");

        MultivaluedMap<String, String> incomingHeaders = new MultivaluedHashMap<>();
        MultivaluedMap<String, String> clientOutgoingHeaders = new MultivaluedHashMap<>();

        MultivaluedMap<String, String> result =
                authHeaderFactory.update(incomingHeaders, clientOutgoingHeaders);

        assertNull(result.getFirst("Authorization"));
    }

    @Test
    void testUpdate_WithNullCredential() {
        // AuthHeaderFactory avec accessTokenCredential null
        AuthHeaderFactory factory = new AuthHeaderFactory();

        MultivaluedMap<String, String> incomingHeaders = new MultivaluedHashMap<>();
        MultivaluedMap<String, String> clientOutgoingHeaders = new MultivaluedHashMap<>();

        MultivaluedMap<String, String> result =
                factory.update(incomingHeaders, clientOutgoingHeaders);

        assertNotNull(result);
        assertNull(result.getFirst("Authorization"));
    }

    @Test
    void testUpdate_ExceptionHandling() {
        when(accessTokenCredential.getToken()).thenThrow(new RuntimeException("Error"));

        MultivaluedMap<String, String> incomingHeaders = new MultivaluedHashMap<>();
        MultivaluedMap<String, String> clientOutgoingHeaders = new MultivaluedHashMap<>();

        MultivaluedMap<String, String> result =
                authHeaderFactory.update(incomingHeaders, clientOutgoingHeaders);

        assertNotNull(result);
        assertNull(result.getFirst("Authorization"));
    }
}
