<?php

declare(strict_types=1);

use App\Services\Operations\OperationLifecyclePolicyValidator;
use Tests\Support\OpsUx\SourceFileScanner;
use Tests\Support\TestLaneManifest;

it('keeps ops ux governance retained in the heavy-governance inventory', function (): void {
    $inventoryRecord = collect(TestLaneManifest::heavyGovernanceHotspotInventory())
        ->firstWhere('familyId', 'ops-ux-governance');

    expect($inventoryRecord)->not->toBeNull()
        ->and($inventoryRecord['classificationId'])->toBe('surface-guard')
        ->and($inventoryRecord['status'])->toBe('retained');
});

it('keeps lifecycle bridge ownership and initiator-null notification discipline intact', function (): void {
    $validator = app(OperationLifecyclePolicyValidator::class);
    $result = $validator->validate();

    expect($result['valid'])->toBeTrue();

    $root = SourceFileScanner::projectRoot();
    $operationRunService = SourceFileScanner::read($root.'/app/Services/OperationRunService.php');

    expect($operationRunService)->toContain('if ($run->user instanceof User)')
        ->and($operationRunService)->toContain('OperationRunCompletedNotification')
        ->and($operationRunService)->toContain('bridgeFailedJobFailure');

    $directBridgeJobs = [
        $root.'/app/Jobs/CaptureBaselineSnapshotJob.php',
        $root.'/app/Jobs/CompareBaselineToTenantJob.php',
        $root.'/app/Jobs/RunInventorySyncJob.php',
        $root.'/app/Jobs/SyncPoliciesJob.php',
        $root.'/app/Jobs/BulkTenantSyncJob.php',
        $root.'/app/Jobs/BulkBackupSetRestoreJob.php',
        $root.'/app/Jobs/ComposeEnvironmentReviewJob.php',
    ];

    foreach ($directBridgeJobs as $jobPath) {
        expect(SourceFileScanner::read($jobPath))->toContain('BridgesFailedOperationRun');
    }
})->group('ops-ux');
