package rd.controller.course;

import cn.dev33.satoken.annotation.SaCheckRole;
import rd.data.model.request.course.CourseTeacherUpdateRequest;
import rd.data.model.response.Response;
import rd.data.model.response.course.CourseTeacherResponse;
import rd.service.course.CourseTeacherService;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

@RestController
@RequestMapping("/course/teachers")
public class CourseTeacherController {
    private final CourseTeacherService courseTeacherService;

    public CourseTeacherController(CourseTeacherService courseTeacherService) {
        this.courseTeacherService = courseTeacherService;
    }

    @GetMapping("/list")
    public Response<List<CourseTeacherResponse>> getCourseTeachers(@RequestParam("course") Long course) {
        var courseTeacherList = courseTeacherService.getCourseTeacherList(course);
        var courseTeacherResponseList = courseTeacherList.stream()
                .map(teacherInfo -> new CourseTeacherResponse(teacherInfo.getId(), teacherInfo.getName()))
                .toList();

        return Response.success(courseTeacherResponseList);
    }

    @SaCheckRole({"ADMIN"})
    @PostMapping("/add")
    public Response<List<CourseTeacherResponse>> addCourseTeacher(@RequestParam("course") Long courseId,
                                                                  @RequestParam("teacher") Long teacherId
    ) {
        var courseTeacherList = courseTeacherService.addCourseTeacher(courseId, teacherId);
        var courseTeacherResponseList = courseTeacherList.stream()
                .map(teacherInfo -> new CourseTeacherResponse(teacherInfo.getId(), teacherInfo.getName()))
                .toList();

        return Response.success(courseTeacherResponseList);
    }

    @SaCheckRole({"ADMIN"})
    @PostMapping("/remove")
    public Response<List<CourseTeacherResponse>> removeCourseTeacher(@RequestParam("course") Long courseId,
                                                                     @RequestParam("teacher") Long teacherId
    ) {
        var courseTeacherList = courseTeacherService.removeCourseTeacher(courseId, teacherId);
        var courseTeacherResponseList = courseTeacherList.stream()
                .map(teacherInfo -> new CourseTeacherResponse(teacherInfo.getId(), teacherInfo.getName()))
                .toList();

        return Response.success(courseTeacherResponseList);
    }

    @SaCheckRole({"ADMIN"})
    @PostMapping("/update")
    public Response<List<CourseTeacherResponse>> updateCourseTeacher(@RequestParam("course") Long courseId,
                                                                     @RequestBody CourseTeacherUpdateRequest courseTeacherUpdateRequest
    ) {
        var courseTeacherIdSet = new HashSet<>(courseTeacherUpdateRequest.getTeachers());
        var courseTeacherList = courseTeacherService.updateCourseTeacherList(courseId, courseTeacherIdSet);

        var courseTeacherResponseList = courseTeacherList.stream()
                .map(teacherInfo -> new CourseTeacherResponse(teacherInfo.getId(), teacherInfo.getName()))
                .toList();

        return Response.success(courseTeacherResponseList);
    }
}
