package shop.alien.store.controller;

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.StoreStaffReview;
import shop.alien.entity.store.dto.StoreStaffReviewDto;
import shop.alien.entity.store.vo.StoreStaffReviewDetailVo;
import shop.alien.entity.store.vo.StoreStaffReviewListWithStaffVo;
import shop.alien.entity.store.vo.StoreStaffReviewStatisticsVo;
import shop.alien.entity.store.vo.StoreStaffReviewVo;
import shop.alien.store.service.StoreStaffReviewService;

/**
 * 员工评价 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"员工评价"})
@ApiSort(20)
@CrossOrigin
@RestController
@RequestMapping("/store/staffReview")
@RequiredArgsConstructor
public class StoreStaffReviewController {

    private final StoreStaffReviewService storeStaffReviewService;

    @ApiOperation("创建员工评价")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "staffUserId", value = "员工用户ID", dataTypeClass = Integer.class, paramType = "body", required = true),
            @ApiImplicitParam(name = "overallRating", value = "总体评分（1-5星，支持0.5星）", dataTypeClass = java.math.BigDecimal.class, paramType = "body"),
            @ApiImplicitParam(name = "serviceAttitudeRating", value = "服务态度评分（1-5星，支持0.5星）", dataTypeClass = java.math.BigDecimal.class, paramType = "body"),
            @ApiImplicitParam(name = "responseTimeRating", value = "响应时间评分（1-5星，支持0.5星）", dataTypeClass = java.math.BigDecimal.class, paramType = "body"),
            @ApiImplicitParam(name = "professionalAbilityRating", value = "专业能力评分（1-5星，支持0.5星）", dataTypeClass = java.math.BigDecimal.class, paramType = "body"),
            @ApiImplicitParam(name = "reviewContent", value = "评价内容", dataTypeClass = String.class, paramType = "body"),
            @ApiImplicitParam(name = "reviewImages", value = "评价图片列表", dataTypeClass = java.util.List.class, paramType = "body"),
            @ApiImplicitParam(name = "isAnonymous", value = "是否匿名评价，0:否，1:是", dataTypeClass = Integer.class, paramType = "body"),
            @ApiImplicitParam(name = "userId", value = "用户ID（必填）", dataTypeClass = Integer.class, paramType = "body", required = true)
    })
    @PostMapping("/create")
    public R<StoreStaffReview> createReview(@RequestBody StoreStaffReviewDto reviewDto) {
        log.info("创建员工评价, reviewDto={}", reviewDto);
        if (reviewDto == null || reviewDto.getUserId() == null) {
            return R.fail("用户未登录");
        }
        return storeStaffReviewService.createReview(reviewDto);
    }

    @ApiOperation("获取评价详情（包含评论和回复）")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reviewId", value = "评价ID", dataTypeClass = Integer.class, paramType = "query", required = true),
            @ApiImplicitParam(name = "currentUserId", value = "当前用户ID（用于判断是否已点赞）", dataTypeClass = Integer.class, paramType = "query", required = false),
            @ApiImplicitParam(name = "longitude", value = "用户经度（用于计算距离，可选）", dataTypeClass = Double.class, paramType = "query", required = false),
            @ApiImplicitParam(name = "latitude", value = "用户纬度（用于计算距离，可选）", dataTypeClass = Double.class, paramType = "query", required = false)
    })
    @GetMapping("/detail/reviewId")
    public R<StoreStaffReviewDetailVo> getReviewDetail(
            @RequestParam Integer reviewId,
            @RequestParam(required = false) Integer currentUserId,
            @RequestParam(required = false) Double longitude,
            @RequestParam(required = false) Double latitude) {
        log.info("获取评价详情, reviewId={}, currentUserId={}, longitude={}, latitude={}", reviewId, currentUserId, longitude, latitude);
        if (reviewId == null) {
            return R.fail("评价ID不能为空");
        }
        return storeStaffReviewService.getReviewDetail(reviewId, currentUserId, longitude, latitude);
    }

    @ApiOperation("点赞评价")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reviewId", value = "评价ID", dataTypeClass = Integer.class, paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataTypeClass = Integer.class, paramType = "query", required = true)
    })
    @PostMapping("/like")
    public R<Boolean> likeReview(@RequestParam Integer reviewId,
                                  @RequestParam Integer userId) {
        log.info("点赞评价, reviewId={}, userId={}", reviewId, userId);
        if (reviewId == null) {
            return R.fail("评价ID不能为空");
        }
        if (userId == null) {
            return R.fail("用户未登录");
        }
        return storeStaffReviewService.likeReview(reviewId, userId);
    }

    @ApiOperation("取消点赞评价")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reviewId", value = "评价ID", dataTypeClass = Integer.class, paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataTypeClass = Integer.class, paramType = "query", required = true)
    })
    @PostMapping("/cancelLike")
    public R<Boolean> cancelLikeReview(@RequestParam Integer reviewId,
                                       @RequestParam Integer userId) {
        log.info("取消点赞评价, reviewId={}, userId={}", reviewId, userId);
        if (reviewId == null) {
            return R.fail("评价ID不能为空");
        }
        if (userId == null) {
            return R.fail("用户未登录");
        }
        return storeStaffReviewService.cancelLikeReview(reviewId, userId);
    }

    @ApiOperation("分页查询评价列表")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataTypeClass = Integer.class, paramType = "query", required = false),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataTypeClass = Integer.class, paramType = "query", required = false),
            @ApiImplicitParam(name = "staffUserId", value = "员工用户ID", dataTypeClass = Integer.class, paramType = "query", required = false),
            @ApiImplicitParam(name = "userId", value = "评价用户ID", dataTypeClass = Integer.class, paramType = "query", required = false),
            @ApiImplicitParam(name = "currentUserId", value = "当前用户ID（用于判断是否已点赞）", dataTypeClass = Integer.class, paramType = "query", required = false)
    })
    @GetMapping("/list")
    public R<IPage<StoreStaffReviewVo>> getReviewList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Integer staffUserId,
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) Integer currentUserId) {
        log.info("分页查询评价列表, page={}, size={}, staffUserId={}, userId={}, currentUserId={}",
                page, size, staffUserId, userId, currentUserId);
        return storeStaffReviewService.getReviewList(page, size, staffUserId, userId, currentUserId);
    }

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

    @ApiOperation(value = "管理员删除评价", notes = "可以删除任何评价，删除评价时，会级联删除该评价下的所有评论和回复")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reviewId", value = "评价ID", dataTypeClass = Integer.class, paramType = "query", required = true)
    })
    @PostMapping("/admin/delete/reviewId")
    public R<Boolean> deleteReviewByAdmin(@RequestParam Integer reviewId) {
        log.info("管理员删除评价, reviewId={}", reviewId);
        if (reviewId == null) {
            return R.fail("评价ID不能为空");
        }
        return storeStaffReviewService.deleteReviewByAdmin(reviewId);
    }

    @ApiOperation("根据人员ID查询评价列表（包含人员信息）")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数（默认1）", dataTypeClass = Integer.class, paramType = "query", required = false),
            @ApiImplicitParam(name = "size", value = "页容（默认10）", dataTypeClass = Integer.class, paramType = "query", required = false),
            @ApiImplicitParam(name = "staffUserId", value = "人员ID（必填）", dataTypeClass = Integer.class, paramType = "query", required = true),
            @ApiImplicitParam(name = "currentUserId", value = "当前用户ID（用于判断是否已点赞）", dataTypeClass = Integer.class, paramType = "query", required = false)
    })
    @GetMapping("/list/byStaffId")
    public R<StoreStaffReviewListWithStaffVo> getReviewListByStaffId(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam Integer staffUserId,
            @RequestParam(required = false) Integer currentUserId) {
        log.info("根据人员ID查询评价列表, page={}, size={}, staffUserId={}, currentUserId={}",
                page, size, staffUserId, currentUserId);
        if (staffUserId == null) {
            return R.fail("人员ID不能为空");
        }
        return storeStaffReviewService.getReviewListByStaffId(page, size, staffUserId, currentUserId);
    }

    @ApiOperation("获取员工评价统计数据（全部数量、好评数量、中评数量、差评数量、有图数量）")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "staffUserId", value = "员工用户ID", dataTypeClass = Integer.class, paramType = "query", required = true)
    })
    @GetMapping("/statistics")
    public R<StoreStaffReviewStatisticsVo> getStaffReviewStatistics(@RequestParam Integer staffUserId) {
        log.info("获取员工评价统计数据, staffUserId={}", staffUserId);
        if (staffUserId == null) {
            return R.fail("员工ID不能为空");
        }
        return storeStaffReviewService.getStaffReviewStatistics(staffUserId);
    }

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