package de.oaa.xxx.config;

import jakarta.servlet.DispatcherType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    private final JwtFilter jwtFilter;

    public SecurityConfig(JwtFilter jwtFilter) {
        this.jwtFilter = jwtFilter;
    }

    @Bean
    SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .csrf(AbstractHttpConfigurer::disable)
            .sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
            .exceptionHandling(ex -> ex
                .authenticationEntryPoint((request, response, authException) ->
                    response.sendRedirect("/login.html")))
            .authorizeHttpRequests(auth -> auth
                .dispatcherTypeMatchers(DispatcherType.ASYNC, DispatcherType.ERROR).permitAll()
                .requestMatchers("/").permitAll()
                .requestMatchers("/error").permitAll()
                .requestMatchers("/api").permitAll()
                .requestMatchers("/userhome.html").authenticated()
                .requestMatchers("/games/chastity/toys.html").authenticated()
                .requestMatchers("/games/bdsm/aufgaben.html").authenticated()
                .requestMatchers("/games/chastity/entdecken.html").authenticated()
                .requestMatchers("/konto/profile.html").authenticated()
                .requestMatchers("/games/vanilla/infovanilla.html").authenticated()
                .requestMatchers("/games/bdsm/infobdsm.html").authenticated()
                .requestMatchers("/games/chastity/infochastity.html").authenticated()
                .requestMatchers("/games/vanilla/sessionvanilla.html").authenticated()
                .requestMatchers("/sessionbdsm.html").authenticated()
                .requestMatchers("/games/chastity/sessionchastity.html").authenticated()
                .requestMatchers("/games/chastity/neulock.html").authenticated()
                .requestMatchers("/games/chastity/activelock.html").authenticated()
                .requestMatchers("/sessionbdsmtoys.html").authenticated()
                .requestMatchers("/sessionbdsmingame.html").authenticated()
                .requestMatchers("/games/bdsm/neubdsm.html").authenticated()
                .requestMatchers("/games/bdsm/bdsmingame.html").authenticated()
                .requestMatchers("/community/personen-suchen.html").authenticated()
                .requestMatchers("/community/freunde.html").authenticated()
                .requestMatchers("/community/nachrichten.html").authenticated()
                .requestMatchers("/community/benutzer.html").authenticated()
                .requestMatchers("/community/gruppen.html").authenticated()
                .requestMatchers("/community/gruppe.html").authenticated()
                .requestMatchers("/community/feed.html").authenticated()
                .requestMatchers("/admin/admin.html").authenticated()
                .requestMatchers("/games/chastity/communityvotes.html").authenticated()
                .requestMatchers("/games/chastity/keyholder.html").authenticated()
                .requestMatchers("/games/chastity/keyholder-finden.html").authenticated()
                .requestMatchers("/games/chastity/meine-locks.html").authenticated()
                .requestMatchers("/games/chastity/entdecken-vorlagen.html").authenticated()
                .requestMatchers("/games/chastity/unlock-history.html").authenticated()
                .requestMatchers("/games/common/einladungen.html").authenticated()
                .requestMatchers("/games/chastity/joinlock.html").authenticated()
                .requestMatchers("/community/benachrichtigungen.html").authenticated()
                .requestMatchers("/community/abonnements.html").authenticated()
                .requestMatchers("/community/locations.html").authenticated()
                .requestMatchers("/community/location-detail.html").authenticated()
                .requestMatchers("/community/events.html").authenticated()
                .requestMatchers("/community/event-detail.html").authenticated()
                .requestMatchers("/gruppen/**").authenticated()
                .requestMatchers("/feed/**").authenticated()
                .requestMatchers("/notifications/**").authenticated()
                .requestMatchers("/events/**").authenticated()
                .requestMatchers("/*.html").permitAll()
                .requestMatchers("/**/*.html").permitAll()
                .requestMatchers("/help/*.html").permitAll()
                .requestMatchers("/css/**").permitAll()
                .requestMatchers("/js/**").permitAll()
                .requestMatchers("/images/**").permitAll()
                .requestMatchers("/img/**").permitAll()
                .requestMatchers("/favicon.ico").permitAll()
                .requestMatchers("/audio/**").permitAll()
                .requestMatchers("/*.png").permitAll()
                .requestMatchers("/*.jpg").permitAll()
                .requestMatchers("/*.svg").permitAll()
                .requestMatchers("/*.webp").permitAll()
                .requestMatchers(HttpMethod.GET, "/login").permitAll()
                .requestMatchers(HttpMethod.GET, "/ttlock").permitAll()
                .requestMatchers(HttpMethod.POST, "/login").permitAll()
                .requestMatchers(HttpMethod.GET, "/login/publickey").permitAll()
                .requestMatchers(HttpMethod.GET, "/login/logout").permitAll()
                .requestMatchers(HttpMethod.POST, "/user").permitAll()
                .requestMatchers(HttpMethod.GET, "/registration").permitAll()
                .requestMatchers(HttpMethod.POST, "/registration").permitAll()
                .requestMatchers(HttpMethod.GET, "/activation").permitAll()
                .requestMatchers(HttpMethod.GET, "/activation/**").permitAll()
                .requestMatchers(HttpMethod.POST, "/password-reset/request").permitAll()
                .requestMatchers(HttpMethod.POST, "/password-reset/confirm").permitAll()
                .requestMatchers(HttpMethod.GET, "/email-change/**").permitAll()
                .requestMatchers(HttpMethod.GET, "/keyholder/invitation/**").permitAll()
                .requestMatchers(HttpMethod.POST, "/api/feedback").permitAll()
                .requestMatchers(HttpMethod.POST, "/filler").permitAll()
                .requestMatchers(HttpMethod.POST, "/api/ttlock/callback").permitAll() 
                .requestMatchers(HttpMethod.GET, "/api/ttlock/callback").permitAll()
                .anyRequest().authenticated()
            )
            .addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
        return http.build();
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
