package top.continew.admin.controller.business;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.business.enums.AssignmentsTypeEnum;
import top.continew.admin.business.model.req.AssignmentSubmissionsReq;
import top.continew.admin.business.model.req.AssignmentsCorrectReq;
import top.continew.admin.business.model.req.AssignmentsReq;
import top.continew.admin.business.model.req.CoursesReq;
import top.continew.admin.business.model.resp.*;
import top.continew.admin.business.service.AssignmentSubmissionsService;
import top.continew.admin.business.service.AssignmentsService;
import top.continew.admin.business.service.CoursesService;
import top.continew.admin.system.service.DeptService;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.BaseIdResp;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.web.model.R;

import java.util.List;

/**
 * @author 张波
 * @since 2024/10/22 12:26
 */
@Tag(name = "课程 API")
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/courses")
public class CoursesController {

    private final CoursesService coursesService;

    private final AssignmentsService assignmentsService;

    private final AssignmentSubmissionsService assignmentSubmissionsService;

    private final DeptService deptService;

    @Operation(summary = "创建课程", description = "创建课程")
    @PostMapping("/add")
    public R add(@RequestBody CoursesReq req) {
        coursesService.add(req);
        return R.ok();
    }

    @Operation(summary = "创建作业", description = "创建作业")
    @PostMapping("/assignments/add")
    public BaseIdResp<Long> assignmentsAdd(@Valid @RequestBody AssignmentsReq assignmentsReq) {
        Long id = assignmentsService.add(assignmentsReq);
        return BaseIdResp.<Long>builder().id(id).build();
    }

    @Operation(summary = "删除作业", description = "删除作业")
    @DeleteMapping("/assignments/delete")
    public R assignmentsDelete(Long id) {
        assignmentsService.removeById(id);
        return R.ok();
    }

    @Operation(summary = "查询课程列表", description = "查询课程列表")
    @Parameter(name = "role", description = "学生角色或者老师角色", example = "pupil/teacher", in = ParameterIn.PATH)
    //@GetMapping("/list/{role}/{db}")
    @GetMapping("/list/{role}")
    public PageResp<CoursesResp> list(@PathVariable("role") String role,
                                      //@PathVariable("db") String db,
                                      PageQuery pageQuery) {
        //todo
        /*    LoginUser loginUser = LoginHelper.getLoginUser();
        DeptResp dept = deptService.get(loginUser.getDeptId());
        String dbName = dept.getDbName();
        if (dept.getParentId() != 0) {
            DeptDO deptDO = deptService.selectDoById(loginUser.getDeptId());
            dbName = deptDO.getDbName();
        }*/
        return coursesService.list(null, role, pageQuery);
    }

    @Operation(summary = "查询课程作业列表", description = "查询课程作业列表")
    @Parameter(name = "coursesId", description = "课程ID", example = "1", in = ParameterIn.PATH)
    @Parameter(name = "type", description = "类型（作业/考试）", example = "1", in = ParameterIn.PATH)
    @Parameter(name = "role", description = "学生角色或者老师角色", example = "pupil/teacher", in = ParameterIn.PATH)
    @GetMapping("/assignments/list/{coursesId}/{type}/{role}")
    public R<List<AssignmentsResp>> assignmentsList(@PathVariable Long coursesId,
                                                    @PathVariable AssignmentsTypeEnum type,
                                                    @PathVariable("role") String role) {
        return R.ok(coursesService.assignmentsList(coursesId, type, role));
    }

    @Operation(summary = "查询作业完成情况")
    @GetMapping("/assignments/degreeOfCompletion")
    public PageResp<AssignmentsProgressResp> assignmentsDegreeOfCompletion(Long assignmentsId, PageQuery pageQuery) {
        return assignmentSubmissionsService.degreeOfCompletion(assignmentsId, pageQuery);
    }

    @Operation(summary = "作业详情查询")
    @GetMapping("/assignments/detail")
    public R<AssignmentsDetailResp> assignmentsDetail(Long assignmentsId) {
        return R.ok(assignmentsService.detail(assignmentsId));
    }

    @Operation(summary = "作业提交记录详情查询")
    @GetMapping("/assignments/submissions/detail")
    public R<AssignmentsSubmissionsDetailResp> assignmentsSubmissionSDetail(Long submissionsId) {
        return R.ok(assignmentSubmissionsService.detail(submissionsId));
    }

    @Operation(summary = "提交作业")
    @PutMapping("/assignments/submit")
    public R assignmentsSubmit(@Valid @RequestBody AssignmentSubmissionsReq req) {
        assignmentSubmissionsService.submit(req);
        return R.ok();
    }

    @Operation(summary = "批改作业")
    @PutMapping("/assignments/correct")
    public R assignmentsCorrect(@Valid @RequestBody AssignmentsCorrectReq req) {
        assignmentSubmissionsService.correct(req);
        return R.ok();
    }

}
