<?php

declare(strict_types=1);

use Symfony\Component\Yaml\Yaml;
use Tests\Support\TestLaneTrendFixtures;

it('keeps the logical OpenAPI contract synchronized with the generated trend assessment vocabulary', function (): void {
    $contractPath = repo_path('specs/211-runtime-trend-recalibration/contracts/test-runtime-trend.logical.openapi.yaml');
    $artifactDirectory = TestLaneTrendFixtures::artifactDirectory('trend-logical-contract');
    $report = TestLaneTrendFixtures::buildReport(
        laneId: 'confidence',
        wallClockSeconds: 438.1,
        durationsByFile: [
            'tests/Feature/Baselines/BaselineCompareMatrixCompareAllActionTest.php' => 22.8,
            'tests/Feature/Baselines/BaselineCompareMatrixBuilderTest.php' => 19.4,
            'tests/Feature/Rbac/OnboardingWizardUiEnforcementTest.php' => 17.6,
        ],
        artifactDirectory: $artifactDirectory,
        ciContext: [
            'workflowId' => 'main-confidence',
            'triggerClass' => 'mainline-push',
            'entryPointResolved' => true,
            'workflowLaneMatched' => true,
        ],
    );

    /** @var array<string, mixed> $contract */
    $contract = Yaml::parseFile($contractPath);
    $paths = $contract['paths'] ?? [];
    $schemas = $contract['components']['schemas'] ?? [];
    $assessment = $report['trendCurrentAssessment'];
    $decision = $report['trendRecalibrationDecisions'][0] ?? null;

    expect(array_keys($paths))->toEqualCanonicalizing([
        '/test-governance/lanes/{laneId}/trend-history',
        '/test-governance/lanes/{laneId}/trend-assessment',
        '/test-governance/lanes/{laneId}/recalibration',
        '/test-governance/cycles/{cycleId}/summary',
    ])
        ->and($paths['/test-governance/lanes/{laneId}/trend-history']['post']['operationId'])->toBe('updateLaneTrendHistory')
        ->and($paths['/test-governance/lanes/{laneId}/trend-assessment']['post']['operationId'])->toBe('evaluateLaneTrendAssessment')
        ->and($paths['/test-governance/lanes/{laneId}/recalibration']['post']['operationId'])->toBe('evaluateLaneRecalibration')
        ->and($paths['/test-governance/cycles/{cycleId}/summary']['get']['operationId'])->toBe('getTrendSummaryCycle')
        ->and($schemas['DriftAssessment']['properties']['healthClass']['enum'])->toEqualCanonicalizing([
            'healthy',
            'budget-near',
            'trending-worse',
            'regressed',
            'unstable',
        ])
        ->and($schemas['DriftAssessment']['properties']['recalibrationRecommendation']['enum'])->toEqualCanonicalizing([
            'none',
            'investigate',
            'review-baseline',
            'review-budget',
        ])
        ->and($schemas['RecalibrationDecision']['properties']['decisionStatus']['enum'])->toEqualCanonicalizing([
            'candidate',
            'approved',
            'rejected',
        ])
        ->and($schemas['LaneTrendHistoryArtifact']['required'])->toContain('schemaVersion', 'policy', 'history', 'currentAssessment')
        ->and($assessment['healthClass'])->toBeIn($schemas['DriftAssessment']['properties']['healthClass']['enum'])
        ->and($assessment['recalibrationRecommendation'])->toBeIn($schemas['DriftAssessment']['properties']['recalibrationRecommendation']['enum']);

    if (is_array($decision)) {
        expect($decision['decisionStatus'])->toBeIn($schemas['RecalibrationDecision']['properties']['decisionStatus']['enum'])
            ->and($decision['targetType'])->toBeIn($schemas['RecalibrationDecision']['properties']['targetType']['enum']);
    }
});
