<?php

declare(strict_types=1);

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

it('renders create and edit flows with neutral target-scope labels', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner', ensureDefaultMicrosoftProviderConnection: false);

    $connection = ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
        'display_name' => 'Neutral connection',
        'entra_tenant_id' => '44444444-4444-4444-4444-444444444444',
    ]);

    $this->actingAs($user)
        ->get(ProviderConnectionResource::getUrl('create', ['environment_id' => (int) $tenant->getKey()], panel: 'admin'))
        ->assertOk()
        ->assertSee('Target scope ID')
        ->assertSee('Target scope')
        ->assertDontSee('Entra tenant ID');

    $this->actingAs($user)
        ->get(ProviderConnectionResource::getUrl('edit', ['record' => $connection, 'environment_id' => (int) $tenant->getKey()], panel: 'admin'))
        ->assertOk()
        ->assertSee('Target scope ID')
        ->assertSee('Target scope')
        ->assertDontSee('Entra tenant ID');
});

it('keeps list and detail surfaces default-visible around provider target scope consent and verification', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner', ensureDefaultMicrosoftProviderConnection: false);

    $connection = ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
        'display_name' => 'Scope-visible connection',
        'entra_tenant_id' => '55555555-5555-5555-5555-555555555555',
        'consent_status' => 'granted',
        'verification_status' => 'healthy',
    ]);

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

    $component = Livewire::actingAs($user)->test(ListProviderConnections::class);
    $table = $component->instance()->getTable();
    $visibleColumnNames = collect($table->getVisibleColumns())
        ->map(fn ($column): string => $column->getName())
        ->values()
        ->all();

    expect($visibleColumnNames)->toContain('provider', 'target_scope', 'consent_status', 'verification_status')
        ->and($visibleColumnNames)->not->toContain('entra_tenant_id')
        ->and($table->getColumn('target_scope')?->getLabel())->toBe('Target scope')
        ->and($table->getColumn('entra_tenant_id')?->getLabel())->toBe('Microsoft tenant ID');

    $this->actingAs($user)
        ->get(ProviderConnectionResource::getUrl('view', ['record' => $connection, 'environment_id' => (int) $tenant->getKey()], panel: 'admin'))
        ->assertOk()
        ->assertSee('Target scope')
        ->assertSee('Provider context')
        ->assertSee('Microsoft tenant ID')
        ->assertSee('Consent')
        ->assertSee('Verification')
        ->assertDontSee('Entra tenant ID');
});

it('uses neutral validation attributes when the create flow misses target-scope context', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner', ensureDefaultMicrosoftProviderConnection: false);

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

    Livewire::actingAs($user)
        ->test(CreateProviderConnection::class)
        ->fillForm([
            'display_name' => 'Missing target scope',
            'entra_tenant_id' => '',
            'is_default' => true,
        ])
        ->call('create')
        ->assertHasFormErrors(['entra_tenant_id' => 'required']);
});

it('blocks unsupported provider target-scope combinations before provider execution', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner', ensureDefaultMicrosoftProviderConnection: false);

    $connection = ProviderConnection::factory()->consentGranted()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
        'provider' => 'contoso',
        'display_name' => 'Unsupported provider connection',
        'entra_tenant_id' => '66666666-6666-6666-6666-666666666666',
        'is_enabled' => true,
    ]);

    $resolution = app(ProviderConnectionResolver::class)
        ->validateConnection($tenant, 'contoso', $connection->fresh(['tenant']));

    expect($user)->not->toBeNull()
        ->and($resolution->resolved)->toBeFalse()
        ->and($resolution->reasonCode)->toBe('provider_binding_unsupported')
        ->and($resolution->extensionReasonCode)->toBe('ext.connection_scope_unsupported')
        ->and($resolution->message)->toBe('This provider and target-scope combination is not supported.');
});
