<?php

declare(strict_types=1);

use App\Filament\System\Pages\Dashboard;
use App\Filament\System\Widgets\ControlTowerKpis;
use App\Filament\System\Widgets\ControlTowerRecentFailures;
use App\Filament\System\Widgets\ControlTowerTopOffenders;
use App\Filament\System\Widgets\CustomerHealthKpis;
use App\Filament\System\Widgets\CustomerHealthTopWorkspaces;
use App\Filament\System\Widgets\ProductTelemetryKpis;
use App\Models\PlatformUser;
use App\Support\Auth\PlatformCapabilities;
use App\Support\SystemConsole\SystemConsoleWindow;
use Filament\Widgets\WidgetConfiguration;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

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

it('forbids system dashboard when platform.console.view is missing', function () {
    $platformUser = PlatformUser::factory()->create([
        'capabilities' => [
            PlatformCapabilities::ACCESS_SYSTEM_PANEL,
        ],
        'is_active' => true,
    ]);

    $this->actingAs($platformUser, 'platform')
        ->get('/system')
        ->assertForbidden();
});

it('defaults dashboard to the 24h window and allows switching window', function () {
    $platformUser = PlatformUser::factory()->create([
        'capabilities' => [
            PlatformCapabilities::ACCESS_SYSTEM_PANEL,
            PlatformCapabilities::CONSOLE_VIEW,
        ],
        'is_active' => true,
    ]);

    $this->actingAs($platformUser, 'platform')
        ->get('/system')
        ->assertSuccessful();

    Livewire::test(Dashboard::class)
        ->assertSet('window', SystemConsoleWindow::LastDay)
        ->callAction('set_window', data: [
            'window' => SystemConsoleWindow::LastWeek,
        ])
        ->assertSet('window', SystemConsoleWindow::LastWeek);
});

it('passes the selected window into all window-aware dashboard widgets', function () {
    $platformUser = PlatformUser::factory()->create([
        'capabilities' => [
            PlatformCapabilities::ACCESS_SYSTEM_PANEL,
            PlatformCapabilities::CONSOLE_VIEW,
        ],
        'is_active' => true,
    ]);

    $component = Livewire::actingAs($platformUser, 'platform')
        ->withQueryParams(['window' => SystemConsoleWindow::LastWeek])
        ->test(Dashboard::class)
        ->assertSet('window', SystemConsoleWindow::LastWeek);

    $widgets = $component->instance()->getWidgets();

    expect($widgets)->toHaveCount(7)
        ->and($widgets[1])->toBeInstanceOf(WidgetConfiguration::class)
        ->and($widgets[2])->toBeInstanceOf(WidgetConfiguration::class)
        ->and($widgets[3])->toBeInstanceOf(WidgetConfiguration::class)
        ->and($widgets[4])->toBeInstanceOf(WidgetConfiguration::class)
        ->and($widgets[5])->toBeInstanceOf(WidgetConfiguration::class)
        ->and($widgets[6])->toBeInstanceOf(WidgetConfiguration::class)
        ->and($widgets[1]->widget)->toBe(CustomerHealthKpis::class)
        ->and($widgets[2]->widget)->toBe(CustomerHealthTopWorkspaces::class)
        ->and($widgets[3]->widget)->toBe(ControlTowerKpis::class)
        ->and($widgets[4]->widget)->toBe(ProductTelemetryKpis::class)
        ->and($widgets[5]->widget)->toBe(ControlTowerTopOffenders::class)
        ->and($widgets[6]->widget)->toBe(ControlTowerRecentFailures::class)
        ->and($widgets[1]->getProperties())->toBe(['window' => SystemConsoleWindow::LastWeek])
        ->and($widgets[2]->getProperties())->toBe(['window' => SystemConsoleWindow::LastWeek])
        ->and($widgets[3]->getProperties())->toBe(['window' => SystemConsoleWindow::LastWeek])
        ->and($widgets[4]->getProperties())->toBe(['window' => SystemConsoleWindow::LastWeek])
        ->and($widgets[5]->getProperties())->toBe(['window' => SystemConsoleWindow::LastWeek])
        ->and($widgets[6]->getProperties())->toBe(['window' => SystemConsoleWindow::LastWeek]);
});
