<?php

declare(strict_types=1);

use App\Models\OperationRun;
use App\Models\ManagedEnvironment;
use App\Support\Navigation\CanonicalNavigationContext;
use App\Support\OperationRunLinks;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('renders the operations landing as a quiet monitoring surface', function (): void {
    $tenant = ManagedEnvironment::factory()->create();
    [$user, $tenant] = createUserWithTenant($tenant, role: 'owner');

    OperationRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $tenant->workspace_id,
        'type' => 'inventory_sync',
    ]);

    Filament::setTenant($tenant, true);

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $tenant->workspace_id])
        ->get(route('admin.operations.index', ['workspace' => $tenant->workspace]))
        ->assertOk()
        ->assertSee('Monitoring landing')
        ->assertSee('Tabs, filters, and row inspection define the active work lane.')
        ->assertSee('Scope context')
        ->assertSee('Scope reset')
        ->assertSee('Inspect flow')
        ->assertSee(__('localization.shell.show_all_environments'));
});

it('surfaces canonical return context separately from the operations work lane', function (): void {
    $tenant = ManagedEnvironment::factory()->create();
    [$user, $tenant] = createUserWithTenant($tenant, role: 'owner');

    $context = new CanonicalNavigationContext(
        sourceSurface: 'backup_set.detail_section',
        canonicalRouteName: 'admin.operations.index',
        tenantId: (int) $tenant->getKey(),
        backLinkLabel: 'Back to backup set',
        backLinkUrl: '/admin/tenant/backup-sets/1',
    );

    Filament::setTenant($tenant, true);

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $tenant->workspace_id])
        ->get(OperationRunLinks::index($tenant, $context))
        ->assertOk()
        ->assertSee('Monitoring landing')
        ->assertSee('Return path')
        ->assertSee('Back to backup set')
        ->assertSee('/admin/tenant/backup-sets/1', false)
        ->assertSee('Inspect flow');
});