package quant.rich.emoney.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

import lombok.RequiredArgsConstructor;
import quant.rich.emoney.service.AuthService;

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {

    private final AuthService userDetailsService;
    
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .headers(headers -> headers
                    .cacheControl(cache -> cache.disable())
                    .frameOptions(f -> f.sameOrigin())
                    )
            .csrf(csrf -> csrf.disable())
            .authorizeHttpRequests(auth -> auth
                .requestMatchers("/favicon.ico").permitAll()
                .requestMatchers("/admin/*/login").permitAll()
                .requestMatchers("/admin/*/static/**").permitAll()
                .requestMatchers("/public/**").permitAll()
                .requestMatchers("/captcha/**").permitAll()
                .requestMatchers("/api/**").permitAll()
                .requestMatchers("/img/**").permitAll()
                .anyRequest().authenticated()
            )
            .formLogin(form -> form  // 开启表单登录，并指定登录页
                    .loginPage("/admin/v1/login")  // 指定登录页
                    .loginProcessingUrl("/admin/v1/doLogin")  // 处理登录请求的 URL
                    .defaultSuccessUrl("/admin/v1/", false)  // 登录成功后默认跳转
                    .permitAll())
            .logout(logout -> logout
                .logoutUrl("/admin/v1/logout")
                .logoutSuccessUrl("/admin/v1/login")
                .invalidateHttpSession(true)
                .permitAll())
            .sessionManagement(session -> session
                    .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
                );
            ;
        
        return http.build();
    }
    
    @Bean
    public AuthenticationManager authenticationManager(@Autowired PasswordEncoder passwordEncoder) {
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
        provider.setUserDetailsService(userDetailsService);
        provider.setPasswordEncoder(passwordEncoder);
        return new ProviderManager(provider);
    }
    
    @SuppressWarnings("deprecation")
    @Bean
    public PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }
}