package org.dromara.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.content.domain.base.BaseInfoProperties;
import com.wzj.soopin.content.domain.bo.CommentBO;
import com.wzj.soopin.content.domain.po.Comment;
import com.wzj.soopin.content.domain.vo.CommentVO;
import com.wzj.soopin.content.enums.MessageEnum;
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.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.mq.domain.MQMessage;
import org.dromara.common.mq.enums.MQMessageType;
import org.dromara.common.mq.enums.MessageActionEnum;
import org.dromara.common.mq.utils.MqUtil;
import org.dromara.common.redis.redis.RedisCache;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Api(tags = "管理端-评论管理接口")
@RequestMapping("/app/comment")
@RestController
public class AppCommentController {
    @Autowired
    private  CommentService commentService;

    @ApiOperation("查询视频评论列表")
    @PostMapping("/page")
    public R<Page<CommentVO>> queryVlogComments(
        @RequestBody CommentBO bo,
        @RequestBody Page<Comment> page) {
        try {
            Page<CommentVO> commentPage = commentService.pageComment(page, bo);
            return R.ok(commentPage);
        } catch (Exception e) {
            log.error("查询视频评论列表失败", e);
            return R.fail("查询视频评论列表失败: " + e.getMessage());
        }
    }

    @ApiOperation("发布评论")
    @PostMapping("/publish")
    public R<Void> publishComment(@RequestBody CommentBO bo) {
        try {
            commentService.publishComment(bo);
            return R.ok();
        } catch (Exception e) {
            log.error("发布评论失败", e);
            return R.fail("发布评论失败: " + e.getMessage());
        }
    }

    @ApiOperation("删除评论")
    @PostMapping("/delete")
    public R<Void> deleteComment(
        @ApiParam(value = "评论ID") @RequestParam String commentId) {
        try {
            String username = LoginHelper.getUsername();
            commentService.deleteComment(commentId, username);
            return R.ok();
        } catch (Exception e) {
            log.error("删除评论失败", e);
            return R.fail("删除评论失败: " + e.getMessage());
        }
    }

    @ApiOperation("获取评论详情")
    @GetMapping("/detail")
    public R<Map<String, Object>> getCommentDetail(@ApiParam(value = "评论ID") @RequestParam String commentId) {
        try {
            Comment comment = commentService.getCommentDetail(commentId);
            if (comment == null) {
                return R.fail("评论不存在");
            }

            // 获取子评论
            List<Comment> childComments = commentService.getChildComments(commentId);

            Map<String, Object> result = new HashMap<>();
            result.put("comment", comment);
            result.put("childComments", childComments);

            return R.ok(result);
        } catch (Exception e) {
            log.error("获取评论详情失败", e);
            return R.fail("获取评论详情失败: " + e.getMessage());
        }
    }
}
