package kr.lunaticbum.back.lun.configs.core

import kr.lunaticbum.back.lun.configs.web.BumsInterceptor
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.CacheControl
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import java.time.Duration


@Configuration
class AppConfig : WebMvcConfigurer {
    @Value("\${resource.handler}")
    private val resourceHandler: String? = null

    @Value("\${resource.location}")
    private val resourceLocation: String? = null

    val cacheControl: CacheControl = CacheControl.maxAge(Duration.ofHours(1))

    @Bean
    fun authInterceptor(): BumsInterceptor {
        return BumsInterceptor()
    }
    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {

        registry.addResourceHandler(resourceHandler).addResourceLocations(resourceLocation).setCacheControl(cacheControl)
    }

    @Bean
    fun passwordEncoder(): PasswordEncoder = BCryptPasswordEncoder()

    override fun addInterceptors(registry: InterceptorRegistry) {
        registry.addInterceptor(authInterceptor())
            .addPathPatterns(
                "/home.bs",
                "/bums/where.bs",
                "/user/info", // "내 정보" 페이지도 추가하면 좋습니다.
                "/tlg/repotToMe.bjx",
                "/user/login.bs", "/user/signup.bs", "/user/login.bjx","/bookmarks/**",
                "/blog/viewer/**", "/blog/posts", "/blog/rankOfViews.bjx", "/blog/recentOfPost.bjx"
            )
    }



}