package com.mosquito.project.web;

import com.mosquito.project.service.AuthService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class UserAuthInterceptorTest {

    @Test
    @DisplayName("缺少Authorization应拒绝")
    void shouldRejectRequest_whenMissingAuthorization() {
        AuthService authService = new AuthService();
        UserAuthInterceptor interceptor = new UserAuthInterceptor(authService);
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();

        boolean result = interceptor.preHandle(request, response, new Object());

        assertFalse(result);
        assertEquals(401, response.getStatus());
    }

    @Test
    @DisplayName("空白Authorization应拒绝")
    void shouldRejectRequest_whenBlankAuthorization() {
        AuthService authService = new AuthService();
        UserAuthInterceptor interceptor = new UserAuthInterceptor(authService);
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.addHeader("Authorization", "   ");
        MockHttpServletResponse response = new MockHttpServletResponse();

        boolean result = interceptor.preHandle(request, response, new Object());

        assertFalse(result);
        assertEquals(401, response.getStatus());
    }

    @Test
    @DisplayName("无效的token应拒绝")
    void shouldRejectRequest_whenTokenIsInvalid() {
        AuthService authService = mock(AuthService.class);
        when(authService.validateToken("Bearer invalid-token")).thenReturn(null);

        UserAuthInterceptor interceptor = new UserAuthInterceptor(authService);
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.addHeader("Authorization", "Bearer invalid-token");
        MockHttpServletResponse response = new MockHttpServletResponse();

        boolean result = interceptor.preHandle(request, response, new Object());

        assertFalse(result);
        assertEquals(401, response.getStatus());
    }

    @Test
    @DisplayName("有效的token应允许访问")
    void shouldAllowRequest_whenTokenIsValid() {
        AuthService authService = mock(AuthService.class);
        AuthService.TokenInfo tokenInfo = new AuthService.TokenInfo(1L, java.time.Instant.now().plusSeconds(3600));
        when(authService.validateToken("Bearer valid-token")).thenReturn(tokenInfo);

        AuthService.UserInfo userInfo = new AuthService.UserInfo(1L, "admin", "hash", "管理员",
            1L, Arrays.asList("super_admin"), Arrays.asList("*"));
        when(authService.getUserById(1L)).thenReturn(userInfo);

        UserAuthInterceptor interceptor = new UserAuthInterceptor(authService);
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.addHeader("Authorization", "Bearer valid-token");
        MockHttpServletResponse response = new MockHttpServletResponse();

        boolean result = interceptor.preHandle(request, response, new Object());

        assertTrue(result);
        assertEquals(200, response.getStatus());
    }
}
