<?php

declare(strict_types=1);

use App\Models\LegalEntity;
use App\Models\PaymentGateway;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Crypt;

uses(DatabaseTransactions::class);

it('сохраняет ключи шлюза в зашифрованном виде', function () {
    LegalEntity::create([
        'code' => 'le_'.uniqid(), 'name' => 'ООО Тест', 'legal_form' => 'OOO', 'inn' => '7700000000',
    ]);

    $resp = $this->putJson('/api/admin/payment-gateways/yookassa', [
        'shop_id' => 'shop_42',
        'secret_key' => 'live_secret_value',
        'is_active' => true,
    ]);

    $resp->assertOk();
    $gw = PaymentGateway::where('code', 'yookassa')->firstOrFail();
    expect($gw->is_active)->toBeTrue()
        ->and($gw->config)->not->toContain('live_secret_value') // зашифровано
        ->and(Crypt::decrypt($gw->config))->toBe(['shop_id' => 'shop_42', 'secret_key' => 'live_secret_value']);
});

it('требует юрлицо перед созданием шлюза', function () {
    $resp = $this->putJson('/api/admin/payment-gateways/yookassa', [
        'shop_id' => 'shop_1', 'secret_key' => 'sk', 'is_active' => true,
    ]);

    $resp->assertStatus(422);
});
