<?php

declare(strict_types=1);

use App\Filament\Resources\ProviderConnectionResource;
use App\Filament\Resources\ProviderConnectionResource\Pages\ListProviderConnections;
use App\Models\ProviderConnection;
use App\Support\Providers\ProviderConsentStatus;
use App\Support\Providers\ProviderVerificationStatus;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

it('promotes consent and verification to the default-visible provider connection list axes', 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' => 'Contoso',
        'provider' => 'microsoft',
        'is_default' => true,
        'is_enabled' => true,
        'consent_status' => ProviderConsentStatus::Granted->value,
        'verification_status' => ProviderVerificationStatus::Degraded->value,
    ]);

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

    $component = Livewire::actingAs($user)
        ->test(ListProviderConnections::class)
        ->assertCanSeeTableRecords([$connection]);

    $table = $component->instance()->getTable();
    $visibleColumnNames = collect($table->getVisibleColumns())
        ->map(fn ($column): string => $column->getName())
        ->values()
        ->all();

    expect($visibleColumnNames)->toContain('is_enabled', 'consent_status', 'verification_status')
        ->and($visibleColumnNames)->not->toContain('status')
        ->and($visibleColumnNames)->not->toContain('health_status')
        ->and($table->getColumn('status'))->toBeNull()
        ->and($table->getColumn('health_status'))->toBeNull();
});

it('separates current state from diagnostics on the provider connection view page', 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' => 'Truthful Connection',
        'provider' => 'microsoft',
        'is_default' => true,
        'is_enabled' => true,
        'consent_status' => ProviderConsentStatus::Granted->value,
        'verification_status' => ProviderVerificationStatus::Degraded->value,
    ]);

    $this->actingAs($user)
        ->get(ProviderConnectionResource::getUrl('view', ['record' => $connection], tenant: $tenant))
        ->assertOk()
        ->assertSeeInOrder([
            'Current state',
            'Lifecycle',
            'Enabled',
            'Consent',
            'Granted',
            'Verification',
            'Degraded',
            'Diagnostics',
            'Last error reason',
        ]);
});

it('shows current consent and verification context before diagnostics on the provider connection edit page', 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' => 'Editable Truthful Connection',
        'provider' => 'microsoft',
        'is_default' => true,
        'is_enabled' => true,
        'consent_status' => ProviderConsentStatus::Granted->value,
        'verification_status' => ProviderVerificationStatus::Blocked->value,
    ]);

    $this->actingAs($user)
        ->get(ProviderConnectionResource::getUrl('edit', ['record' => $connection], tenant: $tenant))
        ->assertOk()
        ->assertSeeInOrder([
            'Current state',
            'Lifecycle',
            'Enabled',
            'Consent',
            'Granted',
            'Verification',
            'Blocked',
            'Diagnostics',
            'Last error reason',
        ]);
});

it('does not treat consented but unverified connections as ready across provider surfaces', 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' => 'Unknown Verification Connection',
        'provider' => 'microsoft',
        'is_default' => true,
        'is_enabled' => true,
        'consent_status' => ProviderConsentStatus::Granted->value,
        'verification_status' => ProviderVerificationStatus::Unknown->value,
    ]);

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

    Livewire::actingAs($user)
        ->test(ListProviderConnections::class)
        ->assertCanSeeTableRecords([$connection])
        ->assertSee('Granted')
        ->assertSee('Unknown')
        ->assertDontSee('Ready');

    $this->get(ProviderConnectionResource::getUrl('view', ['record' => $connection], tenant: $tenant))
        ->assertOk()
        ->assertSee('Granted')
        ->assertSee('Unknown')
        ->assertDontSee('Ready');
});

it('shows lifecycle independently from consent and verification when a connection is disabled', 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' => 'Disabled But Consented',
        'provider' => 'microsoft',
        'is_default' => true,
        'is_enabled' => false,
        'consent_status' => ProviderConsentStatus::Granted->value,
        'verification_status' => ProviderVerificationStatus::Healthy->value,
    ]);

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

    Livewire::actingAs($user)
        ->test(ListProviderConnections::class)
        ->assertCanSeeTableRecords([$connection])
        ->assertSee('Disabled')
        ->assertSee('Granted')
        ->assertSee('Healthy')
        ->assertDontSee('Connected');

    $this->get(ProviderConnectionResource::getUrl('view', ['record' => $connection], tenant: $tenant))
        ->assertOk()
        ->assertSee('Disabled')
        ->assertSee('Granted')
        ->assertSee('Healthy')
        ->assertDontSee('Connected');
});
