<?php

function economic_v2_openapi_content_or_skip(): string
{
    $candidates = [];
    for ($depth = 1; $depth <= 8; $depth++) {
        $candidates[] = dirname(__DIR__, $depth) . DIRECTORY_SEPARATOR . 'openapi.yaml';
    }

    $cwd = getcwd();
    if (is_string($cwd) && $cwd !== '') {
        $candidates[] = $cwd . DIRECTORY_SEPARATOR . 'openapi.yaml';
        $candidates[] = dirname($cwd) . DIRECTORY_SEPARATOR . 'openapi.yaml';
    }

    $candidates = array_values(array_unique($candidates));

    foreach ($candidates as $candidate) {
        if (is_file($candidate)) {
            $content = file_get_contents($candidate);
            if ($content !== false) {
                return $content;
            }
        }
    }

    test()->markTestSkipped('openapi.yaml is not available in this runtime environment.');
}

it('documents economic v2 invoice paths in openapi', function (): void {
    $content = economic_v2_openapi_content_or_skip();

    expect($content)->not->toBeFalse();
    expect($content)->toContain('/collected-invoices/economic/pdf:');
    expect($content)->toContain('/collected-invoices/economic/v2/details:');
    expect($content)->toContain('/collected-invoices/economic/v2/compare:');
    expect($content)->toContain('/collected-invoices/economic/v2/compare/bulk:');
    expect($content)->toContain('/collected-invoices/economic/v2/revenue-statistics:');
});

it('documents v2 historical distribution and pricing history paths in openapi', function (): void {
    $content = economic_v2_openapi_content_or_skip();

    expect($content)->not->toBeFalse();
    expect($content)->toContain('/superuser/invoicing/period/distribution/v2/all:');
    expect($content)->toContain('/superuser/invoicing/period/distribution/v2/fixed-pricing:');
    expect($content)->toContain('/superuser/invoicing/period/distribution/v2/wash-subscriptions:');
    expect($content)->toContain('/superuser/invoicing/period/distribution/v2/customer-prices:');
    expect($content)->toContain('/superuser/invoicing/period/distribution/v2/booked-department-75:');
    expect($content)->toContain('/superuser/customers/pricing-history:');
});

it('aligns legacy compare schema with runtime payload by removing stale required order_ids', function (): void {
    $content = economic_v2_openapi_content_or_skip();

    expect($content)->not->toBeFalse();
    $start = strpos($content, 'CollectedInvoiceEconomicCompareResponse:');
    $end = strpos($content, 'CollectedInvoiceEconomicV2DetailsResponse:');
    expect($start)->not->toBeFalse();
    expect($end)->not->toBeFalse();

    $legacyBlock = substr($content, $start, $end - $start);
    expect($legacyBlock)->toContain('- internal_total');
    expect($legacyBlock)->not->toContain('order_ids:');
    expect($legacyBlock)->not->toContain('- order_ids');
});

it('defines new reusable v2 schemas for normalization comparison versioning and distribution', function (): void {
    $content = economic_v2_openapi_content_or_skip();

    expect($content)->not->toBeFalse();
    expect($content)->toContain('EconomicV2NormalizedLineItem:');
    expect($content)->toContain('CollectedInvoiceEconomicPdfResponse:');
    expect($content)->toContain('EconomicV2Comparison:');
    expect($content)->toContain('CollectedInvoiceEconomicV2CustomerSummary:');
    expect($content)->toContain('CollectedInvoiceEconomicV2RevenueStatisticsResponse:');
    expect($content)->toContain('EconomicV2RevenueSummary:');
    expect($content)->toContain('PricingHistoryVersionEntry:');
    expect($content)->toContain('InvoicingDistributionV2AllResponse:');
    expect($content)->toContain('InvoicingDistributionV2BookedDepartment75Response:');
});
