package rd.controller.teacher;

import cn.dev33.satoken.annotation.SaCheckRole;
import rd.data.dto.user.teacher.TeacherUserDTO;
import rd.data.db.table.TeacherInfo;
import rd.data.model.request.admin.TeacherAddRequest;
import rd.data.model.response.Response;
import rd.service.user.TeacherUserService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/user/teacher")
public class AdminTeacherController {
    private final TeacherUserService teacherUserService;

    public AdminTeacherController(TeacherUserService teacherUserService) {
        this.teacherUserService = teacherUserService;
    }

    @SaCheckRole("ADMIN")
    @PostMapping("/add")
    public Response<TeacherInfo> add(@RequestBody TeacherAddRequest request) {
        String username = request.username();
        String teacherName = request.teacherName();
        String initialPassword = request.initialPassword();

        TeacherUserDTO dto = new TeacherUserDTO(username, teacherName, initialPassword);

        var teacherInfo = teacherUserService.add(dto);
        return Response.success(teacherInfo);
    }

    @SaCheckRole("ADMIN")
    @PostMapping("/update")
    public Response<TeacherInfo> update(@RequestParam("id") Long id, @RequestBody TeacherAddRequest request) {
        String username = request.username();
        String teacherName = request.teacherName();
        String initialPassword = request.initialPassword();

        TeacherUserDTO dto = new TeacherUserDTO(username, teacherName, initialPassword);
        var teacherInfo = teacherUserService.update(id, dto);

        return Response.success(teacherInfo);
    }

    @SaCheckRole("ADMIN")
    @PostMapping("/delete")
    public Response<TeacherInfo> delete(@RequestParam("id") Long id) {
        teacherUserService.delete(id);
        return Response.success();
    }
}
