<?php

declare(strict_types=1);

use Tests\Support\TestLaneManifest;
use Tests\Support\TestLaneReport;

it('keeps the shared fixture slimming pre-migration baselines recorded for the standard lanes', function (): void {
    $fastFeedback = TestLaneManifest::comparisonBaseline('shared-test-fixture-slimming', 'fast-feedback');
    $confidence = TestLaneManifest::comparisonBaseline('shared-test-fixture-slimming', 'confidence');

    expect($fastFeedback)->toMatchArray([
        'laneId' => 'fast-feedback',
        'wallClockSeconds' => 176.73623,
        'targetImprovementPercent' => 10,
        'maxRegressionPercent' => 5,
    ])
        ->and($confidence)->toMatchArray([
            'laneId' => 'confidence',
            'wallClockSeconds' => 394.383441,
            'targetImprovementPercent' => 10,
            'maxRegressionPercent' => 5,
        ]);
});

it('classifies lane-impact comparison status against the recorded fixture slimming baseline', function (): void {
    $improved = TestLaneReport::buildReport(
        laneId: 'fast-feedback',
        wallClockSeconds: 150.0,
        slowestEntries: [],
        durationsByFile: [],
        comparisonProfile: 'shared-test-fixture-slimming',
    );

    $stable = TestLaneReport::buildReport(
        laneId: 'fast-feedback',
        wallClockSeconds: 180.0,
        slowestEntries: [],
        durationsByFile: [],
        comparisonProfile: 'shared-test-fixture-slimming',
    );

    $regressed = TestLaneReport::buildReport(
        laneId: 'fast-feedback',
        wallClockSeconds: 190.0,
        slowestEntries: [],
        durationsByFile: [],
        comparisonProfile: 'shared-test-fixture-slimming',
    );

    expect(data_get($improved, 'sharedFixtureSlimmingComparison.status'))->toBe('improved')
        ->and(data_get($stable, 'sharedFixtureSlimmingComparison.status'))->toBe('stable')
        ->and(data_get($regressed, 'sharedFixtureSlimmingComparison.status'))->toBe('regressed');
});

it('defines lane, classification, and family budget targets for heavy-governance attribution', function (): void {
    $budgetTargets = collect(TestLaneManifest::budgetTargets());
    $contract = TestLaneManifest::heavyGovernanceBudgetContract();
    $laneBudgetTarget = $budgetTargets
        ->first(static fn (array $target): bool => $target['targetType'] === 'lane' && $target['targetId'] === 'heavy-governance');

    expect($laneBudgetTarget)->not->toBeNull()
        ->and($contract['summaryThresholdSeconds'])->toBe(300.0)
        ->and($contract['evaluationThresholdSeconds'])->toBe(200.0)
        ->and($contract['normalizedThresholdSeconds'])->toBeGreaterThanOrEqual(300.0)
        ->and($laneBudgetTarget['thresholdSeconds'])->toBe($contract['normalizedThresholdSeconds'])
        ->and($laneBudgetTarget['lifecycleState'])->toBe($contract['lifecycleState'])
        ->and($budgetTargets->contains(static fn (array $target): bool => $target['targetType'] === 'classification' && $target['targetId'] === 'surface-guard'))->toBeTrue()
        ->and($budgetTargets->contains(static fn (array $target): bool => $target['targetType'] === 'classification' && $target['targetId'] === 'discovery-heavy'))->toBeTrue()
        ->and($budgetTargets->contains(static fn (array $target): bool => $target['targetType'] === 'family' && $target['targetId'] === 'action-surface-contract'))->toBeTrue()
        ->and($budgetTargets->contains(static fn (array $target): bool => $target['targetType'] === 'family' && $target['targetId'] === 'ops-ux-governance'))->toBeTrue();
});

it('evaluates heavy-governance budgets against named class and family totals', function (): void {
    $currentRunContract = TestLaneManifest::heavyGovernanceBudgetContract(110.0);

    $durationsByFile = [
        'tests/Feature/Guards/ActionSurfaceContractTest.php' => 31.2,
        'tests/Feature/Filament/PolicyResourceAdminSearchParityTest.php' => 17.4,
        'tests/Feature/Filament/PolicyVersionAdminSearchParityTest.php' => 16.1,
        'tests/Feature/Filament/Alerts/AlertsKpiHeaderTest.php' => 9.8,
        'tests/Feature/Guards/OperationLifecycleOpsUxGuardTest.php' => 8.7,
    ];

    $slowestEntries = collect($durationsByFile)
        ->map(static fn (float $seconds, string $file): array => [
            'label' => $file.'::synthetic',
            'subject' => $file.'::synthetic',
            'filePath' => $file,
            'durationSeconds' => $seconds,
            'wallClockSeconds' => $seconds,
            'laneId' => 'heavy-governance',
        ])
        ->values()
        ->all();

    $report = TestLaneReport::buildReport(
        laneId: 'heavy-governance',
        wallClockSeconds: 110.0,
        slowestEntries: $slowestEntries,
        durationsByFile: $durationsByFile,
    );

    expect(collect($report['budgetEvaluations'])->pluck('targetType')->unique()->values()->all())
        ->toEqualCanonicalizing(['lane', 'classification', 'family'])
        ->and(collect($report['budgetEvaluations'])->contains(static fn (array $evaluation): bool => $evaluation['targetType'] === 'lane' && $evaluation['targetId'] === 'heavy-governance'))->toBeTrue()
        ->and(collect($report['budgetEvaluations'])->contains(static fn (array $evaluation): bool => $evaluation['targetType'] === 'classification' && $evaluation['targetId'] === 'surface-guard'))->toBeTrue()
        ->and(collect($report['budgetEvaluations'])->contains(static fn (array $evaluation): bool => $evaluation['targetType'] === 'family' && $evaluation['targetId'] === 'action-surface-contract'))->toBeTrue()
        ->and($report['budgetContract']['normalizedThresholdSeconds'])->toBe($currentRunContract['normalizedThresholdSeconds'])
        ->and($report['budgetOutcome']['decisionStatus'])->toBe($currentRunContract['decisionStatus']);
});