package cn.reghao.tnb.search.app.blog.controller;

import cn.reghao.tnb.search.app.blog.model.vo.AdminArticle;
import cn.reghao.tnb.search.app.blog.model.vo.QuestionView;
import cn.reghao.tnb.search.app.blog.service.ArticleQuery;
import cn.reghao.tnb.search.app.blog.service.QuestionService;
import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.web.result.WebResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2023-04-17 10:15:39
 */
@Tag(name = "面试题接口")
@RestController
@RequestMapping("/api/blog/v2/question")
public class AdminQuestionController {
    private final ArticleQuery articleQuery;
    private final QuestionService questionService;
    private int pageSize = 100;

    public AdminQuestionController(ArticleQuery articleQuery, QuestionService questionService) {
        this.articleQuery = articleQuery;
        this.questionService = questionService;
    }

    @Operation(summary = "面试题列表页面", description = "N")
    @GetMapping("/list")
    public String list(@RequestParam("pn") int pageNumber,
                       @RequestParam(value = "categoryId", required = false) Integer categoryId) {
        if (categoryId == null) {
            categoryId = 0;
        }

        int published = 0;
        String title = "";
        int postType = 2;
        Page<AdminArticle> page0 = articleQuery.findAdminArticleByPage(pageSize, pageNumber, postType, categoryId, published, title);
        PageList<QuestionView> pageList = getPageList(page0);
        return WebResult.success(pageList);
    }

    private PageList<QuestionView> getPageList(Page<AdminArticle> page) {
        int pageNumber = page.getNumber() + 1;
        int pageSize = page.getSize();
        long total = page.getTotalElements();
        List<QuestionView> list = page.getContent().stream().map(QuestionView::new).toList();
        return PageList.pageList(pageNumber, pageSize, (int) total, list);
    }

    @Operation(summary = "面试题加权重接口", description = "N")
    @PostMapping(value = "/weight/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addWeight(String questionId) {
        questionService.addWeight(questionId);
        return WebResult.success();
    }

    @Operation(summary = "面试题减权重接口", description = "N")
    @PostMapping(value = "/weight/minus", produces = MediaType.APPLICATION_JSON_VALUE)
    public String minusWeight(String questionId) {
        questionService.minusWeight(questionId);
        return WebResult.success();
    }
}
