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.db.KeyValue;
import cn.reghao.tnb.content.app.exam.model.dto.QuestionAddDto;
import cn.reghao.tnb.content.app.exam.model.query.QuestionQuery;
import cn.reghao.tnb.content.app.exam.model.vo.*;
import cn.reghao.tnb.content.app.exam.service.QuestionService;
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.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * @author reghao
 * @date 2024-08-24 23:57:42
 */
@Tag(name = "试题接口")
@RestController
@RequestMapping("/api/content/exam/question")
public class QuestionController {
    private final QuestionService questionService;

    public QuestionController(QuestionService questionService) {
        this.questionService = questionService;
    }

    @Operation(summary = "添加试题", description = "N")
    @PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addQuestion(@RequestBody @Valid QuestionAddDto questionAddDto) {
        Result result = questionService.addQuestion(questionAddDto);
        return WebResult.result(result);
    }

    @Operation(summary = "删除试题", description = "N")
    @DeleteMapping(value = "/{questionId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteQuestion(@PathVariable("questionId") Long questionId) {
        Result result = questionService.deleteQuestion(questionId);
        return WebResult.result(result);
    }

    @Operation(summary = "获取试题列表", description = "N")
    @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getQuestions(QuestionQuery questionQuery) {
        PageList<QuestionView> pageList = questionService.getQuestions(questionQuery);
        return WebResult.success(pageList);
    }

    @Operation(summary = "获取试题详情", description = "N")
    @GetMapping(value = "/{questionId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getQuestion(@PathVariable("questionId") Long questionId) {
        QuestionDetail questionDetail = questionService.getQuestionDetail(questionId);
        return WebResult.success(questionDetail);
    }

    @Operation(summary = "获取试题类型 KV 列表", description = "N")
    @GetMapping(value = "/type", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getQuestionType() {
        List<KeyValue> list = questionService.getKeyValues();
        return WebResult.success(list);
    }
}
