<?php

declare(strict_types=1);

use App\Filament\Pages\Operations\TenantlessOperationRunViewer;
use App\Filament\Pages\EnvironmentDashboard;
use App\Filament\System\Widgets\ProductTelemetryKpis;
use App\Models\AuditLog;
use App\Models\OperationRun;
use App\Models\PlatformUser;
use App\Models\ProductUsageEvent;
use App\Models\ManagedEnvironment;
use App\Support\Auth\PlatformCapabilities;
use App\Support\OperationRunOutcome;
use App\Support\OperationRunStatus;
use App\Support\OperationRunType;
use App\Support\ProductTelemetry\ProductUsageEventCatalog;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    Filament::setCurrentPanel('system');
    Filament::bootCurrentPanel();
});

it('does not emit telemetry or audit rows on passive dashboard and detail renders', function (): void {
    $tenant = ManagedEnvironment::factory()->create();
    [$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'operator');

    $run = OperationRun::factory()->forTenant($tenant)->create([
        'user_id' => (int) $user->getKey(),
        'initiator_name' => $user->name,
        'type' => OperationRunType::ReviewPackGenerate->value,
        'status' => OperationRunStatus::Completed->value,
        'outcome' => OperationRunOutcome::Succeeded->value,
        'completed_at' => now(),
    ]);

    $platformUser = PlatformUser::factory()->create([
        'capabilities' => [
            PlatformCapabilities::ACCESS_SYSTEM_PANEL,
            PlatformCapabilities::CONSOLE_VIEW,
        ],
        'is_active' => true,
    ]);

    test()->actingAs($user);
    session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);
    setAdminEnvironmentContext($tenant);

    Livewire::actingAs($user)->test(EnvironmentDashboard::class);

    Filament::setTenant(null, true);
    session()->put(WorkspaceContext::SESSION_KEY, (int) $run->workspace_id);

    Livewire::actingAs($user)->test(TenantlessOperationRunViewer::class, ['run' => $run]);

    test()->actingAs($platformUser, 'platform');
    Livewire::test(ProductTelemetryKpis::class);

    expect(ProductUsageEvent::query()->count())->toBe(0)
        ->and(AuditLog::query()->count())->toBe(0);
});

it('keeps the system dashboard aggregate-only without exposing raw telemetry identifiers', function (): void {
    $tenant = ManagedEnvironment::factory()->create([
        'name' => 'NoLeak ManagedEnvironment Name',
    ]);

    ProductUsageEvent::factory()->forEvent(
        ProductUsageEventCatalog::REVIEW_PACK_REQUESTED,
        [
            'source_surface' => 'tenant',
            'include_operations' => true,
            'include_pii' => false,
        ],
    )->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $tenant->workspace_id,
        'subject_type' => 'review_pack',
        'subject_id' => 'review-pack-raw-424242',
        'occurred_at' => now()->subHour(),
    ]);

    $platformUser = PlatformUser::factory()->create([
        'capabilities' => [
            PlatformCapabilities::ACCESS_SYSTEM_PANEL,
            PlatformCapabilities::CONSOLE_VIEW,
        ],
        'is_active' => true,
    ]);

    $this->actingAs($platformUser, 'platform')
        ->get('/system?window=24h')
        ->assertSuccessful()
        ->assertSee('Product telemetry')
        ->assertSee('Review packs requested')
        ->assertDontSee('review-pack-raw-424242')
        ->assertDontSee('NoLeak ManagedEnvironment Name');
});