package com.wzj.soopin.content.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.content.domain.bo.CommentBO;
import com.wzj.soopin.content.domain.po.Comment;
import com.wzj.soopin.content.domain.vo.ChildCommentVO;
import com.wzj.soopin.content.domain.vo.CommentVO;
import com.wzj.soopin.content.service.CommentService;
import com.wzj.soopin.content.service.MsgService;
import com.wzj.soopin.content.utils.RedisOperator;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "管理端-评论管理接口")
@RequestMapping("/cms/comment")
@RestController
public class CommentController extends BaseController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private RedisOperator redis;

    @Autowired
    private MsgService msgService;

    @Operation(summary = "获取评论列表")
    @PostMapping("/list")
    public R<Page<CommentVO>> list(
            @RequestBody CommentBO bo,
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size
    ) {
        Page<Comment> page = new Page<>(current, size);
        Page<CommentVO> commentPage = commentService.pageComment(page, bo);
        return R.ok(commentPage);
    }



    @Operation(summary = "获取子评论列表")
    @GetMapping("/childList")
    public R<Page<ChildCommentVO>> getChildComments(
            @RequestParam String fatherCommentId,
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long size
    ) {
        Page<ChildCommentVO> page = commentService.getChildCommentVOsPaged(fatherCommentId, current, size);
        return R.ok(page);
    }

    @Operation(summary = "下架评论")
    @PostMapping("/offline")
    public R<Void> offlineComment(@ApiParam(value = "评论ID") @RequestParam String commentId) {
        try {
            String username = LoginHelper.getUsername();
            commentService.offlineComment(commentId, username);
            return R.ok();
        } catch (Exception e) {
            log.error("下架评论失败", e);
            return R.fail("下架评论失败: " + e.getMessage());
        }
    }
}
