package rd.controller.schedule;

import rd.data.dto.schedule.CourseScheduleDTO;
import rd.data.model.response.PaginationWrapper;
import rd.data.model.response.Response;
import rd.service.schedule.CourseScheduleService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/course-schedule")
public class ScheduleController {

    private final CourseScheduleService courseScheduleService;

    public ScheduleController(CourseScheduleService courseScheduleService) {
        this.courseScheduleService = courseScheduleService;
    }

    @RequestMapping("/list")
    public Response<PaginationWrapper<List<CourseScheduleDTO>>> list(
            @RequestParam(value = "course") Long course,
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
            @RequestParam(value = "term", required = false) String term
    ) {
        if (term == null || term.isEmpty() | term.equals("全部")) {
            term = null;
        }

        var result = courseScheduleService.getScheduleOfCourse(course, term, page, pageSize);
        return Response.success(result);
    }

    @RequestMapping("/list-available")
    public Response<List<CourseScheduleDTO>> getAllForCourse(
            @RequestParam(value = "course") Long course,
            @RequestParam(value = "term") String term
    ) {

        var result = courseScheduleService.getTermCourseSchedule(course, term);
        return Response.success(result);
    }

    @RequestMapping("/info")
    public Response<CourseScheduleDTO> info(@RequestParam("id") Long id) {
        var result = courseScheduleService.getScheduleDTO(id);
        return Response.success(result);
    }

    @RequestMapping("/selected-count")
    public Response<Integer> selectCount(@RequestParam("id") Long id) {
        var result = courseScheduleService.getSelectedCount(id);
        return Response.success(result);
    }
}
