<?php

declare(strict_types=1);

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

it('keeps provider scope microsoft-only in the create flow and list filter options', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner', ensureDefaultMicrosoftProviderConnection: false);

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

    Livewire::test(CreateProviderConnection::class)
        ->fillForm([
            'display_name' => 'MVP Scope Connection',
            'entra_tenant_id' => (string) fake()->uuid(),
            'is_default' => true,
        ])
        ->call('create')
        ->assertHasNoFormErrors();

    $created = ProviderConnection::query()
        ->where('managed_environment_id', (int) $tenant->getKey())
        ->where('display_name', 'MVP Scope Connection')
        ->first();

    expect($created)->not->toBeNull();
    expect($created?->provider)->toBe('microsoft');
    expect($created?->is_enabled)->toBeTrue();
    expect($created?->consent_status?->value ?? $created?->consent_status)->toBe('required');
    expect($created?->verification_status?->value ?? $created?->verification_status)->toBe('unknown');

    $listComponent = Livewire::test(ListProviderConnections::class);
    $providerFilter = $listComponent->instance()->getTable()->getFilters()['provider'] ?? null;

    expect($providerFilter)->not->toBeNull();
    expect($providerFilter?->getOptions())->toBe(['microsoft' => 'Microsoft']);
});
