package com.example.dateplanner.controllers.advice;

import com.example.dateplanner.models.entities.AppUser;
import com.example.dateplanner.services.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.List;

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

    private final UserService userService;

    @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> user(@AuthenticationPrincipal AppUser user){
        if(user != null){
            return userService.getUserByUuid(user.getUuid());
        }else{
            return Mono.empty();
        }
    }

}
