package com.auth.module.security.filter;


import com.auth.common.context.BaseContext;
import com.auth.common.exception.AuthenticSecurityException;
import com.auth.common.exception.MyAuthenticationException;
import com.auth.common.model.common.result.ResultCodeEnum;
import com.auth.module.security.config.properties.SecurityConfigProperties;
import com.auth.module.security.handler.SecurityAuthenticationEntryPoint;
import com.auth.module.security.provider.JwtTokenProvider;
import com.auth.module.security.service.DbUserDetailService;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

@Slf4j
@Component
@RequiredArgsConstructor
public class JwtAuthenticationFilter extends OncePerRequestFilter {

    private final JwtTokenProvider jwtTokenProvider;
    private final DbUserDetailService userDetailsService;
    private final SecurityAuthenticationEntryPoint securityAuthenticationEntryPoint;
    private final SecurityConfigProperties pathsProperties;

    @Override
    protected void doFilterInternal(@NotNull HttpServletRequest request,
                                    @NotNull HttpServletResponse response,
                                    @NotNull FilterChain filterChain) throws ServletException, IOException {
        try {
            // ========================================
            // 💡先检查白名单路径
            // 有可能会存在，需要认证路径中包含不需要认证路径。
            // ========================================
            if (isNoAuthPath(request)) {
                filterChain.doFilter(request, response);
                return;
            }

            // 检查是否需要认证的路径
            if (!isSecurePath(request)) {
                filterChain.doFilter(request, response);
                return;
            }

            // 验证 Token
            if (validToken(request)) {
                filterChain.doFilter(request, response);
                return;
            }

            filterChain.doFilter(request, response);
        } catch (AuthenticSecurityException e) {
            // 直接处理认证异常，不再调用filterChain.doFilter()
            securityAuthenticationEntryPoint.commence(
                    request,
                    response,
                    new MyAuthenticationException(e.getMessage(), e)
            );
        } catch (RuntimeException e) {
            securityAuthenticationEntryPoint.commence(
                    request,
                    response,
                    new MyAuthenticationException("Authentication failed", e)
            );
        }
    }

    private boolean validToken(@NotNull HttpServletRequest request) {
        // 验证Token
        String authHeader = request.getHeader("Authorization");

        // Token验证
        if (authHeader == null || !authHeader.startsWith("Bearer ")) {
            return true;
            // throw new AuthenticSecurityException(ResultCodeEnum.LOGIN_AUTH);
        }

        String jwtToken = authHeader.substring(7);

        if (jwtTokenProvider.isExpired(jwtToken)) {
            throw new AuthenticSecurityException(ResultCodeEnum.AUTHENTICATION_EXPIRED);
        }

        // 设置认证信息
        String username = jwtTokenProvider.getUsernameFromToken(jwtToken);
        Long userId = jwtTokenProvider.getUserIdFromToken(jwtToken);

        if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
            UserDetails userDetails = userDetailsService.loadUserByUsername(username);
            UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(
                    userDetails,
                    null,
                    userDetails.getAuthorities()
            );
            authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            SecurityContextHolder.getContext().setAuthentication(authToken);
            BaseContext.setUsername(username);
            BaseContext.setUserId(userId);
        }
        return false;
    }

    /**
     * 是否是不用验证的路径
     */
    private boolean isNoAuthPath(HttpServletRequest request) {
        RequestMatcher[] matchers = pathsProperties.noAuthPaths.stream()
                .map(AntPathRequestMatcher::new)
                .toArray(RequestMatcher[]::new);
        return new OrRequestMatcher(matchers).matches(request);
    }

    /**
     * 是否是要验证的路径
     */
    private boolean isSecurePath(HttpServletRequest request) {
        RequestMatcher[] matchers = pathsProperties.securedPaths.stream()
                .map(AntPathRequestMatcher::new)
                .toArray(RequestMatcher[]::new);
        return new OrRequestMatcher(matchers).matches(request);
    }
}