<?php

namespace Tests\Unit;

use App\Services\Stripe\PaymentGatewayConfigurationService;
use Tests\TestCase;

class PaymentGatewayConfigurationServiceTest extends TestCase
{
    public function test_normalize_removes_legacy_top_level_keys(): void
    {
        $service = app(PaymentGatewayConfigurationService::class);

        $normalized = $service->normalize([
            'stripe' => [
                'stripe_account_id' => 'acct_nested',
                'email' => 'a@example.com',
                'country' => 'us',
            ],
            'stripe_account_id' => 'acct_legacy',
            'email' => 'legacy@example.com',
            'country' => 'ca',
            'status' => 'active',
            'viva' => ['status' => 'inactive'],
        ]);

        $this->assertSame('acct_nested', data_get($normalized, 'stripe.stripe_account_id'));
        $this->assertArrayNotHasKey('stripe_account_id', $normalized);
        $this->assertArrayNotHasKey('email', $normalized);
        $this->assertArrayNotHasKey('country', $normalized);
        $this->assertArrayNotHasKey('status', $normalized);
        $this->assertSame('inactive', data_get($normalized, 'viva.status'));
    }

    public function test_collect_candidate_account_ids_prefers_unique_ordered_list(): void
    {
        $service = app(PaymentGatewayConfigurationService::class);

        $ids = $service->collectCandidateAccountIds([
            'stripe' => [
                'stripe_account_id' => 'acct_current',
                'previous_stripe_account_id' => 'acct_previous',
            ],
            'stripe_account_id' => 'acct_legacy',
        ]);

        $this->assertSame(['acct_current', 'acct_previous', 'acct_legacy'], $ids);
    }
}
