package com.example.dateplanner.controllers.advice;

import com.example.dateplanner.models.entities.AppUser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@ControllerAdvice
@PropertySource("classpath:application.properties")
public class SecurityAdviceController {

    @Value("${minio.cdn}")
    private String cdn;

    @ModelAttribute(name = "cdn")
    public Mono<String> cdnUrl() {
        return Mono.just(cdn);
    }

    @ModelAttribute(name = "auth")
    public Mono<Boolean> isAuthenticate(@AuthenticationPrincipal AppUser user){
        if(user != null){
            return Mono.just(true);
        }else{
            return Mono.just(false);
        }
    }

    @ModelAttribute(name = "user")
    public Mono<AppUser> provideUser(@AuthenticationPrincipal AppUser user){
        return Mono.justOrEmpty(user);
    }

//    @ModelAttribute(name = "_csrf")
//    public Mono<CsrfToken> provideCsrfToken(ServerWebExchange exchange) {
//        return exchange.getAttribute(CsrfToken.class.getName());
//    }
}
