package shop.alien.store.controller;

import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.CommonComment;
import shop.alien.store.annotation.TrackEvent;
import shop.alien.store.service.CommonCommentService;

/**
 * 评论表 前端控制器
 *
 * @author lyx
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"评论管理"})
@ApiSort(13)
@CrossOrigin
@RestController
@RequestMapping("/commonComment")
@RequiredArgsConstructor
public class CommonCommentController {

    private final CommonCommentService commonCommentService;

    /**
     商户回复评论 实例：
     {
       "id": 10002,
       "sourceType": 1,
       "sourceId": 50008,
       "userId": 999000,
       "parentId": 10001,
       "content": "感谢您的认可，我们会继续努力为您提供更好的服务！",
       "imageUrls": "",
       "isAnonymous": 0,
       "commentType": 2,
       "merchantId": 10086,
       "isShow": 1,
       "auditStatus": 1,
       "likeCount": 8,
       "replyCount": 0,
       "createdTime": "2025-01-08 15:00:30",
       "updatedTime": "2025-01-08 15:00:30",
       "extInfo": "{\"floor\": 2, \"replyToUserId\": 888999}"
     }
     正常回复实例：
     {
     "sourceType": 1,
     "sourceId": 50008, //回复评价需要填评价id，回复评论不需要
     "userId": 888999, // 发布者id
     "parentId": 0, // 0代表第一级
     "content": "这家店的商品质量非常好，物流也很快，商家服务态度也很棒！",
     "commentType": 1,
     //   "merchantId": 10086, 为2商户评论需要填
     }
     */
    @TrackEvent(
            eventType = "POST_COMMENT",
            eventCategory = "INTERACTION",
            storeId = "",
            userId = "#commonComment.userId",
            targetId = "#commonComment.sourceId",
            targetType = "POST"
    )
    @ApiOperation(value = "新增评论", notes = "0:成功, 1:失败, 2:文本内容异常, 3:图片内容异常")
    @PostMapping("/addComment")
    public R addComment(@RequestBody CommonComment commonComment) {
        Integer addComment = commonCommentService.addComment(commonComment);
        if (addComment == 0) {
            return R.success("新增评论成功");
        }
        if (addComment == 2) {
            return R.fail("新增评论失败，评论内容包含敏感字符");
        }
        if (addComment == 4) {
            return R.fail("新增评论失败，评论内容超过300字限制");
        }
        return R.fail("新增评论失败");
    }

    /**删除评论
     * @param commentId 评论id
     * @return 0:成功, 1:失败
     */
    @ApiOperation(value = "删除评论", notes = "0:成功, 1:失败")
    @GetMapping("/deleteComment")
    public R deleteComment(@RequestParam Long commentId) {
        boolean b = commonCommentService.deleteComment(commentId);
        if (b) {
            return R.success("删除评论成功");
        }
        return R.fail("删除评论失败");
    }


    /**
     * 根据动态类型和动态id查询评论列表
     * @param sourceType
     * @param sourceId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "动态评论")
    @GetMapping("/getListBySourceType")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sourceType", value = "类型", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "sourceId", value = "id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = false, dataType = "Long")
    })
    public R getListBySourceType(@RequestParam Integer sourceType,
                                 @RequestParam Integer sourceId,
                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                 @RequestParam(required = false) Long userId) {
        return R.data(commonCommentService.getListBySourceType(sourceType, sourceId, pageNum, pageSize, userId));
    }

    /**
     * 获取评论数量
     * @param sourceId
     * @param sourceType
     * @param userId
     * @return
     */
    @ApiOperation(value = "获取评论数量")
    @GetMapping("/getCommitCount")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "sourceId", value = "id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "sourceType", value = "类型", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userType", value = "用户类型,user,store", required = true, dataType = "String")
    })
    public R getCommitCount(@RequestParam Integer sourceId,
                            @RequestParam Integer sourceType,
                            @RequestParam String userId,
                            @RequestParam(required = false) String userType){
        return R.data(commonCommentService.getCommitCount(sourceId, sourceType, userId, userType));
    }
}

