package rd.controller.user;

import cn.dev33.satoken.stp.StpUtil;
import rd.data.code.ServiceCode;
import rd.data.enums.UserType;
import rd.data.model.response.Response;

import rd.service.user.UserInfoService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/info")
public class UserInfoController {
    private final UserInfoService userInfoService;

    public UserInfoController(UserInfoService userInfoService) {
        this.userInfoService = userInfoService;
    }

    @RequestMapping("")
    public Object getUserInfo(@RequestParam("uid") Long uid, @RequestParam("userType") String userType) {
        // 如果不是自己的账号并且也不是管理员
        if (!uid.equals(StpUtil.getLoginIdAsLong()) && !StpUtil.hasRole(UserType.ADMIN.name())) {
            return Response.error(ServiceCode.PermissionDenied);
        }

        Object userInfo = switch (userType) {
            case "STUDENT" -> userInfoService.getAsStudent(uid);
            case "TEACHER" -> userInfoService.getAsTeacher(uid);
            default -> null;
        };

        return Response.success(userInfo);
    }
}
