<?php

declare(strict_types=1);

use App\Filament\Resources\ProviderConnectionResource\Pages\ListProviderConnections;
use App\Models\ProviderConnection;
use Filament\Facades\Filament;
use Livewire\Livewire;

it('exposes required provider connections list filters and applies the default-only filter', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner', ensureDefaultMicrosoftProviderConnection: false);

    $defaultConnection = ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
        'display_name' => 'Default Connection',
        'provider' => 'microsoft',
        'is_default' => true,
    ]);

    $nonDefaultConnection = ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
        'display_name' => 'Non Default Connection',
        'provider' => 'microsoft',
        'is_default' => false,
    ]);

    $this->actingAs($user);
    $tenant->makeCurrent();
    Filament::setTenant($tenant, true);

    $component = Livewire::test(ListProviderConnections::class);

    $filters = $component->instance()->getTable()->getFilters();
    $filterNames = array_keys($filters);

    expect($filterNames)->toContain('tenant', 'provider', 'consent_status', 'verification_status', 'is_enabled', 'default_only');
    expect($filterNames)->not->toContain('status', 'health_status');
    expect($filters['is_enabled']->getLabel())->toBe('Lifecycle');

    $component
        ->set('tableFilters.default_only.isActive', true)
        ->assertCanSeeTableRecords([$defaultConnection])
        ->assertCanNotSeeTableRecords([$nonDefaultConnection]);
});
