<?php

declare(strict_types=1);

use App\Filament\Widgets\Dashboard\BaselineCompareNow;
use App\Models\BaselineProfile;
use App\Models\BaselineTenantAssignment;
use App\Models\OperationRun;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

it('renders the tenant dashboard when a baseline assignment exists (regression: missing BaselineCompareRun model)', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');

    $profile = BaselineProfile::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'name' => 'Baseline A',
    ]);

    BaselineTenantAssignment::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'tenant_id' => (int) $tenant->getKey(),
        'baseline_profile_id' => (int) $profile->getKey(),
    ]);

    OperationRun::factory()->create([
        'tenant_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $tenant->workspace_id,
        'type' => 'baseline_compare',
        'status' => 'completed',
        'outcome' => 'succeeded',
        'initiator_name' => 'System',
        'context' => [
            'baseline_profile_id' => (int) $profile->getKey(),
        ],
        'completed_at' => now()->subDay(),
    ]);

    $this->actingAs($user);

    Filament::setCurrentPanel(Filament::getPanel('tenant'));
    Filament::setTenant($tenant, true);

    Livewire::test(BaselineCompareNow::class)
        ->assertSee('Baseline Governance')
        ->assertSee('Baseline A')
        ->assertSee('No open drift — baseline compliant');
});
