package com.xindazhou.business.controller.app.community.comment;

import com.xindazhou.business.controller.app.community.comment.vo.CommentCreateReqVO;
import com.xindazhou.business.controller.app.community.comment.vo.CommentPageReqVO;
import com.xindazhou.business.controller.app.community.comment.vo.CommentRespVO;
import com.xindazhou.business.service.community.comment.CommentService;
import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.xindazhou.framework.common.pojo.CommonResult.success;

/**
 * 移动端 - 评论 Controller
 *
 * @author xindazhou
 */
@Tag(name = "移动端 - 评论管理")
@RestController("appCommunityCommentController")
@RequestMapping("/app-api/community/comment")
@Validated
@Slf4j
public class CommentController {

    @Resource
    private CommentService commentService;

    @PostMapping("/create")
    @Operation(summary = "创建评论")
    public CommonResult<Long> createComment(@Valid @RequestBody CommentCreateReqVO createReqVO) {
        Long commentId = commentService.createComment(createReqVO);
        return success(commentId);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评论")
    @Parameter(name = "id", description = "评论ID", required = true, example = "1")
    public CommonResult<Boolean> deleteComment(@RequestParam("id") Long id) {
        commentService.deleteComment(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得评论")
    @Parameter(name = "id", description = "评论ID", required = true, example = "1")
    public CommonResult<CommentRespVO> getComment(@RequestParam("id") Long id) {
        CommentRespVO comment = commentService.getComment(id);
        return success(comment);
    }

    @GetMapping("/page")
    @Operation(summary = "获得评论分页列表")
    public CommonResult<PageResult<CommentRespVO>> getCommentPage(@Valid CommentPageReqVO pageReqVO) {
        PageResult<CommentRespVO> pageResult = commentService.getCommentPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/tree")
    @Operation(summary = "获得帖子的评论树")
    @Parameter(name = "postId", description = "帖子ID", required = true, example = "1")
    public CommonResult<List<CommentRespVO>> getCommentTree(@RequestParam("postId") Long postId) {
        List<CommentRespVO> commentTree = commentService.getCommentTree(postId);
        return success(commentTree);
    }

}

