package enea.lecservice.controllers;

import enea.lecservice.models.dto.ServiceResponse;
import enea.lecservice.security.dto.UserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

@RestController
@RequiredArgsConstructor
@CrossOrigin("*")
public class HomeController {

    @GetMapping("/")
    public String home() {
        return "Servizio attivo.";
    }

    @GetMapping("/user")
    public ServiceResponse<String> user(Principal principal) {
        UserInfo userInfo = (UserInfo) ((UsernamePasswordAuthenticationToken) principal).getPrincipal();
        return ServiceResponse.Success(String.format("Logged in user: %s (%s %s)", userInfo.getUsername(), userInfo.getName(), userInfo.getSurname()));
    }

    @GetMapping("/admin")
    public String admin(Principal principal) {
        UserInfo userInfo = (UserInfo) ((UsernamePasswordAuthenticationToken) principal).getPrincipal();
        return String.format("Servizio attivo per ADMIN: %s %s", userInfo.getName(), userInfo.getSurname());
    }
}

