package rd.controller.schedule;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;
import rd.data.dto.schedule.CourseScheduleDetailDTO;
import rd.data.dto.schedule.CourseScheduleWithCourseNameDTO;
import rd.data.db.CourseScheduleWithCourseIdName;
import rd.data.db.table.TeacherInfo;
import rd.data.model.response.Response;
import rd.service.schedule.CourseScheduleService;
import rd.service.schedule.StudentCourseScheduleService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import rd.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/student/course-schedule")
public class StudentScheduleController {

    private final CourseScheduleService courseScheduleService;
    private final StudentCourseScheduleService studentCourseScheduleService;

    public StudentScheduleController(CourseScheduleService courseScheduleService,
                                     StudentCourseScheduleService studentCourseScheduleService) {

        this.courseScheduleService = courseScheduleService;
        this.studentCourseScheduleService = studentCourseScheduleService;
    }

    @SaCheckRole("STUDENT")
    @RequestMapping("/result")
    public Response<List<CourseScheduleDetailDTO>> list(
            @RequestParam(value = "term", required = false, defaultValue = "") String term
    ) {
        if (term == null || term.isEmpty()) {
            term = StringUtil.getCurrentTermString();
        }

        var uid = StpUtil.getLoginIdAsLong();
        var schedules = courseScheduleService.getStudentTermSchedule(uid, term);

        var result = schedules.stream().map(StudentScheduleController::convert).toList();

        return Response.success(result);
    }

    private static CourseScheduleDetailDTO convert(CourseScheduleWithCourseIdName courseScheduleWithCourseIdName) {
        CourseScheduleDetailDTO courseScheduleDetailDTO = new CourseScheduleDetailDTO();
        courseScheduleDetailDTO.setCourseId(courseScheduleWithCourseIdName.getCourseId());
        courseScheduleDetailDTO.setScheduleId(courseScheduleWithCourseIdName.getScheduleId());
        courseScheduleDetailDTO.setCourseName(courseScheduleWithCourseIdName.getCourseName());
        courseScheduleDetailDTO.setCourseHours(courseScheduleWithCourseIdName.getCourseHours());
        courseScheduleDetailDTO.setStartWeek(courseScheduleWithCourseIdName.getStartWeek());
        courseScheduleDetailDTO.setEndWeek(courseScheduleWithCourseIdName.getEndWeek());
        courseScheduleDetailDTO.setTime(courseScheduleWithCourseIdName.getTime());
        courseScheduleDetailDTO.setWeekday(courseScheduleWithCourseIdName.getWeekday());
        courseScheduleDetailDTO.setAcademicTerm(courseScheduleWithCourseIdName.getAcademicTerm());
        courseScheduleDetailDTO.setSpringTerm(courseScheduleWithCourseIdName.getSpringTerm());
        courseScheduleDetailDTO.setPlace(courseScheduleWithCourseIdName.getPlace());
        courseScheduleDetailDTO.setExamRatio(courseScheduleWithCourseIdName.getExamRatio());
        courseScheduleDetailDTO.setCreateTime(courseScheduleWithCourseIdName.getCreateTime());

        return courseScheduleDetailDTO;
    }

    @SaCheckRole("STUDENT")
    @RequestMapping("/select")
    public Response<Object> select(@RequestParam("schedule") Long schedule,
                                   @RequestParam("activity") Long activity
    ) {
        Long uid = StpUtil.getLoginIdAsLong();

        studentCourseScheduleService.selectSchedule(uid, schedule, activity);

        return Response.success();
    }

    @SaCheckRole("STUDENT")
    @RequestMapping("/unselect")
    public Response<Object> unselect(@RequestParam("id") Long schedule) {
        Long uid = StpUtil.getLoginIdAsLong();

        studentCourseScheduleService.unselect(uid, schedule);

        return Response.success();
    }

    @SaCheckRole("STUDENT")
    @RequestMapping("/check-selected")
    public Response<Boolean> select(@RequestParam("id") Long schedule) {
        Long uid = StpUtil.getLoginIdAsLong();
        var result = studentCourseScheduleService.isStudentSelected(uid, schedule);

        return Response.success(result);
    }
}
