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.AccountRole;
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.query.PaperQuery;
import cn.reghao.tnb.content.app.exam.model.vo.*;
import cn.reghao.tnb.content.app.exam.service.ExamService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2024-08-24 13:29:10
 */
@Tag(name = "测评接口")
@RestController
@RequestMapping("/api/content/exam/eval")
public class ExamController {
    private final ExamService examService;
    private final PaperService paperService;
    private final PaperViewService paperViewService;

    public ExamController(ExamService examService, PaperService paperService, PaperViewService paperViewService) {
        this.examService = examService;
        this.paperService = paperService;
        this.paperViewService = paperViewService;
    }

    @Operation(summary = "获取测评试卷列表", description = "N")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getExams(PaperQuery paperQuery) {
        int role = -1;
        /*if (UserContext.getUserRoles().contains(AccountRole.examAdmin.getValue())) {
            role = 1;
        } else if (UserContext.getUserRoles().contains(AccountRole.examUser.getValue())) {
            role = 2;
        }*/

        paperQuery.setScope(role);
        PageList<EvalPaper> pageList1 = paperService.getEvalPapers(paperQuery);
        return WebResult.success(pageList1);
    }

    @Operation(summary = "获取试卷开始测评", description = "N")
    @GetMapping(value = "/start/{paperId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getExamPaperView(@PathVariable("paperId") int paperId) {
        int viewType = PaperViewType.PaperExam.getCode();
        long userId = UserContext.getUserId();

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

    @Operation(summary = "获取试卷中的试题", description = "N")
    @GetMapping(value = "/paper/{paperId}/question", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getExamQuestions1(@PathVariable("paperId") Integer paperId) {
        List<QuestionInfo> list1 = paperService.getPaperQuestions(paperId);
        return WebResult.success(list1);
    }

    @Operation(summary = "暂存用户提交的试卷答案", description = "N")
    @PostMapping(value = "/cache", produces = MediaType.APPLICATION_JSON_VALUE)
    public String cacheExamPaper(@Validated @RequestBody UserResult userResult) {
        examService.cacheUserAnswers(userResult);
        return WebResult.success();
    }

    @Operation(summary = "用户提交试卷答案", description = "N")
    @PostMapping(value = "/submit", produces = MediaType.APPLICATION_JSON_VALUE)
    public String submitExamPaper(@Validated @RequestBody UserResult userResult) {
        int resultId = examService.submitExamPaper(userResult);
        return WebResult.success(resultId);
    }
}
