package com.jeesharp.business.modules.finance.payrefund.controller;

import com.jeesharp.business.modules.finance.payrefund.application.PayRefundOrderApplicationService;
import com.jeesharp.business.modules.finance.payrefund.dto.PayRefundCreateDTO;
import com.jeesharp.business.modules.finance.payrefund.entity.PayRefundOrder;
import com.jeesharp.business.modules.finance.payrefund.vo.PayRefundOrderInfoVO;
import com.jeesharp.business.modules.finance.payrefund.vo.PayRefundOrderListVO;
import com.jeesharp.business.modules.finance.payrefund.vo.PayRefundOrderQueryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 支付退款单管理 REST 接口，提供列表、详情、发起退款与重试能力。
 *
 * @author jeesharp
 * @since 2026-06-11
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/finance/payRefund/v1")
@Tag(name = "支付退款")
@AllArgsConstructor
public class PayRefundOrderController extends BaseController {

    private final PayRefundOrderApplicationService payRefundOrderApplicationService;

    @GetMapping("info")
    @Operation(summary = "获取退款单详情")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:payRefund:info')")
    public AjaxResult<PayRefundOrderInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(payRefundOrderApplicationService.get(id));
    }

    @PostMapping("list")
    @Operation(summary = "退款单分页列表")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:payRefund:list')")
    public AjaxResult<PageRespVO<PayRefundOrderListVO>> list(@RequestBody PayRefundOrderQueryVO query) {
        return AjaxResult.okData(payRefundOrderApplicationService.findPage(new Page<>(query), query));
    }

    @PostMapping("create")
    @Operation(summary = "发起退款")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:payRefund:create')")
    public AjaxResult<String> create(@RequestBody @Valid PayRefundCreateDTO dto) {
        String id = payRefundOrderApplicationService.createAndExecute(dto);
        return AjaxResult.ok("退款已提交", id);
    }

    @PostMapping("retry")
    @Operation(summary = "重试退款")
    @PreAuthorize("@ss.hasPermission('manager:business:finance:payRefund:retry')")
    public AjaxResult<Void> retry(@RequestParam("id") String id) {
        payRefundOrderApplicationService.retry(id);
        return AjaxResult.ok("重试已提交");
    }
}
