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

import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.tnb.common.db.KeyValue;
import cn.reghao.tnb.content.app.exam.model.dto.PaperAddDto;
import cn.reghao.tnb.content.app.exam.model.query.GetPaperQuery;
import cn.reghao.tnb.content.app.exam.model.query.PaperQuery;
import cn.reghao.tnb.content.app.exam.model.vo.PaperDetail;
import cn.reghao.tnb.content.app.exam.model.vo.PaperView;
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 jakarta.validation.Valid;
import java.util.List;

/**
 * @author reghao
 * @date 2024-08-27 17:20:34
 */
@Tag(name = "试卷接口")
@RestController
@RequestMapping("/api/content/exam/paper")
public class PaperController {
    private final PaperService paperService;
    private final PaperViewService paperViewService;

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

    @AuthUser
    @Operation(summary = "创建试卷", description = "N")
    @PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addPaper(@RequestBody @Valid PaperAddDto paperAddDto) {
        Result result = paperService.createExamPaper(paperAddDto);
        return WebResult.result(result);
    }

    @AuthUser
    @Operation(summary = "删除试卷", description = "N")
    @DeleteMapping(value = "/{paperId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deletePaper(@PathVariable("paperId") Integer paperId) {
        Result result = paperService.deleteExamPaper(paperId);
        return WebResult.result(result);
    }

    @AuthUser
    @Operation(summary = "获取试卷列表", description = "N")
    @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getPapers(PaperQuery paperQuery) {
        PageList<PaperView> pageList = paperService.getExamPapers(paperQuery);
        return WebResult.success(pageList);
    }

    @AuthUser
    @Operation(summary = "获取试卷详情", description = "N")
    @GetMapping(value = "/detail", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getPaper(@Validated GetPaperQuery getPaperQuery) {
        int paperId = getPaperQuery.getPaperId();
        int viewType = getPaperQuery.getViewType();
        Long userId = getPaperQuery.getUserId();

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

    @AuthUser
    @Operation(summary = "获取试卷 KV 列表", description = "N")
    @GetMapping(value = "/kv", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getPaperKeyValue() {
        List<KeyValue> list = paperService.getKeyValues();
        return WebResult.success(list);
    }
}
