<?php

declare(strict_types=1);

use App\Models\OperationRun;
use App\Models\ManagedEnvironment;
use App\Support\OperationRunLinks;
use App\Support\OpsUx\OperationRunUrl;
use App\Support\System\SystemOperationRunLinks;
use Illuminate\Support\Facades\File;

it('routes all OperationRun view links through OperationRunLinks', function (): void {
    $files = File::allFiles(app_path());

    $violations = [];

    foreach ($files as $file) {
        $path = $file->getRealPath();
        if (! is_string($path)) {
            continue;
        }

        // OperationRunLinks is the canonical wrapper.
        if (str_ends_with($path, '/Support/OperationRunLinks.php')) {
            continue;
        }

        $contents = File::get($path);

        if (preg_match("/\\bOperationRunResource::getUrl\(\\s*'view'/", $contents) === 1
            || preg_match("/route\(\s*'filament\.admin\.resources\.operations\.view'/", $contents) === 1) {
            $violations[] = $path;
        }
    }

    expect($violations)->toBeEmpty();
})->group('ops-ux');

it('resolves tenantless operation run links to the canonical admin.operations.view route', function (): void {
    $run = OperationRun::factory()->create();

    $expectedUrl = route('admin.operations.view', [
        'workspace' => (int) $run->workspace_id,
        'run' => (int) $run->getKey(),
    ]);

    expect(OperationRunLinks::tenantlessView($run))->toBe($expectedUrl);
    expect(OperationRunLinks::tenantlessView((int) $run->getKey()))->toBe($expectedUrl);
})->group('ops-ux');

it('normalizes tenant-scoped callers onto the canonical tenantless run route', function (): void {
    $tenant = ManagedEnvironment::factory()->create();
    $run = OperationRun::factory()->for($tenant)->create();

    $expectedUrl = route('admin.operations.view', [
        'workspace' => (int) $run->workspace_id,
        'run' => (int) $run->getKey(),
    ]);

    expect(OperationRunLinks::view($run, $tenant))->toBe($expectedUrl)
        ->and(OperationRunLinks::view((int) $run->getKey(), $tenant))->toBe($expectedUrl);
})->group('ops-ux');

it('preserves tenant prefilter, requested tab, and problem class on canonical operations collection links', function (): void {
    $tenant = ManagedEnvironment::factory()->create();
    $workspaceId = (int) $tenant->workspace_id;

    expect(OperationRunLinks::index($tenant, activeTab: 'active'))
        ->toBe(route('admin.operations.index', [
            'workspace' => $workspaceId,
            'managed_environment_id' => (int) $tenant->getKey(),
            'activeTab' => 'active',
        ]))
        ->and(OperationRunLinks::index(
            $tenant,
            activeTab: OperationRun::PROBLEM_CLASS_ACTIVE_STALE_ATTENTION,
            problemClass: OperationRun::PROBLEM_CLASS_ACTIVE_STALE_ATTENTION,
        ))
        ->toBe(route('admin.operations.index', [
            'workspace' => $workspaceId,
            'managed_environment_id' => (int) $tenant->getKey(),
            'activeTab' => OperationRun::PROBLEM_CLASS_ACTIVE_STALE_ATTENTION,
            'problemClass' => OperationRun::PROBLEM_CLASS_ACTIVE_STALE_ATTENTION,
        ]))
        ->and(OperationRunLinks::index(
            $tenant,
            activeTab: OperationRun::PROBLEM_CLASS_TERMINAL_FOLLOW_UP,
            problemClass: OperationRun::PROBLEM_CLASS_TERMINAL_FOLLOW_UP,
        ))
        ->toBe(route('admin.operations.index', [
            'workspace' => $workspaceId,
            'managed_environment_id' => (int) $tenant->getKey(),
            'activeTab' => OperationRun::PROBLEM_CLASS_TERMINAL_FOLLOW_UP,
            'problemClass' => OperationRun::PROBLEM_CLASS_TERMINAL_FOLLOW_UP,
        ]));
})->group('ops-ux');

it('preserves helper-owned operation type filters on canonical operations collection links', function (): void {
    $tenant = ManagedEnvironment::factory()->create();

    expect(OperationRunLinks::index($tenant, operationType: 'inventory_sync'))
        ->toBe(route('admin.operations.index', [
            'workspace' => (int) $tenant->workspace_id,
            'managed_environment_id' => (int) $tenant->getKey(),
            'tableFilters' => [
                'type' => [
                    'value' => 'inventory.sync',
                ],
            ],
        ]));
})->group('ops-ux');

it('keeps the thin operation URL delegate on the canonical admin helpers', function (): void {
    $tenant = ManagedEnvironment::factory()->create();
    $run = OperationRun::factory()->for($tenant)->create();

    expect(OperationRunUrl::view($run, $tenant))
        ->toBe(OperationRunLinks::view($run, $tenant))
        ->and(OperationRunUrl::index($tenant))
        ->toBe(OperationRunLinks::index($tenant));
})->group('ops-ux');

it('resolves system operation links through the canonical system helper family', function (): void {
    $run = OperationRun::factory()->create();

    expect(SystemOperationRunLinks::index())
        ->toBe(\App\Filament\System\Pages\Ops\Runs::getUrl(panel: 'system'))
        ->and(SystemOperationRunLinks::view($run))
        ->toBe(\App\Filament\System\Pages\Ops\ViewRun::getUrl(['run' => (int) $run->getKey()], panel: 'system'))
        ->and(SystemOperationRunLinks::view((int) $run->getKey()))
        ->toBe(\App\Filament\System\Pages\Ops\ViewRun::getUrl(['run' => (int) $run->getKey()], panel: 'system'));
})->group('ops-ux');
