<?php

declare(strict_types=1);

use Tests\Support\TestLaneTrendFixtures;

function seededTrendHistoryArtifact(array $baseArtifact, array $seededWallClockSeconds): array
{
    $templateRecord = $baseArtifact['history'][0];
    $seededHistory = [];

    foreach ($seededWallClockSeconds as $index => $seconds) {
        $record = $templateRecord;
        $record['runRef'] = sprintf('%s-seeded-%d', $templateRecord['laneId'], $index + 1);
        $record['generatedAt'] = sprintf('2026-04-%02dT12:00:00+00:00', $index + 1);
        $record['wallClockSeconds'] = round((float) $seconds, 6);
        $seededHistory[] = $record;
    }

    $baseArtifact['history'] = $seededHistory;

    return $baseArtifact;
}

it('keeps the trend summary bounded while exposing current, previous, baseline, and budget fields for fast-feedback and confidence', function (): void {
    $scenarios = [
        'fast-feedback' => [
            'artifactDirectory' => TestLaneTrendFixtures::artifactDirectory('trend-summary/fast-feedback'),
            'workflowId' => 'pr-fast-feedback',
            'triggerClass' => 'pull-request',
            'comparisonProfile' => 'shared-test-fixture-slimming',
            'durationsByFile' => [
                'tests/Feature/Guards/TestLaneTrendSummaryContractTest.php' => 12.6,
                'tests/Feature/Guards/TestLaneArtifactsContractTest.php' => 8.4,
            ],
            'seededHistory' => [176.73, 178.91, 181.22, 183.41, 185.07, 186.64],
            'currentSeconds' => 188.18,
        ],
        'confidence' => [
            'artifactDirectory' => TestLaneTrendFixtures::artifactDirectory('trend-summary/confidence'),
            'workflowId' => 'main-confidence',
            'triggerClass' => 'mainline-push',
            'comparisonProfile' => 'shared-test-fixture-slimming',
            'durationsByFile' => [
                'tests/Feature/Baselines/BaselineCompareMatrixCompareAllActionTest.php' => 24.3,
                'tests/Feature/Baselines/BaselineCompareMatrixBuilderTest.php' => 20.4,
                'tests/Feature/Rbac/OnboardingWizardUiEnforcementTest.php' => 16.1,
            ],
            'seededHistory' => [394.38, 401.12, 407.05, 411.61, 419.24, 424.77],
            'currentSeconds' => 431.81,
        ],
    ];

    foreach ($scenarios as $laneId => $scenario) {
        $baseReport = TestLaneTrendFixtures::buildReport(
            laneId: $laneId,
            wallClockSeconds: $scenario['seededHistory'][0],
            durationsByFile: $scenario['durationsByFile'],
            artifactDirectory: $scenario['artifactDirectory'],
            ciContext: [
                'workflowId' => $scenario['workflowId'],
                'triggerClass' => $scenario['triggerClass'],
                'entryPointResolved' => true,
                'workflowLaneMatched' => true,
            ],
            comparisonProfile: $scenario['comparisonProfile'],
        );

        TestLaneTrendFixtures::writeTrendHistory(
            $laneId,
            seededTrendHistoryArtifact($baseReport['trendHistoryArtifact'], $scenario['seededHistory']),
            $scenario['artifactDirectory'],
        );

        $report = TestLaneTrendFixtures::buildReport(
            laneId: $laneId,
            wallClockSeconds: $scenario['currentSeconds'],
            durationsByFile: $scenario['durationsByFile'],
            artifactDirectory: $scenario['artifactDirectory'],
            ciContext: [
                'workflowId' => $scenario['workflowId'],
                'triggerClass' => $scenario['triggerClass'],
                'entryPointResolved' => true,
                'workflowLaneMatched' => true,
            ],
            comparisonProfile: $scenario['comparisonProfile'],
        );

        expect($report['trendHistoryArtifact']['history'])->toHaveCount(7)
            ->and($report['trendCurrentAssessment']['sampleCount'])->toBe(5)
            ->and($report['trendCurrentAssessment']['previousComparableRunRef'])->not->toBeNull()
            ->and($report['trendHistoryArtifact']['history'][0]['baselineSeconds'])->not->toBeNull()
            ->and($report['trendHistoryArtifact']['history'][0]['budgetSeconds'])->toEqual((float) $report['budgetThresholdSeconds'])
            ->and($report['trendCurrentAssessment']['summaryLine'])->not->toBe('')
            ->and($report['trendWarnings'])->toBeArray();
    }
});
