package f1.controller;

import f1.db.UserDao;
import f1.entity.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserDao userDao;

    @GetMapping
    public User getCurrentUser(Principal principal) {
        if (principal == null) {
            return null; // Or throw exception / return 401
        }
        String username = principal.getName();
        // getUserByUsername returns full user, but password is now @JsonIgnore-d
        return userDao.getUserByUsername(username);
    }

    @GetMapping("/{id}")
    public User getUserById(@PathVariable int id) {
        return userDao.getUserById(id);
    }
}
