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

import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.search.app.comment.model.dto.ArticlePublishSbt;
import cn.reghao.tnb.search.app.comment.model.dto.UpdateArticleContent;
import cn.reghao.tnb.search.app.comment.model.dto.UpdateArticleScope;
import cn.reghao.tnb.search.app.comment.model.po.ArticlePost;
import cn.reghao.tnb.search.app.comment.service.ArticlePostService;
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.*;

/**
 * @author reghao
 * @date 2023-02-02 11:36:06
 */
@Tag(name = "文章贴接口")
@RestController
@RequestMapping("/api/search1/post/article")
public class ArticlePostController {
    private final ArticlePostService articleService;

    public ArticlePostController(ArticlePostService articleService) {
        this.articleService = articleService;
    }

    @AuthUser
    @Operation(summary = "添加文章贴")
    @PostMapping("")
    public String submitArticlePost(@RequestBody @Validated ArticlePublishSbt articlePublishSbt) {
        articleService.submitArticle(articlePublishSbt);
        return WebResult.success();
    }

    @AuthUser
    @Operation(summary = "修改文章可见范围")
    @PostMapping("/update/scope")
    public String updateArticleScope(@RequestBody @Validated UpdateArticleScope updateArticleScope) {
        articleService.updateArticleScope(updateArticleScope);
        return WebResult.success();
    }

    @AuthUser
    @Operation(summary = "修改文章内容")
    @PostMapping("/update/content")
    public String updateArticleContent(@RequestBody @Validated UpdateArticleContent updateArticleContent) {
        articleService.updateArticleContent(updateArticleContent);
        return WebResult.success();
    }

    @AuthUser
    @Operation(summary = "删除文章贴")
    @DeleteMapping("/{articleId}")
    public String deleteArticlePost(@PathVariable("articleId") String articleId) {
        return WebResult.success();
    }

    @AuthUser
    @Operation(summary = "获取文章贴列表")
    @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getArticlePosts(@RequestParam("page") int page) {
        long userId = UserContext.getUserId();
        PageList<ArticlePost> pageList = articleService.getPageByUserId(userId, page);
        return WebResult.success(pageList);
    }

    @AuthUser
    @Operation(summary = "获取文章贴")
    @GetMapping(value = "/{articleId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getArticleInfo(@PathVariable("articleId") String articleId) {
        ArticlePost articlePost = articleService.getArticleInfo(articleId);
        return WebResult.success(articlePost);
    }
}
