<?php

declare(strict_types=1);

use App\Filament\System\Pages\Directory\ViewWorkspace;
use App\Models\PlatformUser;
use App\Models\Workspace;
use App\Support\Auth\PlatformCapabilities;

pest()->browser()->timeout(20_000);

it('smokes system support-access request and end actions', function (): void {
    $workspace = Workspace::factory()->create([
        'name' => 'Browser Support Access Workspace',
    ]);

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

    auth('web')->logout();
    $this->flushSession();
    $this->actingAs($platformUser, 'platform');

    $page = visit(ViewWorkspace::getUrl(panel: 'system', parameters: ['workspace' => $workspace]));

    $page
        ->waitForText('Support access')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs()
        ->assertSee('No active support access')
        ->assertSee('Request support access')
        ->click('Request support access')
        ->waitForText('Support access is workspace-scoped, time-limited, and audit-backed.');

    $page->script(<<<'JS'
(() => {
    const field = (labelText) => {
        const label = Array.from(document.querySelectorAll('label')).find((element) => element.textContent?.replace('*', '').trim() === labelText);

        if (! label) {
            return null;
        }

        const targetId = label.getAttribute('for');

        if (targetId) {
            return document.getElementById(targetId);
        }

        return label.parentElement?.querySelector('input, textarea, select') ?? null;
    };

    const reason = field('Reason');
    const ttl = field('TTL');

    if (! reason || ! ttl) {
        return false;
    }

    reason.value = 'Browser smoke support access review.';
    reason.dispatchEvent(new Event('input', { bubbles: true }));
    reason.dispatchEvent(new Event('change', { bubbles: true }));

    ttl.value = '30';
    ttl.dispatchEvent(new Event('input', { bubbles: true }));
    ttl.dispatchEvent(new Event('change', { bubbles: true }));

    return true;
})();
JS);

    $page->script(<<<'JS'
(() => {
    const confirmButton = Array.from(document.querySelectorAll('button')).find((element) => element.textContent?.trim() === 'Confirm');

    confirmButton?.click();
})();
JS);

    $page
        ->waitForText('Browser smoke support access review.')
        ->assertSee('Audit trail review')
        ->assertSee('End support access')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs()
        ->click('End support access')
        ->waitForText('This immediately ends the active support-access grant for this workspace and records an audit event.');

    $page->script(<<<'JS'
(() => {
    const confirmButton = Array.from(document.querySelectorAll('button')).find((element) => element.textContent?.trim() === 'Confirm');

    confirmButton?.click();
})();
JS);

    $page
        ->waitForText('No active support access')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs();
});
