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.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartRequest;
import shop.alien.entity.result.R;
import shop.alien.entity.store.vo.StoreCommentAppealInfoVo;
import shop.alien.entity.store.vo.StoreCommentAppealVo;
import shop.alien.store.annotation.TrackEvent;
import shop.alien.store.service.StoreCommentAppealService;

import java.util.Map;
import java.util.Set;

/**
 * 评论申诉表 前端控制器
 *
 * @author ssk
 * @since 2025-01-02
 */
@Slf4j
@Api(tags = {"二期-评论申诉"})
@ApiSort(12)
@CrossOrigin
@RestController
@RequestMapping("/storeCommentAppeal")
@RequiredArgsConstructor
public class StoreCommentAppealController {

    private final StoreCommentAppealService storeCommentAppealService;

    @ApiOperationSupport(order = 1)
    @ApiOperation("商家端-申诉历史")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "appealStatus", value = "申诉状态: 0:待处理, 1:已驳回, 2:已同意", dataType = "Integer", paramType = "query")})
    @GetMapping("/getAppealHistory")
    public R<IPage<StoreCommentAppealVo>> getAppealHistory(Integer pageNum, Integer pageSize, Integer storeId, String appealStatus) {
        log.info("StoreCommentAppealController.getAppealHistory?pageNum={}&pageSize={}&storeId={}&appealStatus={}", pageNum, pageSize, storeId, appealStatus);
        return R.data(storeCommentAppealService.getAppealHistory(pageNum, pageSize, storeId, appealStatus));
    }

    @TrackEvent(
            eventType = "APPEAL",
            eventCategory = "SERVICE",
            storeId = "#{#storeId}",
            userId = "",
            targetId = "#{#commentId}",
            targetType = "STORE"
    )
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "商家端-新增申诉(0:申诉成功, 1:申诉失败, 2:申诉已存在, 3:文本内容异常)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "imgUrls", value = "申诉图片OSS地址，多个用英文逗号分隔", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "commentId", value = "评论id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "appealReason", value = "申诉原因", dataType = "String", paramType = "query", required = true)})
    @PostMapping("/addAppealNew")
    public R<StoreCommentAppealInfoVo> addAppealNew(@RequestParam(value = "imgUrls", required = false) String imgUrls,
                                                 @RequestParam("storeId") Integer storeId,
                                                 @RequestParam("commentId") Integer commentId,
                                                 @RequestParam("appealReason") String appealReason) {
        log.info("StoreCommentAppealController.addAppealNew?imgUrls={}&storeId={}&commentId={}&appealReason={}", imgUrls, storeId, commentId, appealReason);
        return R.data(storeCommentAppealService.addAppealNew(imgUrls, storeId, commentId, appealReason));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "商家端-新增申诉(0:申诉成功, 1:申诉失败, 2:申诉已存在)")
    @ApiImplicitParams({@ApiImplicitParam(name = "multipartRequest", value = "文件", dataType = "File", paramType = "query", required = true),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "commentId", value = "评论id", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "appealReason", value = "申诉原因", dataType = "String", paramType = "query", required = true)})
    @PostMapping("/addAppeal")
    public R<Integer> addAppeal(MultipartRequest multipartRequest,
                                @RequestParam("storeId") Integer storeId,
                                @RequestParam("commentId") Integer commentId,
                                @RequestParam("appealReason") String appealReason) {
        Set<String> fileNameSet = multipartRequest.getMultiFileMap().keySet();
        log.info("StoreCommentAppealController.addAppeal?multipartRequest={}&storeId={}&commentId={}&appealReason={}", fileNameSet, storeId, commentId, appealReason);
        return R.data(storeCommentAppealService.addAppeal(multipartRequest, storeId, commentId, appealReason));
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "商家端-申诉详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "申诉id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getAppealDetail")
    public R<StoreCommentAppealVo> getAppealDetail(Integer id) {
        log.info("StoreCommentAppealController.getAppealDetail?id={}", id);
        return R.data(storeCommentAppealService.getStoreCommentAppealDetail(id));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "按评价ID查询申诉详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "commentId", value = "评价ID（common_rating.id）", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getAppealDetailByCommentId")
    public R<StoreCommentAppealVo> getAppealDetailByCommentId(@RequestParam Integer commentId) {
        log.info("StoreCommentAppealController.getAppealDetailByCommentId?commentId={}", commentId);
        return R.data(storeCommentAppealService.getStoreCommentAppealDetailByCommentId(commentId));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "web-申诉分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "storeName", value = "门店名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "storeContact", value = "门店联系人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "storePhone", value = "门店电话", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "appealStatus", value = "申诉状态: 0:待处理, 1:已驳回, 2:已同意", dataType = "String", paramType = "query")
    })
    @GetMapping("/getStoreAppealPage")
    public R<IPage<StoreCommentAppealVo>> getStoreAppealPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String storeName,
            @RequestParam(required = false) String storeContact,
            @RequestParam(required = false) String storePhone,
            @RequestParam(required = false) String appealStatus) {
        log.info("StoreCommentAppealController.getStoreAppealPage?pageNum={}&pageSize={}&storeName={}&storeContact={}&storePhone={}&appealType={}", pageNum, pageSize, storeName, storeContact, storePhone, appealStatus);
        return R.data(storeCommentAppealService.getStoreAppealPage(pageNum, pageSize, storeName, storeContact, storePhone, appealStatus));
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "web-修改申诉状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "申诉id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "appealStatus", value = "申诉状态: 0:待处理, 1:已驳回, 2:已同意", dataType = "String", paramType = "query")
    })
    @GetMapping("/setAppealStatus")
    public R<Boolean> setAppealStatus(Integer id, Integer appealStatus, @RequestParam(required = false, defaultValue = "") String logRemark) {
        log.info("StoreCommentAppealController.setAppealStatus?id={}&appealStatus={}&logRemark={}", id, appealStatus, logRemark);
        if (storeCommentAppealService.setAppealStatus(id, appealStatus, logRemark)) {
            return R.success("修改申诉状态成功");
        }
        return R.fail("修改申诉状态失败");
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "web-导出Excel")
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportToExcel() {
        log.info("StoreCommentAppealController.exportToExcel");
        return storeCommentAppealService.exportToExcel();
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation("申诉历史各状态数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getAppealHistoryCountStatus")
    public R getAppealHistoryCountStatus(Integer storeId) {
        log.info("StoreCommentAppealController.getAppealHistoryCountStatus?storeId={}", storeId);
        return R.data(storeCommentAppealService.getAppealHistoryCountStatus(storeId));
    }

    @ApiOperation("是否可以评价（含最新申诉通过记录与10天冷却倒计时）")
    @GetMapping("/canRate")
    public R<Map<String, Object>> canRate(Integer userId) {
        log.info("StoreCommentAppealController.canRate?userId={}", userId);
        return R.data(storeCommentAppealService.canRate(userId));
    }
}
