package com.cool.store.controller.webc;

import com.cool.store.context.PartnerUserHolder;
import com.cool.store.enums.tp.TpFormTypeEnum;
import com.cool.store.request.tp.*;
import com.cool.store.response.AuditInfoResponse;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.tp.TpApplyService;
import com.cool.store.vo.PartnerUserInfoVO;
import com.cool.store.vo.tp.TpStoreScoreVO;
import com.cool.store.vo.tp.mini.MiniTpApplyListVO;
import com.cool.store.vo.tp.mini.MiniTpPenaltyApplyVO;
import com.cool.store.vo.tp.mini.MiniTpRewardApplyVO;
import com.cool.store.vo.tp.mini.MiniTpRuleListVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 十二分制-申请单 前端控制器
 * </p>
 *
 * @author wangff
 * @since 2025/11/10
 */
@Api(tags = "Mini十二分制-申请单")
@RestController
@RequestMapping("/mini/tp/apply")
@RequiredArgsConstructor
@Validated
public class MiniTpApplyController {
    private final TpApplyService tpApplyService;

    @ApiOperation("加分申请提交or保存")
    @PostMapping("/rewardApplySubmit")
    public ResponseResult<Boolean> rewardApplySubmit(@RequestBody @Validated TpApplyRequest request) {
        PartnerUserInfoVO user = PartnerUserHolder.getUser();
        request.setSource(1);
        request.setApplyUserId(user.getPartnerId());
        request.setApplyUserName(user.getUsername());
        return ResponseResult.success(tpApplyService.rewardApplySubmit(request));
    }

    @ApiOperation("加分申请单详情")
    @GetMapping("/rewardDetail")
    @ApiImplicitParam(name = "applyId", value = "加分申请单id", required = true, dataType = "Long", paramType = "query")
    public ResponseResult<MiniTpRewardApplyVO> rewardDetail(@NotNull(message = "加分申请单id不能为空") Long applyId) {
        return ResponseResult.success(tpApplyService.getMiniRewardApplyDetail(applyId));
    }

    @ApiOperation("加分申请单分页查询")
    @PostMapping("/rewardPage")
    public ResponseResult<PageInfo<MiniTpApplyListVO>> rewardPage(@RequestBody TpApplyQueryRequest request) {
        request.setType(TpFormTypeEnum.REWARD.getType());
        return ResponseResult.success(tpApplyService.getMiniApplyList(request));
    }

    @ApiOperation("惩处申请单详情")
    @GetMapping("/penaltyDetail")
    @ApiImplicitParam(name = "applyId", value = "惩处申请单id", required = true, dataType = "Long", paramType = "query")
    public ResponseResult<MiniTpPenaltyApplyVO> penaltyDetail(@NotNull(message = "惩处申请单id不能为空") Long applyId) {
        return ResponseResult.success(tpApplyService.getMiniPenaltyApplyDetail(applyId));
    }

    @ApiOperation("惩处申请单分页查询")
    @PostMapping("/penaltyPage")
    public ResponseResult<PageInfo<MiniTpApplyListVO>> penaltyPage(@RequestBody TpApplyQueryRequest request) {
        request.setIsPenalty(true);
        request.setIsDraft(0);
        return ResponseResult.success(tpApplyService.getMiniApplyList(request));
    }

    @ApiOperation("复议申请提交or保存")
    @PostMapping("/appealApplySubmit")
    public ResponseResult<Boolean> appealApplySubmit(@RequestBody @Validated TpAppealApplyRequest request) {
        PartnerUserInfoVO user = PartnerUserHolder.getUser();
        request.setSource(1);
        request.setApplyUserId(user.getPartnerId());
        request.setApplyUserName(user.getUsername());
        return ResponseResult.success(tpApplyService.appealApplySubmit(request));
    }

    @ApiOperation("审批记录列表")
    @GetMapping("/auditRecords")
    @ApiImplicitParam(name = "applyId", value = "申请单id", required = true, dataType = "Long", paramType = "query")
    public ResponseResult<List<AuditInfoResponse>> auditRecords(@NotNull(message = "申请单id不能为空") Long applyId) {
        return ResponseResult.success(tpApplyService.getAuditRecordList(applyId));
    }

    @ApiOperation("规则分页查询")
    @PostMapping("/rulePage")
    public ResponseResult<PageInfo<MiniTpRuleListVO>> rewardRulePage(@RequestBody TpRuleQueryRequest request) {
        request.setStatus(1);
        return ResponseResult.success(tpApplyService.getMiniRulePage(request));
    }

    @ApiOperation("认罚缴款")
    @PostMapping("/acceptPenalty")
    public ResponseResult<Boolean> acceptPenalty(@RequestBody @Validated TpApplyIdRequest request) {
        return ResponseResult.success(tpApplyService.acceptPenalty(request.getApplyId()));
    }

    @ApiOperation("撤销复议申请")
    @PostMapping("/withdrawAppeal")
    public ResponseResult<Boolean> withdrawAppeal(@RequestBody @Validated TpApplyIdRequest request) {
        return ResponseResult.success(tpApplyService.withdrawAppeal(request.getApplyId()));
    }

    @ApiOperation("门店积分")
    @GetMapping("/storeScore")
    @ApiImplicitParam(name = "storeId", value = "门店id", required = true, dataType = "String", paramType = "query")
    public ResponseResult<TpStoreScoreVO> storeScore(@NotBlank(message = "门店id不能为空") String storeId) {
        return ResponseResult.success(tpApplyService.getStoreScore(storeId));
    }
}
