package cn.meowrain.aioj.backend.blogservice.controller;

import cn.meowrain.aioj.backend.framework.core.web.Result;
import cn.meowrain.aioj.backend.framework.core.web.Results;
import cn.meowrain.aioj.backend.blogservice.dto.req.CommentCreateRequestDTO;
import cn.meowrain.aioj.backend.blogservice.dto.req.CommentQueryRequestDTO;
import cn.meowrain.aioj.backend.blogservice.dto.resp.CommentResponseDTO;
import cn.meowrain.aioj.backend.blogservice.service.CommentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评论管理控制器
 *
 * @author AIOJ
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/comment")
@Tag(name = "评论管理", description = "评论的发表、查询、删除等接口")
public class CommentController {

    private final CommentService commentService;

    @PostMapping("/create")
    @Operation(summary = "发表评论", description = "发表文章评论，支持Markdown格式")
    public Result<Long> createComment(
            @Parameter(description = "评论创建信息", required = true)
            @Valid @RequestBody CommentCreateRequestDTO request) {
        Long commentId = commentService.createComment(request);
        return Results.success(commentId);
    }

    @PostMapping("/reply")
    @Operation(summary = "回复评论", description = "回复指定评论")
    public Result<Long> replyComment(
            @Parameter(description = "评论回复信息", required = true)
            @Valid @RequestBody CommentCreateRequestDTO request) {
        Long commentId = commentService.replyComment(request);
        return Results.success(commentId);
    }

    @DeleteMapping("/delete/{commentId}")
    @Operation(summary = "删除评论", description = "删除指定评论")
    public Result<Void> deleteComment(
            @Parameter(description = "评论ID", required = true)
            @PathVariable("commentId") Long commentId) {
        commentService.deleteComment(commentId);
        return Results.success();
    }

    @GetMapping("/{commentId}")
    @Operation(summary = "查询评论详情", description = "根据评论ID查询评论详细信息")
    public Result<CommentResponseDTO> getCommentById(
            @Parameter(description = "评论ID", required = true)
            @PathVariable("commentId") Long commentId) {
        CommentResponseDTO comment = commentService.getCommentById(commentId);
        return Results.success(comment);
    }

    @GetMapping("/article/{articleId}")
    @Operation(summary = "查询文章评论", description = "根据文章ID查询该文章的所有评论（树形结构）")
    public Result<List<CommentResponseDTO>> getCommentsByArticleId(
            @Parameter(description = "文章ID", required = true)
            @PathVariable("articleId") Long articleId) {
        List<CommentResponseDTO> comments = commentService.getCommentsByArticleId(articleId);
        return Results.success(comments);
    }

    @PostMapping("/list")
    @Operation(summary = "分页查询评论列表", description = "支持多条件查询")
    public Result<IPage<CommentResponseDTO>> getCommentList(
            @Parameter(description = "查询条件")
            @RequestBody CommentQueryRequestDTO request) {
        IPage<CommentResponseDTO> page = commentService.getCommentList(request);
        return Results.success(page);
    }

    @GetMapping("/children/{parentId}")
    @Operation(summary = "查询子评论", description = "查询指定评论的所有子评论")
    public Result<List<CommentResponseDTO>> getChildComments(
            @Parameter(description = "父评论ID", required = true)
            @PathVariable("parentId") Long parentId) {
        List<CommentResponseDTO> comments = commentService.getChildComments(parentId);
        return Results.success(comments);
    }
}
