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

import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import org.eclipse.microprofile.jwt.JsonWebToken;
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 static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
 * Tests unitaires pour AuthHeaderFactory
 */
@ExtendWith(MockitoExtension.class)
class AuthHeaderFactoryTest {

    @Mock
    private JsonWebToken jwt;

    @InjectMocks
    private AuthHeaderFactory authHeaderFactory;

    @BeforeEach
    void setUp() {
        // Setup
    }

    @Test
    void testUpdate_WithToken() {
        when(jwt.getRawToken()).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(jwt.getRawToken()).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(jwt.getRawToken()).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_WithNullJwt() {
        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);
    }

    @Test
    void testUpdate_ExceptionHandling() {
        when(jwt.getRawToken()).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"));
    }
}
