package org.dromara.main.controller.miniapp;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.main.service.IPaymentConfigService;
import org.dromara.system.domain.bo.PaymentConfigBo;
import org.dromara.system.domain.vo.PaymentConfigVo;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.ISysOssService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@SaIgnore
@RequiredArgsConstructor
@RestController
@RequestMapping("/miniapp/paymentConfig")
public class MiniappPaymentConfigController {

    private final IPaymentConfigService paymentConfigService;
    private final ISysOssService ossService;

    @GetMapping("/list")
    public R<PaymentConfigVo> getConfig() {
        return R.ok(paymentConfigService.getWechatPayConfig());
    }

    @RepeatSubmit
    @PutMapping("/wxpay")
    public R<Void> saveWxPayConfig(@RequestBody PaymentConfigBo bo) {
        paymentConfigService.saveWechatPayConfig(bo);
        return R.ok();
    }

    @RepeatSubmit
    @PostMapping(value = "/uploadPrivateKey", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> uploadPrivateKey(@RequestPart("file") MultipartFile file) {
        SysOssVo oss = ossService.upload(file);
        paymentConfigService.updateWechatPrivateKeyPath(oss.getUrl());
        return R.ok();
    }

    @RepeatSubmit
    @PostMapping(value = "/uploadCert", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> uploadCert(@RequestPart("file") MultipartFile file) {
        SysOssVo oss = ossService.upload(file);
        paymentConfigService.updateWechatCertPath(oss.getUrl());
        return R.ok();
    }

    @RepeatSubmit
    @PostMapping(value = "/uploadPublicKey", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> uploadPublicKey(@RequestPart("file") MultipartFile file) {
        SysOssVo oss = ossService.upload(file);
        paymentConfigService.updateWechatPublicKeyPath(oss.getUrl());
        return R.ok();
    }
}
