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.second.vo.SecondUserViolationDetailVo;
import shop.alien.entity.store.LifeUserViolation;
import shop.alien.entity.store.UserLoginInfo;
import shop.alien.entity.store.dto.LifeUserViolationDto;
import shop.alien.mapper.LifeNoticeMapper;
import shop.alien.store.annotation.TrackEvent;
import shop.alien.store.service.LifeUserViolationService;
import shop.alien.util.common.TokenInfo;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.util.Map;

/**
 * <p>
 * 用户举报 前端控制器
 * </p>
 *
 * @author ssk
 * @since 2025-04-29
 */
@Api(tags = {"用户举报"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/user-violation")
@RequiredArgsConstructor
public class UserViolationController {

    private final LifeUserViolationService lifeUserViolationService;

    private final LifeNoticeMapper lifeNoticeMapper;

    @TrackEvent(
            eventType = "REPORT",
            eventCategory = "INTERACTION",
            storeId = "",
            userId = "#lifeUserViolation.reportingUserId",
            targetId = "#lifeUserViolation.dynamicsId != null ? #lifeUserViolation.dynamicsId : #lifeUserViolation.commentId",
            targetType = "STORE"
    )
    @ApiOperation("举报")
    @ApiOperationSupport(order = 1)
    @PostMapping("/reporting")
    public R<String> reporting(@RequestBody LifeUserViolation lifeUserViolation) throws Exception {
        log.info("UserViolationController.reporting?lifeuserViolation={}", lifeUserViolation.toString());
        int reporting = lifeUserViolationService.reporting(lifeUserViolation);
        if (0 == reporting) {
            return R.fail("举报失败");
        } else {
            return R.data("举报成功");
        }
    }

    @ApiOperation("举报结果")
    @ApiOperationSupport(order = 2)
    @GetMapping("/reportListByUserId")
    public R<Map<String, Object>> reportListByUserId(@RequestParam(value = "userId") String userId) {
        return R.data(lifeUserViolationService.reportListByUserId(userId));
    }

    @ApiOperation("举报结果详细信息")
    @ApiOperationSupport(order = 3)
    @GetMapping("/reportListById")
    public R<Map<String, Object>> reportListById(@RequestParam(value = "id") String id) {
        return R.data(lifeUserViolationService.reportListById(id));
    }

    @ApiOperation("举报分页")
    @ApiOperationSupport(order = 4)
    @GetMapping("/page")
    public R<IPage<LifeUserViolationDto>> getViolationPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String nickname,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) String processingStatus) {
        log.info("UserViolationController.getViolationPage?pageNum={},pageSize={},nickName={},phone={},processingStatus={}", pageNum, pageSize, nickname, phone, processingStatus);
        return R.data(lifeUserViolationService.getViolationPage(pageNum, pageSize, nickname, phone, processingStatus));
    }

    @ApiOperation("举报分页（带时间筛选）")
    @ApiOperationSupport(order = 9)
    @GetMapping("/allPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "nickname", value = "昵称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "processingStatus", value = "处理状态", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "String", paramType = "query")
    })
    public R<IPage<LifeUserViolationDto>> getAllViolationPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(value = "nickname", required = false) String nickname,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "processingStatus", required = false) String processingStatus,
            @RequestParam(value = "reportContextType", required = false) String reportContextType,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime ) {
        log.info("UserViolationController.getAllViolationPage?pageNum={},pageSize={},nickName={},phone={},processingStatus={},startTime={},endTime={}", 
                pageNum, pageSize, nickname, phone, processingStatus, startTime, endTime);
        return R.data(lifeUserViolationService.getAllViolationPage(pageNum, pageSize, nickname, phone, processingStatus, startTime, endTime, reportContextType));
    }

    @ApiOperation(value = "举报审核")
    @ApiOperationSupport(order = 5)
    @GetMapping("/approve")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "approvalStatus", value = "审批状态（1：审批成功，2：审批失败）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "reportResult", value = "处理结果", dataType = "String", paramType = "query")
    })
    @ResponseBody
    public R approveStoreInfo(@RequestParam("id") int id, @RequestParam("processingStatus") String processingStatus, @RequestParam("reportResult") String reportResult) {
        log.info("StoreInfoController.approveStoreInfo?id={}&approvalStatus={}", id, processingStatus);
        lifeUserViolationService.approve(id, processingStatus, reportResult);
        return R.success("审批完成");
    }

    @ApiOperation("查看详情")
    @ApiOperationSupport(order = 6)
    @GetMapping("/byId")
    public LifeUserViolationDto byId(@RequestParam(defaultValue = "10") int id) {
        return lifeUserViolationService.byId(id);
    }


    @ApiOperation("查看通知详情")
    @ApiOperationSupport(order = 6)
    @GetMapping("/byIdNotice")
    public LifeUserViolationDto byIdNotice(@RequestParam(defaultValue = "10") int id) {
        return lifeUserViolationService.byIdNotice(id);
    }


    @ApiOperation(value = "web端导出用户举报")
    @ApiOperationSupport(order = 7)
    @GetMapping("/exportExcel")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nickname", value = "姓名", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "联系电话", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "processingStatus", value = "状态", dataType = "String", paramType = "query"),
    })
    @ResponseBody
    public R exportExcel(@RequestParam(value = "nickname", required = false) String nickname
            , @RequestParam(value = "phone", required = false) String phone
            , @RequestParam(value = "processingStatus", required = false) String processingStatus) throws IOException {
        log.info("StoreInfoController.exportExcel");
        String excelPath = lifeUserViolationService.exportExcel(nickname, phone, processingStatus);
        return R.data(excelPath);
    }

    @ApiOperation("举报等级")
    @ApiOperationSupport(order = 8)
    @PostMapping("/level")
    public R<String> level(@ApiIgnore @TokenInfo UserLoginInfo userLoginInfo) {
        return R.data(lifeUserViolationService.level(userLoginInfo));
    }


    @ApiOperation("中台举报详情")
    @ApiOperationSupport(order = 9)
    @GetMapping("/queryViolationDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "reportContextType", value = "举报类型", dataType = "String", paramType = "query"),
    })
    public R<SecondUserViolationDetailVo> queryViolationDetail(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "reportContextType", required = false) String reportContextType) {
        log.info("UserViolationController.queryViolationDetail?id={}&reportContextType={}", id, reportContextType);
        return R.data(lifeUserViolationService.queryViolationDetail(id, reportContextType));
    }
}
