<?php

it('documents both synchronous fallback and queued response contracts for POST /collected-invoices/economic', function (): void {
    $content = file_get_contents(app_path('routes/orderInvoicesRoute.php'));

    expect($content)->not->toBeFalse();

    $content = (string)$content;
    $start = strpos($content, "\$this->post('/collected-invoices/economic'");
    $end = strpos($content, "\$this->get('/collected-invoices/economic/queue'");

    expect($start)->not->toBeFalse();
    expect($end)->not->toBeFalse();
    expect($end)->toBeGreaterThan($start);

    $endpointBlock = substr($content, (int)$start, (int)$end - (int)$start);

    expect($endpointBlock)->toContain("if (!\$this->isEconomicTransferQueueAvailable()) {");
    expect($endpointBlock)->toContain("'message' => 'Collected invoice export processed synchronously'");
    expect($endpointBlock)->toContain("'mode' => 'synchronous_fallback'");
    expect($endpointBlock)->toContain("'result' => \$result");
    expect($endpointBlock)->toContain("\$job_id = (int)(\$job['id'] ?? 0);");
    expect($endpointBlock)->toContain("'job_id' => \$job_id");
    expect($endpointBlock)->toContain("'job' => \$job");
    expect($endpointBlock)->toContain("], 202);");
    expect($endpointBlock)->toContain('exportCollectedInvoiceSynchronously');
});

it('documents both synchronous fallback and queued response contracts for POST /collected-invoices/stripe/book', function (): void {
    $content = file_get_contents(app_path('routes/orderInvoicesRoute.php'));

    expect($content)->not->toBeFalse();

    $content = (string)$content;
    $start = strpos($content, "\$this->post('/collected-invoices/stripe/book'");
    $end = strpos($content, "\$this->post('/collected-invoices/vehicle-subscriptions'");

    expect($start)->not->toBeFalse();
    expect($end)->not->toBeFalse();
    expect($end)->toBeGreaterThan($start);

    $endpointBlock = substr($content, (int)$start, (int)$end - (int)$start);

    expect($endpointBlock)->toContain("if (!\$this->isEconomicTransferQueueAvailable()) {");
    expect($endpointBlock)->toContain("'message' => 'Stripe collected invoice export processed synchronously'");
    expect($endpointBlock)->toContain("'mode' => 'synchronous_fallback'");
    expect($endpointBlock)->toContain("'result' => \$result");
    expect($endpointBlock)->toContain('economic_transfer_queue::TYPE_COLLECTED_INVOICE_EXPORT');
    expect($endpointBlock)->toContain("\$job_id = (int)(\$job['id'] ?? 0);");
    expect($endpointBlock)->toContain("'job_id' => \$job_id");
    expect($endpointBlock)->toContain("'job' => \$job");
    expect($endpointBlock)->toContain("'message' => 'Stripe collected invoice export queued'");
    expect($endpointBlock)->toContain("], 202);");
});
