<?php

namespace Tests\Feature;

use App\Models\AllowedAdAccount;
use App\Services\Platforms\Support\Guardrails;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class SharedGoogleAccountMigrationTest extends TestCase
{
    use RefreshDatabase;

    public function test_fresh_migration_allows_only_the_configured_google_account(): void
    {
        config(['platforms.guardrails.allowed_ad_accounts' => []]);

        $this->assertDatabaseHas('allowed_ad_accounts', [
            'platform' => 'google',
            'account_id' => '3460855874',
            'is_active' => true,
        ]);
        $this->assertTrue(app(Guardrails::class)->allows('3460855874', 'google'));
        $this->assertFalse(app(Guardrails::class)->allows('9876543210', 'google'));
    }

    public function test_rerunning_migration_does_not_reactivate_an_account_an_admin_disabled(): void
    {
        $account = AllowedAdAccount::query()
            ->where('platform', 'google')
            ->where('account_id', '3460855874')
            ->firstOrFail();
        $account->update(['is_active' => false]);

        $migration = require database_path('migrations/2026_10_06_124356_allow_shared_google_ads_account.php');
        $migration->up();

        $this->assertFalse($account->fresh()->is_active);
        $this->assertSame(1, AllowedAdAccount::query()->where('platform', 'google')->where('account_id', '3460855874')->count());
    }
}
