package com.tashow.cloud.pay.controller.admin.transfer.vo;

// 移除Swagger相关导入

import com.tashow.cloud.common.util.validation.ValidationUtils;
import com.tashow.cloud.common.validation.InEnum;
import com.tashow.cloud.sdk.payment.enums.order.PayOrderTypeEnum;
import com.tashow.cloud.sdk.payment.enums.transfer.PayTransferTypeEnum;
import jakarta.validation.Validator;
import jakarta.validation.constraints.*;
import lombok.Data;

import java.util.Map;

import static com.tashow.cloud.common.exception.enums.GlobalErrorCodeConstants.NOT_IMPLEMENTED;
import static com.tashow.cloud.common.exception.util.ServiceExceptionUtil.exception;
import static com.tashow.cloud.sdk.payment.enums.transfer.PayTransferTypeEnum.typeOf;


// 管理后台 - 发起转账 Request VO
@Data
public class PayTransferCreateReqVO {

    // 应用编号 - 必填 - 示例: 1
    @NotNull(message = "应用编号不能为空")
    private Long appId;

    // 商户转账单编号 - 必填 - 示例: 1
    @NotNull(message = "商户转账单编号不能为空")
    private String merchantTransferId;

    // 转账类型 - 必填 - 示例: 1
    @NotNull(message = "转账类型不能为空")
    @InEnum(PayTransferTypeEnum.class)
    private Integer type;

    // 转账渠道 - 必填 - 示例: alipay_pc
    @NotEmpty(message = "转账渠道不能为空")
    private String channelCode;

    @Min(value = 1, message = "转账金额必须大于零")
    @NotNull(message = "转账金额不能为空")
    private Integer price;

    // 转账标题 - 必填 - 示例: 示例转账
    @NotEmpty(message = "转账标题不能为空")
    private String subject;

    // 收款人姓名 - 示例: test1
    @NotBlank(message = "收款人姓名不能为空", groups = {PayTransferTypeEnum.Alipay.class})
    private String userName;

    // 支付宝登录号 - 示例: test1@sandbox.com
    @NotBlank(message = "支付宝登录号不能为空", groups = {PayTransferTypeEnum.Alipay.class})
    private String alipayLogonId;

    // 微信 openId - 示例: oLefc4g5Gxx
    @NotBlank(message = "微信 openId 不能为空", groups = {PayTransferTypeEnum.WxPay.class})
    private String openid;

    // 转账渠道的额外参数
    private Map<String, String> channelExtras;

    public void validate(Validator validator) {
       PayTransferTypeEnum transferType = typeOf(type);
        switch (transferType) {
            case ALIPAY_BALANCE: {
                ValidationUtils.validate(validator, this, PayTransferTypeEnum.Alipay.class);
                break;
            }
            case WX_BALANCE: {
                ValidationUtils.validate(validator, this, PayTransferTypeEnum.WxPay.class);
                break;
            }
            default: {
                throw new UnsupportedOperationException("待实现");
            }
        }
    }

    @AssertTrue(message = "转账类型和转账渠道不匹配")
    public boolean isValidChannelCode() {
        PayTransferTypeEnum transferType = typeOf(type);
        switch (transferType) {
            case ALIPAY_BALANCE: {
                return PayOrderTypeEnum.isAlipay(channelCode);
            }
            case WX_BALANCE:
            case BANK_CARD:
            case WALLET_BALANCE: {
                throw exception(NOT_IMPLEMENTED);
            }
        }
        return Boolean.FALSE;
    }

}
