<?php

function bird_openapi_content_or_skip(): string
{
    $candidates = [
        dirname(__DIR__, 6) . DIRECTORY_SEPARATOR . 'openapi.yaml',
        dirname(__DIR__, 5) . DIRECTORY_SEPARATOR . 'openapi.yaml',
        dirname(__DIR__, 4) . DIRECTORY_SEPARATOR . 'openapi.yaml',
        dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'openapi.yaml',
        dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'openapi.yaml',
    ];

    foreach ($candidates as $candidate) {
        if (!is_file($candidate)) {
            continue;
        }

        $content = file_get_contents($candidate);
        if ($content !== false) {
            return $content;
        }
    }

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

function bird_openapi_path_block_or_fail(string $content, string $path): string
{
    $pathMarker = '  ' . $path . ':';
    $start = strpos($content, $pathMarker);
    if ($start === false) {
        throw new RuntimeException('OpenAPI path block not found: ' . $path);
    }

    $rest = substr($content, $start + strlen($pathMarker));
    $nextPath = strpos($rest, "\n  /");
    if ($nextPath === false) {
        return substr($content, $start);
    }

    return substr($content, $start, strlen($pathMarker) + $nextPath);
}

it('documents all Bird voice call parity endpoints including gather recordings insights and log', function (): void {
    $content = bird_openapi_content_or_skip();

    expect($content)->toContain('/bird/voice/calls:');
    expect($content)->toContain('/bird/voice/calls/log:');
    expect($content)->toContain('/bird/voice/calls/{id}:');
    expect($content)->toContain('/bird/voice/calls/{id}/answer:');
    expect($content)->toContain('/bird/voice/calls/{id}/ringing:');
    expect($content)->toContain('/bird/voice/calls/{id}/hangup:');
    expect($content)->toContain('/bird/voice/calls/{id}/playback:');
    expect($content)->toContain('/bird/voice/calls/{id}/say:');
    expect($content)->toContain('/bird/voice/calls/{id}/gather:');
    expect($content)->toContain('/bird/voice/calls/{id}/bridge:');
    expect($content)->toContain('/bird/voice/calls/{id}/record:');
    expect($content)->toContain('/bird/voice/calls/{id}/recordings:');
    expect($content)->toContain('/bird/voice/calls/{id}/recordings/{recordingId}:');
    expect($content)->toContain('/bird/voice/calls/{id}/insights:');
    expect($content)->toContain('/bird/voice/calls/test-outbound:');
    expect($content)->toContain('/bird/voice/calls/webhook/inbound:');
});

it('documents flash hangup endpoint and marks end alias as deprecated', function (): void {
    $content = bird_openapi_content_or_skip();
    $aliasPath = bird_openapi_path_block_or_fail($content, '/bird/voice/flash-calls/end');

    expect($content)->toContain('/bird/voice/flash-calls:');
    expect($content)->toContain('/bird/voice/flash-calls/{id}:');
    expect($content)->toContain('/bird/voice/flash-calls/hangup:');
    expect($content)->toContain('/bird/voice/flash-calls/end:');
    expect($aliasPath)->toContain('deprecated: true');
    expect($aliasPath)->toContain('birdEndFlashCallByNumbers');
});

it('defines request and response schemas for Bird call command recording insight log and flash payloads', function (): void {
    $content = bird_openapi_content_or_skip();
    $webhookPath = bird_openapi_path_block_or_fail($content, '/bird/voice/calls/webhook/inbound');

    expect($content)->toContain('BirdVoiceCallCommandResponse:');
    expect($content)->toContain('BirdVoiceCallBridgeResponse:');
    expect($content)->toContain('BirdVoiceCallRecording:');
    expect($content)->toContain('BirdVoiceCallRecordingListResponse:');
    expect($content)->toContain('BirdVoiceCallRecordingSingleResponse:');
    expect($content)->toContain('BirdVoiceCallInsightsResponse:');
    expect($content)->toContain('BirdVoiceCallsLogResponse:');
    expect($content)->toContain('BirdFlashCallHangupRequest:');
    expect($content)->toContain('BirdFlashCallHangupResponse:');

    expect($content)->toContain('BirdVoiceCallCreateRequest:');
    expect($content)->toContain('BirdVoiceCallUpdateRequest:');
    expect($content)->toContain('BirdVoiceCallGatherRequest:');
    expect($content)->toContain('BirdVoiceCallRecordingUpdateRequest:');
    expect($content)->toContain('BirdFlashCallCreateRequest:');
    expect($content)->toContain('BirdFlashCallEndRequest:');
    expect($content)->toContain('BirdTestOutboundCallRequest:');
    expect($content)->toContain('BirdInboundCallWebhookRequest:');
    expect($content)->toContain('BirdInboundCallWebhookFlowGatherResponse:');
    expect($content)->toContain('BirdInboundCallWebhookGatherAcceptedResponse:');
    expect($content)->toContain('BirdInboundCallWebhookActionResultResponse:');

    expect($webhookPath)->toContain('BirdInboundCallWebhookRequest');
    expect($webhookPath)->toContain('BirdInboundCallWebhookFlowGatherResponse');
    expect($webhookPath)->toContain('BirdInboundCallWebhookGatherAcceptedResponse');
    expect($webhookPath)->toContain('BirdInboundCallWebhookActionResultResponse');
    expect($webhookPath)->toContain("'202'");
    expect($webhookPath)->toContain("'400'");
});
