package shop.alien.lawyer.controller;

import com.alibaba.nacos.client.naming.utils.RandomUtils;
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.CommentAppeal;
import shop.alien.entity.store.dto.AuditAppealRequestDto;
import shop.alien.entity.store.vo.CommentAppealVo;
import shop.alien.lawyer.service.CommentAppealService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 评论申诉表 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"评论申诉管理"})
@ApiSort(20)
@CrossOrigin
@RestController
@RequestMapping("/commentAppeal")
@RequiredArgsConstructor
public class CommentAppealController {

    private final CommentAppealService commentAppealService;

    @ApiOperation(value = "提交申诉", notes = "用户提交评论申诉")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentAppeal", value = "申诉信息", dataType = "CommentAppeal", paramType = "body", required = true)
    })
    @PostMapping("/submit")
    public R<CommentAppeal> submitAppeal(@RequestBody CommentAppeal commentAppeal) {
        log.info("CommentAppealController.submitAppeal?commentAppeal={}", commentAppeal);
        //申诉时间
//        commentAppeal.setAppealTime(new Date());
//        commentAppeal.setAppealNumber(generateOrderNumber());
        return commentAppealService.submitAppeal(commentAppeal);
    }

    @ApiOperation(value = "审核申诉", notes = "管理员审核申诉，状态：1-已通过，2-已驳回。审核通过时会删除评价及回复，并发送通知给相关用户")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "auditAppealRequestDto", value = "审核申诉请求", dataType = "AuditAppealRequestDto", paramType = "body", required = true)
    })
    @PostMapping("/audit")
    public R<Boolean> auditAppeal(@RequestBody AuditAppealRequestDto auditAppealRequestDto) {
        log.info("CommentAppealController.auditAppeal?auditAppealRequestDto={}", auditAppealRequestDto);
        return commentAppealService.auditAppeal(auditAppealRequestDto.getId(), auditAppealRequestDto.getStatus(), auditAppealRequestDto.getReviewReasons());
    }

    @ApiOperation(value = "中台申诉列表", notes = "分页查询申诉列表，支持多条件筛选，包含订单编号、用户信息、律师信息等")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页容（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "申诉状态：0-待处理，1-已通过，2-已驳回", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "orderNumber", value = "评价单号（模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userName", value = "用户姓名（模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userPhone", value = "用户电话（模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "lawyerName", value = "律师姓名（模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "lawyerPhone", value = "律师电话（模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间（格式：yyyy-MM-dd HH:mm:ss）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间（格式：yyyy-MM-dd HH:mm:ss）", dataType = "String", paramType = "query")
    })
    @GetMapping("/getPage")
    public R<IPage<CommentAppealVo>> getAppealPage(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String orderNumber,
            @RequestParam(required = false) String userName,
            @RequestParam(required = false) String userPhone,
            @RequestParam(required = false) String lawyerName,
            @RequestParam(required = false) String lawyerPhone,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        log.info("CommentAppealController.getAppealPage?pageNum={}, pageSize={}, status={}, orderNumber={}, userName={}, userPhone={}, lawyerName={}, lawyerPhone={}, startTime={}, endTime={}",
                pageNum, pageSize, status, orderNumber, userName, userPhone, lawyerName, lawyerPhone, startTime, endTime);
        return commentAppealService.getAppealPage(pageNum, pageSize, status, orderNumber, userName, userPhone, lawyerName, lawyerPhone, startTime, endTime);
    }

    @ApiOperation(value = "获取申诉详情", notes = "根据申诉ID获取申诉详情，包含评价和用户信息")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "申诉ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getDetail")
    public R<CommentAppealVo> getAppealDetail(@RequestParam("id") Integer id) {
        log.info("CommentAppealController.getAppealDetail?id={}", id);
        return commentAppealService.getAppealDetail(id);
    }

    @ApiOperation(value = "申诉历史列表", notes = "分页查询申诉历史列表，支持按状态和评论ID筛选，包含评价和用户信息")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数（默认1）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页容（默认10）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "申诉状态：0-待处理，1-已通过，2-已驳回", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "lawyerUserId", value = "律师用户ID", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getAppealHistory")
    public R<List<CommentAppealVo>> getAppealHistory(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) Integer lawyerUserId) {
        log.info("CommentAppealController.getAppealHistory?pageNum={}, pageSize={}, status={}, lawyerUserId={}",
                pageNum, pageSize, status, lawyerUserId);
        List<CommentAppealVo> appealList = commentAppealService.getAppealHistory(pageNum, pageSize, status, lawyerUserId);
        return R.data(appealList, "查询成功");
    }

//    private String generateOrderNumber() {
//        String dateStr = new SimpleDateFormat("yyyyMMdd").format(new Date());
//        String randomStr = String.format("%05d", RandomUtils.nextInt(100000));
//        return "LAW" + dateStr + randomStr;
//    }
}


