package com.seer.teach.pay.admin.withdrawal.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.pay.admin.withdrawal.controller.request.PayWithdrawalApplyPageRequest;
import com.seer.teach.pay.admin.withdrawal.controller.request.PayWithdrawalApplyReq;
import com.seer.teach.pay.admin.withdrawal.controller.response.PayWithdrawalApplyResponse;
import com.seer.teach.pay.admin.withdrawal.service.AdminPayWithdrawalApplyQueryService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 申请提现控制器
 *
 * @author ZS
 * @since 2025-09-17
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/pay/merchant/withdrawal/apply")
@Tag(name = "商户端 - 提现申请管理")
public class MerchantWithdrawalApplyController {

    private final AdminPayWithdrawalApplyQueryService payWithdrawalApplyService;

    @PostMapping("/paged")
    @Operation(summary = "分页查询提现申请")
    @SaCheckPermission("admin:pay:withdrawal:apply:page")
    public ResultBean<PageListBean<PayWithdrawalApplyResponse>> pageList(@Parameter(description = "分页查询参数") @RequestBody PayWithdrawalApplyPageRequest pageRequest) {
        pageRequest.setUserId(StpUtil.getLoginIdAsLong());
        return ResultBean.success(payWithdrawalApplyService.pageList(pageRequest));
    }

    @PostMapping("/list")
    @Operation(summary = "获取PayWithdrawalApply列表")
    @SaCheckPermission("admin:pay:withdrawal:apply:list")
    public ResultBean<List<PayWithdrawalApplyResponse>> list(@Parameter(description = "查询参数") @RequestBody PayWithdrawalApplyPageRequest queryParam) {
        queryParam.setUserId(StpUtil.getLoginIdAsLong());
        return ResultBean.success(payWithdrawalApplyService.list(queryParam));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取提现申请")
    @SaCheckPermission("admin:pay:withdrawal:apply:detail")
    public ResultBean<PayWithdrawalApplyResponse> getDetail(@Parameter(description = "ID") @PathVariable Long id) {
        long userId = StpUtil.getLoginIdAsLong();
        PayWithdrawalApplyResponse detail = payWithdrawalApplyService.getDetail(id);
        if (detail == null || detail.getUserId() != userId) {
            return ResultBean.error(ResultCodeEnum.UNAUTHORIZED);
        }
        return ResultBean.success(detail);
    }

    @PostMapping
    @Operation(summary = "提现申请")
    @SaCheckPermission("admin:pay:withdrawal:apply:create")
    public ResultBean<String> applyWithdrawal(@Parameter(description = "提现申请参数") @RequestBody PayWithdrawalApplyReq payWithdrawalApply) {
        return ResultBean.success(payWithdrawalApplyService.applyWithdrawal(payWithdrawalApply));
    }
}