package test05.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import test05.enums.UserRole;
import test05.pojo.Result;
import test05.pojo.User;
import test05.service.UserService;
import test05.utils.UserRoleUtils;

/**
 * 用户管理控制器 - 处理用户创建功能
 */
@RestController
@RequestMapping("/userManagementAction")
public class UserManagementController {
    private final UserService userService;
    
    @Autowired
    public UserManagementController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 创建老师用户 - 仅管理员可访问
     */
    @RequestMapping("/createTeacher")
    public Result createTeacher(String username, String password, HttpServletRequest request) {
        String token = request.getHeader("token");
        UserRole creatorRole = UserRoleUtils.getUserRoleFromToken(token);
        
        // 检查权限
        if (!UserRoleUtils.canCreateUser(creatorRole, UserRole.TEACHER)) {
            return Result.error("access denied");
        }
        
        // 验证用户名格式
        if (!username.startsWith(UserRole.TEACHER.getPrefix())) {
            return Result.error("teacher username must start with 't_'");
        }
        
        if (!UserRole.isValidUsername(username)) {
            return Result.error("invalid username format");
        }
        
        // 创建用户
        User newUser = new User(username, password);
        return userService.addUser(newUser);
    }

    /**
     * 创建学生用户 - 管理员和老师可访问
     */
    @RequestMapping("/createStudent")
    public Result createStudent(String username, String password, HttpServletRequest request) {
        String token = request.getHeader("token");
        UserRole creatorRole = UserRoleUtils.getUserRoleFromToken(token);
        
        // 检查权限
        if (!UserRoleUtils.canCreateUser(creatorRole, UserRole.STUDENT)) {
            return Result.error("access denied");
        }
        
        // 验证用户名格式
        if (!username.startsWith(UserRole.STUDENT.getPrefix())) {
            return Result.error("student username must start with 's_'");
        }
        
        if (!UserRole.isValidUsername(username)) {
            return Result.error("invalid username format");
        }
        
        // 创建用户
        User newUser = new User(username, password);
        return userService.addUser(newUser);
    }

    /**
     * 创建管理员用户 - 仅管理员可访问
     */
    @RequestMapping("/createAdmin")
    public Result createAdmin(String username, String password, HttpServletRequest request) {
        String token = request.getHeader("token");
        UserRole creatorRole = UserRoleUtils.getUserRoleFromToken(token);
        
        // 只有管理员可以创建管理员
        if (creatorRole != UserRole.ADMIN) {
            return Result.error("access denied");
        }
        
        // 验证用户名格式
        if (!username.startsWith(UserRole.ADMIN.getPrefix())) {
            return Result.error("admin username must start with 'a_'");
        }
        
        if (!UserRole.isValidUsername(username)) {
            return Result.error("invalid username format");
        }
        
        // 创建用户
        User newUser = new User(username, password);
        return userService.addUser(newUser);
    }
}