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.domain.page.ScrollPageResponse;
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 io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/comment")
@Tag(name = "评论模块", description = "评论相关接口")
public class CommentController {

    private final CommentService commentService;

    // 创建评论
    @PostMapping
    @Operation(
            summary = "创建评论",
            tags = {"评论模块"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "创建成功"),
            @ApiResponse(responseCode = "500", description = "创建失败，请稍后重试")
    })
    public Result<Long> saveComment(@RequestBody CommentSaveDTO commentSaveDTO) {
        return Result.success(commentService.saveComment(commentSaveDTO));
    }

    // 更新评论
    @PutMapping
    @Operation(
            summary = "更新评论",
            tags = {"评论模块"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "500", description = "更新失败，请稍后重试")
    })
    public Result<Void> updateComment(@RequestBody CommentSaveDTO commentSaveDTO) {
        commentService.updateComment(commentSaveDTO);
        return Result.success();
    }

    // 删除评论
    @DeleteMapping
    @Operation(
            summary = "删除评论",
            tags = {"评论模块"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "500", description = "删除失败，评论不存在或被删除")
    })
    public Result<Void> deleteComment(@RequestParam("id") Long id) {
        commentService.deleteComment(id);
        return Result.success();
    }

    // 根据帖子ID分页获取根评论
    @GetMapping("/list")
    @Operation(
            summary = "根据帖子ID分页获取根评论",
            tags = {"评论模块"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "获取失败，帖子ID不合法")
    })
    public Result<ScrollPageResponse<CommentInfoDTO>> getCommentsByPostId(CommentPageQueryDTO commentPageQueryDTO) {
        ScrollPageResponse<CommentInfoDTO> commentList = commentService.getCommentsByPostId(commentPageQueryDTO);
        return Result.success(commentList);
    }


    // 根据评论ID获取回复
    @GetMapping("/list/reply")
    @Operation(
            summary = "根据评论ID获取回复",
            tags = {"评论模块"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "获取失败，评论ID不合法")
    })
    public Result<ScrollPageResponse<CommentInfoDTO>> getReplyById(CommentPageQueryDTO commentPageQueryDTO) {
        ScrollPageResponse<CommentInfoDTO> commentInfoDTOList = commentService.getReplyById(commentPageQueryDTO);
        return Result.success(commentInfoDTOList);
    }

    // 点赞评论
    @PutMapping("/like/{id}")
    @Operation(
            summary = "点赞评论",
            tags = {"评论模块"}
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "点赞成功"),
            @ApiResponse(responseCode = "500", description = "点赞失败，评论不存在或被删除")
    })
    public Result<Void> likeComment(@PathVariable("id") Long id) {
        commentService.likeComment(id);
        return Result.success();
    }

}
