package org.lym.pom.controller;

import org.lym.pom.dto.posts.CommentDTO;
import org.lym.pom.repository.mongo.Comment;
import org.lym.pom.service.CommentService;
import org.shoulder.core.dto.request.BasePageQuery;
import org.shoulder.core.dto.request.PageQuery;
import org.shoulder.core.dto.response.BaseResult;
import org.shoulder.core.dto.response.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/comment")
public class CommentController extends BaseController {

    @Autowired
    private CommentService commentService;

    // 添加评论
    @PostMapping("add")
    public BaseResult<CommentDTO> addComment(@RequestBody @Validated CommentDTO comment) {
        Comment addedComment = commentService.addComment(toDomain(comment));
        return BaseResult.success(toDTO(addedComment));
    }

    private Comment toDomain(CommentDTO comment) {
        return converionService.convert(comment, Comment.class);
    }

    private CommentDTO toDTO(Comment comment) {
        return converionService.convert(comment, CommentDTO.class);
    }

    // 删除评论
    @PostMapping("delete/{id}")
    public BaseResult<Void> removeComment(@PathVariable("id") String id) {
        Comment commentToDelete = new Comment();
        commentToDelete.setId(id);
        commentService.removeComment(commentToDelete);
        return BaseResult.success();
    }

    // 分页查询某个评论下的子评论
    @GetMapping("/page")
    public BaseResult<PageResult<CommentDTO>> pageQuerySubComments(@RequestBody PageQuery<CommentDTO> pageQuery) {
        PageResult<Comment> domainPage = commentService.pageQueryDirectCommentsWithSubComments(BasePageQuery.create(pageQuery));
        PageResult<CommentDTO> result = PageResult.build(
                converionService.convert(domainPage.getList(), CommentDTO.class),
                domainPage.getPageNum(), domainPage.getPageSize(), domainPage.getTotal()
        );
        return BaseResult.success(result);
    }

    // 根据 id 查询单条评论
    @GetMapping("detail")
    public BaseResult<Comment> queryCommentDetail(@RequestParam("id") String commentId) {
        Comment comment = commentService.queryCommentDetail(commentId);
        return BaseResult.success(comment);
    }

    // 统计评论数量
    @GetMapping("count")
    public BaseResult<Long> countCommentNumber(@RequestParam("relatedId") String relatedId,
                                               @RequestParam("relatedType") String relatedType,
                                               @RequestParam(required = false) String rootCommentId) {
        long count = commentService.countCommentNumber(relatedId, relatedType, rootCommentId);
        return BaseResult.success(count);
    }
}
