package com.luojia_channel.modules.post.controller;

import com.luojia_channel.common.domain.Result;
import com.luojia_channel.common.domain.page.PageResponse;
import com.luojia_channel.common.utils.UserContext;
import com.luojia_channel.modules.post.dto.req.CommentPageQueryDTO;
import com.luojia_channel.modules.post.dto.req.CommentSaveDTO;
import com.luojia_channel.modules.post.dto.resp.CommentInfoDTO;
import com.luojia_channel.modules.post.entity.Comment;
import com.luojia_channel.modules.post.service.CommentService;
import com.luojia_channel.modules.post.utils.ValidatePostUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/comments")
public class CommentController {

    private final CommentService commentService;

    // 创建评论
    @PostMapping
    public Result<Void> saveComment(@RequestBody CommentSaveDTO commentSaveDTO) {
        commentService.saveComment(commentSaveDTO);
        return Result.success();
    }

    // 更新评论
    @PutMapping
    public Result<Void> updateComment(@RequestBody CommentSaveDTO commentSaveDTO) {
        commentService.updateComment(commentSaveDTO);
        return Result.success();
    }

    // 删除评论
    @DeleteMapping
    public Result<Void> deleteComment(@RequestParam("id") Long id) {
        commentService.deleteComment(id);
        return Result.success();
    }

    // 根据帖子ID分页获取根评论
    @GetMapping("/list")
    public Result<PageResponse<CommentInfoDTO>> getCommentsByPostId(@RequestBody CommentPageQueryDTO commentPageQueryDTO) {
        PageResponse<CommentInfoDTO> commentList = commentService.getCommentsByPostId(commentPageQueryDTO);
        return Result.success(commentList);
    }


    // 根据评论ID获取回复
    @GetMapping("/list/reply")
    public Result<PageResponse<CommentInfoDTO>> getReplyById(@RequestBody CommentPageQueryDTO commentPageQueryDTO) {
        PageResponse<CommentInfoDTO> commentInfoDTOList = commentService.getReplyById(commentPageQueryDTO);
        return Result.success(commentInfoDTOList);
    }

}
