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.resp.CommentInfoDTO;
import com.luojia_channel.modules.post.entity.Comment;
import com.luojia_channel.modules.post.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


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

    private final CommentService commentService;

    @Autowired
    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

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

    // 更新评论
    @PutMapping("/{id}")
    public Result<Void> updateComment(@PathVariable Long id, @RequestBody Comment comment) {
        Long currentUserId = UserContext.getUserId();
        commentService.updateComment(comment, currentUserId);
        return Result.success();
    }

    // 删除评论
    @DeleteMapping("/{id}")
    public Result<Void> deleteComment(@PathVariable Long id) {
        Long currentUserId = UserContext.getUserId();
        commentService.deleteComment(id, currentUserId);
        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("/nested/post/{postId}")
    public Result<List<CommentInfoDTO>> getNestedCommentsByPostId(@PathVariable Long postId) {
        List<CommentInfoDTO> nestedComments = commentService.getNestedCommentsByPostId(postId);
        return Result.success(nestedComments);
    }

}
