package shop.alien.lawyer.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.ReviewComment;
import shop.alien.entity.store.dto.ReviewCommentRequestDto;
import shop.alien.entity.store.dto.ReviewReplyDto;
import shop.alien.entity.store.vo.ReviewCommentVo;
import shop.alien.lawyer.service.ReviewCommentService;

import java.util.List;

/**
 * 评价评论 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"律师平台-评价评论"})
@ApiSort(16)
@CrossOrigin
@RestController
@RequestMapping("/lawyer/reviewComment")
@RequiredArgsConstructor
public class ReviewCommentController {

    private final ReviewCommentService reviewCommentService;

    @ApiOperation("创建评论（其他用户对评价的评论）")
    @ApiOperationSupport(order = 1)
    @PostMapping("/create")
    public R<ReviewComment> createComment(@RequestBody ReviewComment comment) {
        log.info("ReviewCommentController.createComment?comment={}", comment);
        if (comment.getUserId() == null) {
            return R.fail("用户未登录");
        }
        return reviewCommentService.createComment(comment);
    }

    @ApiOperation("根据评价ID查询评论列表")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reviewId", value = "评价ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "currentUserId", value = "当前用户ID（用于判断是否已点赞）", dataType = "int", paramType = "query")
    })
    @GetMapping("/list/reviewId")
    public R<List<ReviewCommentVo>> getCommentListByReviewId(
            @RequestParam Integer reviewId,
            @RequestParam(required = false) Integer currentUserId) {
        log.info("ReviewCommentController.getCommentListByReviewId?reviewId={}, currentUserId={}", reviewId, currentUserId);
        return reviewCommentService.getCommentListByReviewId(reviewId, currentUserId);
    }

    @ApiOperation("点赞评论")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "评论ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "int", paramType = "query", required = true)
    })
    @PostMapping("/like")
    public R<Boolean> likeComment(@RequestBody ReviewComment reviewComment) {
        Integer commentId = reviewComment.getId();
        Integer userId = reviewComment.getUserId();
        log.info("ReviewCommentController.likeComment?commentId={}, userId={}", commentId, userId);
        if (userId == null) {
            return R.fail("用户未登录");
        }
        if (commentId == null) {
            return R.fail("评论ID不能为空");
        }
        ReviewCommentRequestDto requestDto = new ReviewCommentRequestDto();
        requestDto.setCommentId(commentId);
        requestDto.setUserId(userId);
        return reviewCommentService.likeComment(requestDto);
    }

    @ApiOperation("取消点赞评论")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "评论ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "int", paramType = "query", required = true)
    })
    @PostMapping("/cancelLike")
    public R<Boolean> cancelLikeComment(@RequestBody ReviewComment reviewComment) {
        Integer commentId = reviewComment.getId();
        Integer userId = reviewComment.getUserId();
        log.info("ReviewCommentController.cancelLikeComment?commentId={}, userId={}", commentId, userId);
        if (userId == null) {
            return R.fail("用户未登录");
        }
        if (commentId == null) {
            return R.fail("评论ID不能为空");
        }
        ReviewCommentRequestDto requestDto = new ReviewCommentRequestDto();
        requestDto.setCommentId(commentId);
        requestDto.setUserId(userId);
        return reviewCommentService.cancelLikeComment(requestDto);
    }

    @ApiOperation("创建回复（用户对评论的回复）")
    @ApiOperationSupport(order = 6)
    @PostMapping("/reply/create")
    public R<ReviewComment> createReply(@RequestBody ReviewReplyDto replyDto) {
        log.info("ReviewCommentController.createReply?replyDto={}", replyDto);
        if (replyDto.getUserId() == null) {
            return R.fail("用户未登录");
        }
        return reviewCommentService.createReply(replyDto);
    }

    @ApiOperation("根据首评ID查询回复列表")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "headId", value = "首评ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "currentUserId", value = "当前用户ID（用于判断是否已点赞）", dataType = "int", paramType = "query")
    })
    @GetMapping("/reply/list/headId")
    public R<List<ReviewCommentVo>> getReplyListByHeadId(
            @RequestParam Integer headId,
            @RequestParam(required = false) Integer currentUserId) {
        log.info("ReviewCommentController.getReplyListByHeadId?headId={}, currentUserId={}", headId, currentUserId);
        return reviewCommentService.getReplyListByHeadId(headId, currentUserId);
    }

    @ApiOperation("删除回复")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "replyId", value = "回复ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "int", paramType = "query", required = true)
    })
    @PostMapping("/reply/delete")
    public R<Boolean> deleteReply(
            @RequestParam Integer replyId,
            @RequestParam Integer userId) {
        log.info("ReviewCommentController.deleteReply?replyId={}, userId={}", replyId, userId);
        if (userId == null) {
            return R.fail("用户未登录");
        }
        return reviewCommentService.deleteReply(replyId, userId);
    }

    @ApiOperation(value = "删除评论（根据ID）", notes = "根据评论ID删除评论，userId有值时只能删除自己发布的评论，userId为空时允许删除任何评论（管理员删除）")
    @ApiOperationSupport(order = 11)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论ID", dataType = "int", paramType = "body", required = true),
            @ApiImplicitParam(name = "userId", value = "用户ID（可选，有值时只能删除自己的评论，为空时允许删除任何评论）", dataType = "int", paramType = "body", required = false)
    })
    @PostMapping("/deleteReviewComment")
    public R<Boolean> deleteReviewComment(@RequestBody ReviewComment reviewComment) {
        log.info("ReviewCommentController.deleteReviewComment?reviewComment={}", reviewComment);
        return reviewCommentService.deleteReviewComment(reviewComment);
    }


}

