<?php

namespace Kylin\Wechat\Services;

use Illuminate\Contracts\Container\Container;

/**
 * 微信企业付款服务类
 */
final class WechatTransferService
{
    /**
     * 构造函数
     */
    public function __construct(
        private readonly Container $app
    ) {}

    /**
     * 企业付款到零钱
     */
    public function transferToBalance(array $params, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->transfer->toBalance($params);

        return [
            'code' => 'SUCCESS',
            'message' => '企业付款到零钱成功',
            'data' => $result
        ];
    }

    /**
     * 企业付款到银行卡
     */
    public function transferToBank(array $params, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->transfer->toBank($params);

        return [
            'code' => 'SUCCESS',
            'message' => '企业付款到银行卡成功',
            'data' => $result
        ];
    }

    /**
     * 查询企业付款到零钱记录
     */
    public function queryTransfer(string $partnerTradeNo, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->transfer->queryBalance($partnerTradeNo);

        return [
            'code' => 'SUCCESS',
            'message' => '查询企业付款到零钱记录成功',
            'data' => $result
        ];
    }

    /**
     * 查询企业付款到银行卡记录
     */
    public function queryBankTransfer(string $partnerTradeNo, string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->transfer->queryBank($partnerTradeNo);

        return [
            'code' => 'SUCCESS',
            'message' => '查询企业付款到银行卡记录成功',
            'data' => $result
        ];
    }

    /**
     * 获取RSA加密公钥
     */
    public function getPublicKey(string $driver = 'payment', string $name = 'default'): array
    {
        $app = $this->getApp($driver, $name);
        
        $result = $app->transfer->getPublicKey();

        return [
            'code' => 'SUCCESS',
            'message' => '获取RSA加密公钥成功',
            'data' => $result
        ];
    }

    /**
     * 加密银行卡号
     */
    public function encryptBankCard(string $bankCardNo, string $publicKey, string $driver = 'payment', string $name = 'default'): string
    {
        $app = $this->getApp($driver, $name);
        
        return $app->transfer->encryptBankCard($bankCardNo, $publicKey);
    }

    /**
     * 加密身份证号
     */
    public function encryptIdCard(string $idCardNo, string $publicKey, string $driver = 'payment', string $name = 'default'): string
    {
        $app = $this->getApp($driver, $name);
        
        return $app->transfer->encryptIdCard($idCardNo, $publicKey);
    }

    /**
     * 获取微信应用实例
     */
    protected function getApp(string $driver, string $name): mixed
    {
        return $this->app->make('kylin.wechat')->driver($driver, $name);
    }
}
