<?php

declare(strict_types=1);

use App\Filament\Resources\ManagedEnvironmentResource;
use App\Models\ProviderConnection;
use App\Support\Providers\ProviderConsentStatus;
use App\Support\Providers\ProviderVerificationStatus;

it('renders provider health context on the canonical environment dashboard', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');

    $this->actingAs($user)
        ->get(ManagedEnvironmentResource::getUrl('view', ['record' => $tenant], tenant: $tenant))
        ->assertOk()
        ->assertSee('Environment governance overview')
        ->assertSee('Provider Health');
});

it('keeps provider health context when the environment needs a default microsoft connection', function (): void {
    $tenant = \App\Models\ManagedEnvironment::factory()->active()->create();
    [$user, $tenant] = createUserWithTenant(
        tenant: $tenant,
        role: 'owner',
        ensureDefaultMicrosoftProviderConnection: false,
    );

    \App\Models\ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
        'provider' => 'microsoft',
        'display_name' => 'Fallback Connection',
        'is_default' => false,
    ]);

    $this->actingAs($user)
        ->get(ManagedEnvironmentResource::getUrl('view', ['record' => $tenant], tenant: $tenant))
        ->assertOk()
        ->assertSee('Environment governance overview')
        ->assertSee('Provider Health');
});

it('renders the environment dashboard provider health summary from the default connection', function (): void {
    $tenant = \App\Models\ManagedEnvironment::factory()->active()->create();
    [$user, $tenant] = createUserWithTenant(
        tenant: $tenant,
        role: 'owner',
        ensureDefaultMicrosoftProviderConnection: false,
    );

    ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
        'provider' => 'microsoft',
        'display_name' => 'Canonical Summary Connection',
        'is_default' => true,
        'is_enabled' => false,
        'consent_status' => ProviderConsentStatus::Granted->value,
        'verification_status' => ProviderVerificationStatus::Healthy->value,
    ]);

    $this->actingAs($user)
        ->get(ManagedEnvironmentResource::getUrl('view', ['record' => $tenant], tenant: $tenant))
        ->assertOk()
        ->assertSee('Provider Health')
        ->assertSee('Canonical Summary Connection')
        ->assertSee('Disabled')
        ->assertSee('Healthy')
        ->assertDontSee('Connected')
        ->assertDontSee('Legacy health');
});
