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

import com.jeesharp.business.modules.pay.paychannelconfig.application.PayChannelConfigApplicationService;
import com.jeesharp.business.modules.pay.paychannelconfig.dto.PayChannelConfigSaveDTO;
import com.jeesharp.business.modules.pay.paychannelconfig.entity.PayChannelConfig;
import com.jeesharp.business.modules.pay.paychannelconfig.vo.PayChannelConfigInfoVO;
import com.jeesharp.business.modules.pay.paychannelconfig.vo.PayChannelConfigListVO;
import com.jeesharp.business.modules.pay.paychannelconfig.vo.PayChannelConfigQueryVO;
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-05-25
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/pay/payChannelConfig/v1")
@Tag(name = "支付渠道配置")
@AllArgsConstructor
public class PayChannelConfigController extends BaseController {

    private final PayChannelConfigApplicationService payChannelConfigApplicationService;

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

    @GetMapping("active")
    @Operation(summary = "获取指定渠道当前生效配置")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:payChannelConfig:info')")
    public AjaxResult<PayChannelConfigInfoVO> active(
            @RequestParam("channelCode") String channelCode,
            @RequestParam(value = "tenantId", required = false) String tenantId) {
        return AjaxResult.okData(payChannelConfigApplicationService.getActiveByChannelCode(channelCode, tenantId));
    }

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

    @PostMapping("create")
    @Operation(summary = "新增")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:payChannelConfig:create')")
    public AjaxResult<Void> create(@RequestBody @Valid PayChannelConfigSaveDTO dto) {
        payChannelConfigApplicationService.create(dto);
        return AjaxResult.ok("新增成功");
    }

    @PostMapping("update")
    @Operation(summary = "更新")
    @PreAuthorize("@ss.hasPermission('manager:business:pay:payChannelConfig:update')")
    public AjaxResult<Void> update(@RequestBody @Valid PayChannelConfigSaveDTO dto) {
        payChannelConfigApplicationService.update(dto);
        return AjaxResult.ok("更新成功");
    }

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