<?php

declare(strict_types=1);

use Tests\Support\TestLaneBudget;
use Tests\Support\TestLaneReport;

it('classifies wrapper or manifest drift before lower-level lane failures', function (): void {
    $primaryFailureClassId = TestLaneReport::classifyPrimaryFailure(
        exitCode: 1,
        artifactPublicationStatus: ['complete' => true],
        budgetOutcome: ['budgetStatus' => 'over-budget'],
        entryPointResolved: false,
        workflowLaneMatched: false,
    );

    expect($primaryFailureClassId)->toBe('wrapper-failure');
});

it('keeps confidence budget overruns visible without converting them into blocking failures', function (): void {
    $budgetOutcome = TestLaneBudget::evaluateLaneForTrigger('confidence', 'mainline-push', 481.0);
    $summary = TestLaneReport::buildCiSummary(
        report: [
            'laneId' => 'confidence',
            'budgetStatus' => 'within-budget',
            'ciContext' => ['workflowId' => 'main-confidence'],
        ],
        exitCode: 0,
        budgetOutcome: $budgetOutcome,
        artifactPublicationStatus: ['complete' => true, 'publishedArtifacts' => []],
    );

    expect($budgetOutcome['budgetStatus'])->toBe('over-budget')
        ->and($summary['primaryFailureClassId'])->toBe('budget-breach')
        ->and($summary['blockingStatus'])->toBe('non-blocking-warning');
});

it('treats mature fast-feedback budget overruns as blocking when they exceed the CI tolerance', function (): void {
    $budgetOutcome = TestLaneBudget::evaluateLaneForTrigger('fast-feedback', 'pull-request', 216.0);
    $summary = TestLaneReport::buildCiSummary(
        report: [
            'laneId' => 'fast-feedback',
            'budgetStatus' => 'within-budget',
            'ciContext' => ['workflowId' => 'pr-fast-feedback'],
        ],
        exitCode: 0,
        budgetOutcome: $budgetOutcome,
        artifactPublicationStatus: ['complete' => true, 'publishedArtifacts' => []],
    );

    expect($budgetOutcome['budgetStatus'])->toBe('over-budget')
        ->and($summary['primaryFailureClassId'])->toBe('budget-breach')
        ->and($summary['blockingStatus'])->toBe('blocking');
});

it('classifies incomplete artifact bundles independently from test and budget status', function (): void {
    $summary = TestLaneReport::buildCiSummary(
        report: [
            'laneId' => 'fast-feedback',
            'budgetStatus' => 'within-budget',
            'ciContext' => ['workflowId' => 'pr-fast-feedback'],
        ],
        exitCode: 0,
        budgetOutcome: null,
        artifactPublicationStatus: [
            'complete' => false,
            'publishedArtifacts' => [],
        ],
    );

    expect($summary['primaryFailureClassId'])->toBe('artifact-publication-failure')
        ->and($summary['blockingStatus'])->toBe('blocking');
});

it('surfaces classification-only scope boundaries for heavy governance and browser reports', function (): void {
    $heavySummary = TestLaneReport::buildCiSummary(
        report: [
            'laneId' => 'heavy-governance',
            'budgetStatus' => 'within-budget',
            'ciContext' => ['workflowId' => 'heavy-governance-manual'],
        ],
        exitCode: 1,
        artifactPublicationStatus: ['complete' => true, 'publishedArtifacts' => []],
    );
    $browserSummary = TestLaneReport::buildCiSummary(
        report: [
            'laneId' => 'browser',
            'budgetStatus' => 'within-budget',
            'ciContext' => ['workflowId' => 'browser-manual'],
        ],
        exitCode: 1,
        artifactPublicationStatus: ['complete' => true, 'publishedArtifacts' => []],
    );

    expect($heavySummary['scopeBoundaryNote'])->toContain('full-suite repair ownership')
        ->and($browserSummary['scopeBoundaryNote'])->toContain('classification-only');
});