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

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.po.PaperResult;
import cn.reghao.tnb.content.app.exam.model.vo.PaperDetail;
import cn.reghao.tnb.content.app.exam.service.PaperService;
import cn.reghao.tnb.content.app.exam.service.PaperViewService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @author reghao
 * @date 2024-08-29 09:54:34
 */
@Tag(name = "考试结果接口")
@RestController
@RequestMapping("/api/content/exam/result")
public class ExamResultController {
    private final PaperService paperService;
    private final PaperViewService paperViewService;

    public ExamResultController(PaperService paperService, PaperViewService paperViewService) {
        this.paperService = paperService;
        this.paperViewService = paperViewService;
    }

    @Operation(summary = "获取试卷结果", description = "N")
    @GetMapping(value = "/view/{paperId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getExamMark(@PathVariable("paperId") Integer paperId) {
        int viewType = PaperViewType.PaperResult.getCode();
        long loginUser = UserContext.getUserId();

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

        if (!paperResult.getMarked()) {
            // 试卷尚未批改
            PaperDetail paperDetail = new PaperDetail(paperId, 6);
            return WebResult.success(paperDetail);
        }

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

    @Operation(summary = "获取用户的考试成绩", description = "N")
    @GetMapping(value = "/score", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getExamScore() {
        return WebResult.success();
    }
}
