<?php

declare(strict_types=1);

use App\Filament\System\Pages\Dashboard;
use App\Models\PlatformUser;
use App\Support\Auth\PlatformCapabilities;
use App\Support\SystemConsole\SystemConsoleWindow;
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);
});
