package rd.controller.course;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import rd.data.dto.course.CourseDTO;
import rd.data.model.request.course.CourseRequest;
import rd.data.model.response.Response;
import rd.service.course.CourseService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/course")
public class AdminCourseController {
    private final CourseService courseService;

    public AdminCourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    @SaCheckLogin
    @SaCheckRole("ADMIN")
    @PostMapping("/add")
    public Response<CourseDTO> add(@RequestBody CourseRequest request) {
        CourseDTO dto = CourseDTO.builder()
                .name(request.getName())
                .courseHours(request.getCourseHours())
                .springTerm(request.getSpringTerm())
                .examRatio(request.getExamRatio())
                .build();

        var newCourse = courseService.add(dto);

        return Response.success(newCourse);
    }

    @SaCheckLogin
    @SaCheckRole("ADMIN")
    @PostMapping("/update")
    public Response<CourseDTO> update(@RequestParam("id") Long id, @RequestBody CourseRequest request) {
        CourseDTO dto = CourseDTO.builder()
                .name(request.getName())
                .courseHours(request.getCourseHours())
                .springTerm(request.getSpringTerm())
                .examRatio(request.getExamRatio())
                .build();

        var teacherInfo = courseService.update(id, dto);

        return Response.success(teacherInfo);
    }

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