package rd.controller.student;

import cn.dev33.satoken.annotation.SaCheckRole;
import rd.data.code.ServiceCode;
import rd.data.dto.user.student.BasicStudentInfoDto;
import rd.data.dto.user.student.BasicStudentInfoWithMajorNameDto;
import rd.data.db.table.StudentInfo;
import rd.data.db.table.TeacherInfo;
import rd.data.model.request.admin.StudentAddWithMajorIdRequest;
import rd.data.model.request.admin.StudentAddWithMajorNameRequest;
import rd.data.model.response.Response;

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

@RestController
@RequestMapping("/admin/user/student")
public class AdminStudentController {
    private final StudentUserService studentUserService;

    public AdminStudentController(StudentUserService studentUserService) {
        this.studentUserService = studentUserService;
    }

    @SaCheckRole("ADMIN")
    @PostMapping("/add")
    public Response<StudentInfo> add(@RequestBody StudentAddWithMajorIdRequest request) {
        var dto = new BasicStudentInfoDto(
                request.studentNumber(), request.studentName(), request.majorId()
        );

        var teacherInfo = studentUserService.add(dto);
        return Response.success(teacherInfo);
    }
//
//    @SaCheckRole("ADMIN")
//    @PostMapping("/add-batch")
//    public Response<StudentInfo> addBatch(@RequestBody StudentBatchAddRequest request) {
//        var dto = new BasicStudentInfoDto(request.studentNumber(), request.studentName(), request.major());
//
//        var teacherInfo = studentUserService.add(dto);
//        return Response.success(teacherInfo);
//    }

    @SaCheckRole("ADMIN")
    @PostMapping("/add-with-auto-major")
    public Response<StudentInfo> addWithAutoMajor(@RequestBody StudentAddWithMajorNameRequest request) {
        if (request.studentNumber() == null || request.studentName() == null || request.majorName() == null) {
            return Response.error(ServiceCode.ParamWrong);
        }
        var dto = new BasicStudentInfoWithMajorNameDto(
                request.studentNumber(), request.studentName(), request.majorName()
        );

        var teacherInfo = studentUserService.addWithAutoMajor(dto);
        return Response.success(teacherInfo);
    }
//
//    @SaCheckRole("ADMIN")
//    @PostMapping("/update")
//    public Response<StudentInfo> update(@RequestParam("id") String id, @RequestBody StudentAddRequest request) {
//        String username = request.studentNumber();
//        String teacherName = request.studentName();
//        String initialPassword = request.initialPassword();
//
//        var dto = new BasicStudentInfoDto(request.studentNumber(), request.studentName());
//
//        var teacherInfo = studentUserService.update(id, dto);
//
//        return Response.success(teacherInfo);
//    }

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