package shop.alien.lawyer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.OrderReview;
import shop.alien.entity.store.dto.OrderReviewDto;
import shop.alien.entity.store.vo.LawyerReviewStatisticsVo;
import shop.alien.entity.store.vo.OrderReviewDetailVo;
import shop.alien.entity.store.vo.OrderReviewVo;
import shop.alien.entity.store.vo.PendingReviewVo;
import shop.alien.lawyer.service.OrderReviewService;

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

    private final OrderReviewService orderReviewService;

    @ApiOperation("创建订单评价（只有订单用户才能评价）")
    @ApiOperationSupport(order = 1)
    @PostMapping("/create")
    public R<OrderReview> createReview(@RequestBody OrderReviewDto reviewDto) {
        log.info("OrderReviewController.createReview?reviewDto={}, ", reviewDto);
        if (reviewDto.getUserId() == null) {
            return R.fail("用户未登录");
        }
        return orderReviewService.createReview(reviewDto);
    }

    @ApiOperation("获取评价详情（包含评论和回复）")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reviewId", value = "评价ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "currentUserId", value = "当前用户ID（用于判断是否已点赞）", dataType = "int", paramType = "query")
    })
    @GetMapping("/detail/reviewId")
    public R<OrderReviewDetailVo> getReviewDetail(
            @RequestParam Integer reviewId,
            @RequestParam(required = false) Integer currentUserId) {
        log.info("OrderReviewController.getReviewDetail?reviewId={}, currentUserId={}", reviewId, currentUserId);
        if (reviewId == null) {
            return R.fail("评价ID不能为空");
        }
        return orderReviewService.getReviewDetail(reviewId, currentUserId);
    }

    @ApiOperation("获取评价")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "currentUserId", value = "用户ID只能删除自己的评价）", dataType = "int", paramType = "query")
    })
    @GetMapping("/select/reviewIdByOrderId")
    public R<OrderReviewDetailVo> reviewIdByOrderId(
            @RequestParam Integer orderId,
            @RequestParam(required = false) Integer currentUserId) {
        log.info("OrderReviewController.getReviewDetail?reviewId={}, currentUserId={}", orderId, currentUserId);
        QueryWrapper<OrderReview> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        queryWrapper.eq("delete_flag", 0);
        OrderReview review = orderReviewService.getOne(queryWrapper);
        if (review == null) {
            return R.fail("评价ID不能为空");
        }
        return orderReviewService.getReviewDetail(review.getId(), currentUserId);
    }

    @ApiOperation("点赞评价")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reviewId", value = "评价ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "int", paramType = "query", required = true)
    })
    @PostMapping("/like")
    public R<Boolean> likeReview(@RequestBody OrderReview orderReview) {
        Integer reviewId = orderReview.getId();
        Integer userId = orderReview.getUserId();
        log.info("OrderReviewController.likeReview?reviewId={}, userId={}", reviewId, userId);
        if (userId == null) {
            return R.fail("用户未登录");
        }
        if (reviewId == null) {
            return R.fail("评价ID不能为空");
        }
        return orderReviewService.likeReview(reviewId, userId);
    }

    @ApiOperation("取消点赞评价")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reviewId", value = "评价ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "int", paramType = "query", required = true)
    })
    @PostMapping("/cancelLike")
    public R<Boolean> cancelLikeReview(@RequestBody OrderReview orderReview) {
        Integer reviewId = orderReview.getId();
        Integer userId = orderReview.getUserId();
        log.info("OrderReviewController.cancelLikeReview?reviewId={}, userId={}", reviewId, userId);
        if (userId == null) {
            return R.fail("用户未登录");
        }
        return orderReviewService.cancelLikeReview(reviewId, userId);
    }

    @ApiOperation("分页查询评价列表")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "评价用户ID", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "currentUserId", value = "当前用户ID（用于判断是否已点赞）", dataType = "int", paramType = "query")
    })
    @GetMapping("/list")
    public R<IPage<OrderReviewVo>> getReviewList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Integer orderId,
            @RequestParam(required = false) Integer lawyerUserId,
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) Integer currentUserId) {
        log.info("OrderReviewController.getReviewList?page={}, size={}, orderId={}, lawyerUserId={}, userId={}, currentUserId={}",
                page, size, orderId, lawyerUserId, userId, currentUserId);
        return orderReviewService.getReviewList(page, size, orderId, lawyerUserId, userId, currentUserId);
    }

    @ApiOperation("用户删除评价（只能删除自己的评价，删除评价时，会级联删除该评价下的所有评论和回复）")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reviewId", value = "评价ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户ID（必填，只能删除自己的评价）", dataType = "int", paramType = "query", required = true)
    })
    @PostMapping("/delete/reviewId")
    public R<Boolean> deleteReview(@RequestParam Integer reviewId,
                                   @RequestParam Integer userId) {
        log.info("OrderReviewController.deleteReview?reviewId={}, userId={}", reviewId, userId);
        if (userId == null) {
            return R.fail("用户ID不能为空");
        }
        return orderReviewService.deleteReview(reviewId, userId);
    }

    @ApiOperation("管理员删除评价（可以删除任何评价，删除评价时，会级联删除该评价下的所有评论和回复）")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reviewId", value = "评价ID", dataType = "int", paramType = "query", required = true)
    })
    @PostMapping("/admin/delete/reviewId")
    public R<Boolean> deleteReviewByAdmin(@RequestParam Integer reviewId) {
        log.info("OrderReviewController.deleteReviewByAdmin?reviewId={}", reviewId);
        return orderReviewService.deleteReviewByAdmin(reviewId);
    }

    @ApiOperation("根据订单ID查询评价")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "currentUserId", value = "当前用户ID（用于判断是否已点赞）", dataType = "int", paramType = "query")
    })
    @GetMapping("/order/orderId")
    public R<OrderReviewVo> getReviewByOrderId(
            @RequestParam Integer orderId,
            @RequestParam(required = false) Integer currentUserId) {
        log.info("OrderReviewController.getReviewByOrderId?orderId={}, currentUserId={}", orderId, currentUserId);
        return orderReviewService.getReviewByOrderId(orderId, currentUserId);
    }


    @ApiOperation("分页查询待评价的订单列表（返回未评论的订单信息以及律师信息）")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/pending/list")
    public R<IPage<PendingReviewVo>> getPendingReviewList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam Integer userId) {
        log.info("OrderReviewController.getPendingReviewList?page={}, size={}, userId={}", page, size, userId);
        if (userId == null) {
            return R.fail("用户未登录");
        }
        return orderReviewService.getPendingReviewList(page, size, userId);
    }

    @ApiOperation("分页查询我的评价列表（查询当前用户的所有评价）")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "currentUserId", value = "当前用户ID（用于判断是否已点赞）", dataType = "int", paramType = "query")
    })
    @GetMapping("/my/list")
    public R<IPage<OrderReviewVo>> getMyReviewList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam Integer userId,
            @RequestParam(required = false) Integer currentUserId) {
        log.info("OrderReviewController.getMyReviewList?page={}, size={}, userId={}, currentUserId={}", page, size, userId, currentUserId);
        if (userId == null) {
            return R.fail("用户未登录");
        }
        return orderReviewService.getMyReviewList(page, size, userId, currentUserId);
    }

    @ApiOperation("获取律师评价统计数据（全部数量、好评数量、中评数量、差评数量、有图数量）")
    @ApiOperationSupport(order = 10)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/statistics")
    public R<LawyerReviewStatisticsVo> getLawyerReviewStatistics(@RequestParam Integer lawyerUserId) {
        log.info("OrderReviewController.getLawyerReviewStatistics?lawyerUserId={}", lawyerUserId);
        if (lawyerUserId == null) {
            return R.fail("律师ID不能为空");
        }
        return orderReviewService.getLawyerReviewStatistics(lawyerUserId);
    }

    @ApiOperation("根据律师ID分页查询评价列表（查询指定律师的所有评价）")
    @ApiOperationSupport(order = 11)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "currentUserId", value = "当前用户ID（用于判断是否已点赞）", dataType = "int", paramType = "query")
    })
    @GetMapping("/list/byLawyer")
    public R<IPage<OrderReviewVo>> getReviewListByLawyer(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam Integer lawyerUserId,
            @RequestParam(required = false) Integer currentUserId) {
        log.info("OrderReviewController.getReviewListByLawyer?page={}, size={}, lawyerUserId={}, currentUserId={}",
                page, size, lawyerUserId, currentUserId);
        if (lawyerUserId == null) {
            return R.fail("律师ID不能为空");
        }
        return orderReviewService.getReviewList(page, size, null, lawyerUserId, null, currentUserId);
    }

    @ApiOperation("根据律师ID和类型分页查询评价列表（不包含评论）")
    @ApiOperationSupport(order = 12)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "查询分类（1：好评，2：中评，3：差评，4：有图，为空时返回全部）", dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "currentUserId", value = "当前用户ID（用于判断是否已点赞）", dataType = "int", paramType = "query")
    })
    @GetMapping("/list/byLawyerAndType")
    public R<IPage<OrderReviewVo>> getReviewListByLawyerAndType(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam Integer lawyerUserId,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Integer currentUserId) {
        log.info("OrderReviewController.getReviewListByLawyerAndType?page={}, size={}, lawyerUserId={}, type={}, currentUserId={}",
                page, size, lawyerUserId, type, currentUserId);
        if (lawyerUserId == null) {
            return R.fail("律师ID不能为空");
        }
        return orderReviewService.getReviewListByLawyerAndType(page, size, lawyerUserId, type, currentUserId);
    }

    @ApiOperation("根据订单ID查询订单关联的评价")
    @ApiOperationSupport(order = 13)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/getByOrderId")
    public R<OrderReviewVo> queryReviewByOrderId(
            @RequestParam Integer orderId) {
        log.info("OrderReviewController.queryReviewByOrderId?orderId={}", orderId);
        if (orderId == null) {
            return R.fail("订单ID不能为空");
        }
        return orderReviewService.getOrderEvaluation(orderId);
    }

}

