package cn.reghao.tnb.content.app.exam.controller;

import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.content.app.exam.model.constant.PaperViewType;
import cn.reghao.tnb.content.app.exam.model.dto.UserResult;
import cn.reghao.tnb.content.app.exam.model.po.PaperResult;
import cn.reghao.tnb.content.app.exam.model.query.ExamQuery;
import cn.reghao.tnb.content.app.exam.model.vo.ExamMark;
import cn.reghao.tnb.content.app.exam.model.vo.PaperDetail;
import cn.reghao.tnb.content.app.exam.service.MarkService;
import cn.reghao.tnb.content.app.exam.service.PaperService;
import cn.reghao.tnb.content.app.exam.service.PaperViewService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author reghao
 * @date 2025-08-11 17:14:46
 */
@Tag(name = "阅卷接口")
@RestController
@RequestMapping("/api/content/exam/mark")
public class MarkController {
    private PaperService paperService;
    private final PaperViewService paperViewService;
    private final MarkService markService;

    public MarkController(PaperService paperService, PaperViewService paperViewService, MarkService markService) {
        this.paperService = paperService;
        this.paperViewService = paperViewService;
        this.markService = markService;
    }

    @Operation(summary = "获取阅卷列表", description = "N")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getExamMark(ExamQuery examQuery) {
        PageList<ExamMark> pageList = markService.getExamReviewList(examQuery);
        return WebResult.success(pageList);
    }

    @Operation(summary = "获取阅卷视图", description = "N")
    @GetMapping(value = "/view", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getMarkPaperView(@RequestParam("paperId") int paperId,
                                   @RequestParam("userId") long userId) {
        int viewType = PaperViewType.PaperMark.getCode();
        long loginUser = UserContext.getUserId();
        if (userId == 0) {
            userId = loginUser;
        }

        PaperResult paperResult = paperService.getPaperResult(paperId, userId);
        if (paperResult == null) {
            // 试卷结果不存在
            PaperDetail paperDetail = new PaperDetail(paperId, 5);
            return WebResult.success(paperDetail);
        }

        boolean marked = paperResult.getMarked();
        if (marked) {
            // 试卷已批改
            PaperDetail paperDetail = new PaperDetail(paperId, 7);
            return WebResult.success(paperDetail);
        }

        int resultId = paperResult.getId();
        PaperDetail paperDetail = paperViewService.getPaperDetail(paperId, viewType, resultId);
        return WebResult.success(paperDetail);
    }

    @Operation(summary = "提交阅卷结果", description = "N")
    @PostMapping(value = "/submit", produces = MediaType.APPLICATION_JSON_VALUE)
    public String submitExamMark(@RequestBody @Validated UserResult userResult) {
        markService.markExamResult(userResult);
        return WebResult.success();
    }
}
