<?php

declare(strict_types=1);

namespace Tests\Feature;

use App\Models\Setting;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Plugins\Neatstudio\Payment\Services\PaymentManager;
use Tests\TestCase;

class PaymentGatewayConfigTest extends TestCase
{
    use RefreshDatabase;

    public function test_alipay_config_lands_in_default_tenant(): void
    {
        Setting::set('pay_alipay_mode', 'sandbox');
        Setting::set('pay_alipay_app_id', '2021000000000000');
        Setting::set('pay_alipay_app_secret', "-----BEGIN PRIVATE KEY-----\nAAAA\n-----END PRIVATE KEY-----");
        Setting::set('pay_alipay_public_key', "-----BEGIN PUBLIC KEY-----\nBBBB\n-----END PUBLIC KEY-----");

        app(PaymentManager::class)->alipay();

        $config = \Yansongda\Pay\get_provider_config('alipay');

        $this->assertSame(\Yansongda\Pay\Pay::MODE_SANDBOX, $config['mode']);
        $this->assertSame('2021000000000000', $config['app_id']);
        $this->assertStringContainsString('PRIVATE KEY', $config['app_secret_cert']);
        $this->assertStringContainsString('PUBLIC KEY', $config['alipay_public_cert_path']);
        $this->assertStringContainsString('pay/notify', $config['notify_url']);
    }

    public function test_wechat_config_lands_in_default_tenant(): void
    {
        Setting::set('pay_wechat_mode', 'normal');
        Setting::set('pay_wechat_mch_id', '1900000001');
        Setting::set('pay_wechat_mch_secret', 'api-v3-key');
        Setting::set('pay_wechat_mch_cert', "-----BEGIN PRIVATE KEY-----\nCCCC\n-----END PRIVATE KEY-----");
        Setting::set('pay_wechat_platform_cert', "-----BEGIN PUBLIC KEY-----\nDDDD\n-----END PUBLIC KEY-----");
        Setting::set('pay_wechat_app_id', 'wx123456');

        app(PaymentManager::class)->wechat();

        $config = \Yansongda\Pay\get_provider_config('wechat');

        $this->assertSame(\Yansongda\Pay\Pay::MODE_NORMAL, $config['mode']);
        $this->assertSame('1900000001', $config['mch_id']);
        $this->assertSame('api-v3-key', $config['mch_secret_key']);
        $this->assertStringContainsString('PRIVATE KEY', $config['mch_secret_cert']);
        $this->assertStringContainsString('PUBLIC KEY', $config['mch_public_cert_path']);
        $this->assertSame('wx123456', $config['mp_app_id']);
    }
}
