<?php

declare(strict_types=1);

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

it('hydrates an explicit prior history file into the canonical lane artifact path', function (): void {
    $sourceArtifactDirectory = TestLaneTrendFixtures::artifactDirectory('trend-history-hydration/source');
    $targetArtifactDirectory = TestLaneTrendFixtures::artifactDirectory('trend-history-hydration/target');
    $report = TestLaneTrendFixtures::buildReport(
        laneId: 'fast-feedback',
        wallClockSeconds: 182.4,
        durationsByFile: [
            'tests/Feature/Guards/TestLaneHistoryHydrationContractTest.php' => 14.2,
            'tests/Feature/Guards/TestLaneArtifactsContractTest.php' => 9.8,
        ],
        artifactDirectory: $sourceArtifactDirectory,
        ciContext: [
            'workflowId' => 'pr-fast-feedback',
            'triggerClass' => 'pull-request',
            'entryPointResolved' => true,
            'workflowLaneMatched' => true,
        ],
        comparisonProfile: 'shared-test-fixture-slimming',
    );

    $sourcePath = TestLaneManifest::absolutePath($sourceArtifactDirectory.'/seeded-fast-feedback.trend-history.json');

    if (! is_dir(dirname($sourcePath))) {
        mkdir(dirname($sourcePath), 0777, true);
    }

    file_put_contents($sourcePath, json_encode($report['trendHistoryArtifact'], JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));

    $result = TestLaneReport::hydrateTrendHistory(
        laneId: 'fast-feedback',
        historyFile: $sourcePath,
        artifactDirectory: $targetArtifactDirectory,
    );

    $hydratedArtifact = TestLaneTrendFixtures::readTrendHistory('fast-feedback', $targetArtifactDirectory);

    expect($result['hydrated'])->toBeTrue()
        ->and($result['sourceType'])->toBe('history-file')
        ->and($hydratedArtifact['laneId'])->toBe('fast-feedback')
        ->and($hydratedArtifact['workflowProfile'])->toBe('pr-fast-feedback')
        ->and($hydratedArtifact['history'][0]['artifactRefs']['trendHistory'])->toBe($sourceArtifactDirectory.'/fast-feedback-latest.trend-history.json');
});

it('hydrates staged bundle directories and zip bundles using the canonical trend-history artifact name', function (): void {
    $sourceArtifactDirectory = TestLaneTrendFixtures::artifactDirectory('trend-history-hydration/bundle-source');
    $bundleDirectory = TestLaneManifest::absolutePath($sourceArtifactDirectory.'/bundle');
    $targetArtifactDirectory = TestLaneTrendFixtures::artifactDirectory('trend-history-hydration/bundle-target');
    $report = TestLaneTrendFixtures::buildReport(
        laneId: 'confidence',
        wallClockSeconds: 431.2,
        durationsByFile: [
            'tests/Feature/Baselines/BaselineCompareMatrixCompareAllActionTest.php' => 24.7,
            'tests/Feature/Baselines/BaselineCompareMatrixBuilderTest.php' => 20.4,
        ],
        artifactDirectory: $sourceArtifactDirectory,
        ciContext: [
            'workflowId' => 'main-confidence',
            'triggerClass' => 'mainline-push',
            'entryPointResolved' => true,
            'workflowLaneMatched' => true,
        ],
    );

    if (! is_dir($bundleDirectory)) {
        mkdir($bundleDirectory, 0777, true);
    }

    file_put_contents(
        $bundleDirectory.'/confidence.trend-history.json',
        json_encode($report['trendHistoryArtifact'], JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR),
    );

    $directoryResult = TestLaneReport::hydrateTrendHistory(
        laneId: 'confidence',
        bundlePath: $bundleDirectory,
        artifactDirectory: $targetArtifactDirectory,
    );

    $zipPath = $bundleDirectory.'/confidence-artifacts.zip';
    $zip = new ZipArchive();
    $zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE);
    $zip->addFile($bundleDirectory.'/confidence.trend-history.json', 'confidence.trend-history.json');
    $zip->close();

    $zipResult = TestLaneReport::hydrateTrendHistory(
        laneId: 'confidence',
        bundlePath: $zipPath,
        artifactDirectory: $targetArtifactDirectory,
    );

    $hydratedArtifact = TestLaneTrendFixtures::readTrendHistory('confidence', $targetArtifactDirectory);

    expect($directoryResult['hydrated'])->toBeTrue()
        ->and($directoryResult['sourceType'])->toBe('bundle-directory')
        ->and($zipResult['hydrated'])->toBeTrue()
        ->and($zipResult['sourceType'])->toBe('bundle-zip')
        ->and($hydratedArtifact['laneId'])->toBe('confidence')
        ->and($hydratedArtifact['history'][0]['workflowId'])->toBe('main-confidence');
});
