<?php

namespace Kylin\Payment\Services;

use Illuminate\Contracts\Container\Container;

/**
 * 版本管理服务类
 */
class VersionManagerService
{
    /**
     * 支付SDK版本映射
     */
    protected array $versionMap = [
        'wechat' => [
            'v2' => 'Kylin\Payment\Gateways\WechatGateway',
            'v3' => 'Kylin\Payment\Gateways\WechatGateway',
        ],
        'alipay' => [
            'v1' => 'Kylin\Payment\Gateways\AlipayGateway',
            'v2' => 'Kylin\Payment\Gateways\AlipayGateway',
        ],
        'unionpay' => [
            'v1' => 'Kylin\Payment\Gateways\UnionpayGateway',
        ],
        'douyin' => [
            'v1' => 'Kylin\Payment\Gateways\DouyinGateway',
        ],
        'jsbank' => [
            'v1' => 'Kylin\Payment\Gateways\JsbankGateway',
        ],
        'alipay_global' => [
            'v1' => 'Kylin\Payment\Gateways\AlipayGlobalGateway',
        ],
        'paypal' => [
            'v1' => 'Kylin\Payment\Gateways\PaypalGateway',
            'v2' => 'Kylin\Payment\Gateways\PaypalGateway',
        ],
        'stripe' => [
            'v1' => 'Kylin\Payment\Gateways\StripeGateway',
            'v2' => 'Kylin\Payment\Gateways\StripeGateway',
        ],
        'google_pay' => [
            'v1' => 'Kylin\Payment\Gateways\GooglePayGateway',
        ],
        'apple_pay' => [
            'v1' => 'Kylin\Payment\Gateways\ApplePayGateway',
        ],
    ];

    /**
     * 构造函数
     */
    public function __construct(
        protected readonly Container $app
    ) {
    }

    /**
     * 获取支付网关实例
     */
    public function getGateway(string $type, array $config, ?string $version = null): object
    {
        // 获取版本
        $version = $version ?? $this->getDefaultVersion($type);

        // 获取网关类
        $gatewayClass = $this->getGatewayClass($type, $version);

        // 创建实例
        return new $gatewayClass($config);
    }

    /**
     * 获取默认版本
     */
    public function getDefaultVersion(string $type): string
    {
        if (isset($this->versionMap[$type])) {
            $versions = array_keys($this->versionMap[$type]);
            return end($versions);
        }

        return 'v1';
    }

    /**
     * 获取网关类
     */
    public function getGatewayClass(string $type, string $version): string
    {
        if (isset($this->versionMap[$type][$version])) {
            return $this->versionMap[$type][$version];
        }

        // 如果指定版本不存在，返回默认版本
        $defaultVersion = $this->getDefaultVersion($type);
        return $this->versionMap[$type][$defaultVersion] ?? '';
    }

    /**
     * 获取支持的版本列表
     */
    public function getSupportedVersions(string $type): array
    {
        return $this->versionMap[$type] ?? [];
    }

    /**
     * 注册新版本
     */
    public function registerVersion(string $type, string $version, string $gatewayClass): self
    {
        if (!isset($this->versionMap[$type])) {
            $this->versionMap[$type] = [];
        }

        $this->versionMap[$type][$version] = $gatewayClass;

        return $this;
    }
}
