package com.jeesharp.business.modules.pay.paywechatconfig.controller;

import com.jeesharp.business.modules.pay.paywechatconfig.application.PayWechatConfigApplicationService;
import com.jeesharp.business.modules.pay.paywechatconfig.dto.PayWechatConfigSaveDTO;
import com.jeesharp.business.modules.pay.paywechatconfig.entity.PayWechatConfig;
import com.jeesharp.business.modules.pay.paywechatconfig.vo.PayWechatConfigInfoVO;
import com.jeesharp.business.modules.pay.paywechatconfig.vo.PayWechatConfigListVO;
import com.jeesharp.business.modules.pay.paywechatconfig.vo.PayWechatConfigQueryVO;
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.*;

/**
 * 微信支付 V3 配置管理端 REST 接口。
 *
 * @author jeesharp
 * @since 2026-05-25
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/pay/payWechatConfig/v1")
@Tag(name = "微信支付配置")
@AllArgsConstructor
public class PayWechatConfigController extends BaseController {

    private final PayWechatConfigApplicationService payWechatConfigApplicationService;

    @GetMapping("info")
    @Operation(summary = "获取详情")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:payWechatConfig:info')")
    public AjaxResult<PayWechatConfigInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(payWechatConfigApplicationService.get(id));
    }

    @GetMapping("active")
    @Operation(summary = "获取当前生效配置")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:payWechatConfig:info')")
    public AjaxResult<PayWechatConfigInfoVO> active(
            @RequestParam(value = "tenantId", required = false) String tenantId,
            @RequestParam(value = "channelConfigId", required = false) String channelConfigId,
            @RequestParam(value = "payChannelConfigCode", required = false) String payChannelConfigCode) {
        return AjaxResult.okData(
                payWechatConfigApplicationService.resolveActive(tenantId, channelConfigId, payChannelConfigCode));
    }

    @PostMapping("list")
    @Operation(summary = "分页列表")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:payWechatConfig:list')")
    public AjaxResult<PageRespVO<PayWechatConfigListVO>> list(@RequestBody PayWechatConfigQueryVO query) {
        return AjaxResult.okData(payWechatConfigApplicationService.findPage(new Page<>(query), query));
    }

    @PostMapping("save")
    @Operation(summary = "保存")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:payWechatConfig:save')")
    public AjaxResult<Void> save(@RequestBody @Valid PayWechatConfigSaveDTO dto) {
        payWechatConfigApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

    @GetMapping("delete")
    @Operation(summary = "删除")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:payWechatConfig:delete')")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        payWechatConfigApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }
}
