<?php

declare(strict_types=1);

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

it('surfaces top family and file hotspot deltas together with new and dropped hotspot detection', function (): void {
    $artifactDirectory = TestLaneTrendFixtures::artifactDirectory('trend-hotspots/available');
    $previousDurations = [
        'tests/Feature/Baselines/BaselineCompareMatrixCompareAllActionTest.php' => 24.0,
        'tests/Feature/Baselines/BaselineCompareMatrixBuilderTest.php' => 18.0,
        'tests/Feature/Rbac/OnboardingWizardUiEnforcementTest.php' => 12.0,
    ];
    $currentDurations = [
        'tests/Feature/Baselines/BaselineCompareMatrixCompareAllActionTest.php' => 41.0,
        'tests/Feature/Filament/BackupSetAdminTenantParityTest.php' => 15.0,
    ];
    $previousReport = TestLaneTrendFixtures::buildReport(
        laneId: 'confidence',
        wallClockSeconds: 424.0,
        durationsByFile: $previousDurations,
        artifactDirectory: $artifactDirectory,
        ciContext: [
            'workflowId' => 'main-confidence',
            'triggerClass' => 'mainline-push',
            'entryPointResolved' => true,
            'workflowLaneMatched' => true,
        ],
        comparisonProfile: 'shared-test-fixture-slimming',
    );

    TestLaneTrendFixtures::writeTrendHistory('confidence', $previousReport['trendHistoryArtifact'], $artifactDirectory);

    $report = TestLaneTrendFixtures::buildReport(
        laneId: 'confidence',
        wallClockSeconds: 438.0,
        durationsByFile: $currentDurations,
        artifactDirectory: $artifactDirectory,
        ciContext: [
            'workflowId' => 'main-confidence',
            'triggerClass' => 'mainline-push',
            'entryPointResolved' => true,
            'workflowLaneMatched' => true,
        ],
        comparisonProfile: 'shared-test-fixture-slimming',
    );

    $hotspotSnapshot = $report['trendHotspotSnapshot'];

    expect($hotspotSnapshot['evidenceAvailability'])->toBe('available')
        ->and($hotspotSnapshot['familyDeltas'])->not->toBeEmpty()
        ->and(collect($hotspotSnapshot['familyDeltas'])->pluck('name')->all())
        ->toContain('baseline-compare-matrix-workflow', 'backup-set-admin-tenant-parity')
        ->and(collect($hotspotSnapshot['fileHotspots'])->pluck('name')->all())
        ->toContain(
            'tests/Feature/Baselines/BaselineCompareMatrixCompareAllActionTest.php',
            'tests/Feature/Filament/BackupSetAdminTenantParityTest.php',
        )
        ->and($hotspotSnapshot['newEntrants'])->toContain('tests/Feature/Filament/BackupSetAdminTenantParityTest.php')
        ->and($hotspotSnapshot['droppedEntrants'])->toContain('tests/Feature/Rbac/OnboardingWizardUiEnforcementTest.php');
});

it('discloses unavailable hotspot evidence instead of silently omitting the hotspot section', function (): void {
    $artifactDirectory = TestLaneTrendFixtures::artifactDirectory('trend-hotspots/unavailable');
    $report = TestLaneTrendFixtures::buildReport(
        laneId: 'fast-feedback',
        wallClockSeconds: 189.0,
        durationsByFile: [],
        artifactDirectory: $artifactDirectory,
        ciContext: [
            'workflowId' => 'pr-fast-feedback',
            'triggerClass' => 'pull-request',
            'entryPointResolved' => true,
            'workflowLaneMatched' => true,
        ],
        comparisonProfile: 'shared-test-fixture-slimming',
    );

    TestLaneReport::writeArtifacts(
        laneId: 'fast-feedback',
        report: $report,
        artifactDirectory: $artifactDirectory,
    );

    $summary = (string) file_get_contents(TestLaneManifest::absolutePath(
        TestLaneReport::artifactPaths('fast-feedback', $artifactDirectory)['summary'],
    ));

    expect($report['trendHotspotSnapshot']['evidenceAvailability'])->toBe('unavailable')
        ->and($report['trendWarnings'])->toContain('Hotspot evidence is unavailable for this cycle.')
        ->and($summary)->toContain('Hotspot evidence: unavailable');
});
