<?php

it('supports synchronous fallback and queued processing for order draft export route', function (): void {
    $content = file_get_contents(app_path('routes/economicInvoiceRoute.php'));

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

    $start = strpos($content, "\$this->post('/economic/invoice/draft/export'");
    $end = strpos($content, "\$this->delete('/economic/invoice/draft/delete'");
    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("'mode' => 'synchronous_fallback'");
    expect($endpointBlock)->toContain("'result' => \$result");
    expect($endpointBlock)->toContain('economic_transfer_queue::TYPE_ORDER_DRAFT_EXPORT');
    expect($endpointBlock)->toContain("'job_id' => \$job_id");
    expect($endpointBlock)->toContain("], 202);");
});

it('supports synchronous fallback and queued processing for collected invoice draft-producing routes', function (): void {
    $content = file_get_contents(app_path('routes/orderInvoicesRoute.php'));

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

    $collectedStart = strpos($content, "\$this->post('/collected-invoices/economic'");
    $collectedEnd = strpos($content, "\$this->get('/collected-invoices/economic/queue'");
    expect($collectedStart)->not->toBeFalse();
    expect($collectedEnd)->not->toBeFalse();
    expect($collectedEnd)->toBeGreaterThan($collectedStart);
    $collectedBlock = substr($content, (int)$collectedStart, (int)$collectedEnd - (int)$collectedStart);

    expect($collectedBlock)->toContain("if (!\$this->isEconomicTransferQueueAvailable()) {");
    expect($collectedBlock)->toContain('exportCollectedInvoiceSynchronously');
    expect($collectedBlock)->toContain("'mode' => 'synchronous_fallback'");
    expect($collectedBlock)->toContain("'result' => \$result");
    expect($collectedBlock)->toContain('economic_transfer_queue::TYPE_COLLECTED_INVOICE_EXPORT');
    expect($collectedBlock)->toContain("'job_id' => \$job_id");
    expect($collectedBlock)->toContain("], 202);");

    $stripeStart = strpos($content, "\$this->post('/collected-invoices/stripe/book'");
    $stripeEnd = strpos($content, "\$this->post('/collected-invoices/vehicle-subscriptions'");
    expect($stripeStart)->not->toBeFalse();
    expect($stripeEnd)->not->toBeFalse();
    expect($stripeEnd)->toBeGreaterThan($stripeStart);
    $stripeBlock = substr($content, (int)$stripeStart, (int)$stripeEnd - (int)$stripeStart);

    expect($stripeBlock)->toContain("if (!\$this->isEconomicTransferQueueAvailable()) {");
    expect($stripeBlock)->toContain('exportCollectedInvoiceSynchronously');
    expect($stripeBlock)->toContain("'mode' => 'synchronous_fallback'");
    expect($stripeBlock)->toContain("'result' => \$result");
    expect($stripeBlock)->toContain('economic_transfer_queue::TYPE_COLLECTED_INVOICE_EXPORT');
    expect($stripeBlock)->toContain("'message' => 'Stripe collected invoice export queued'");
    expect($stripeBlock)->toContain("'job_id' => \$job_id");
    expect($stripeBlock)->toContain("], 202);");

    expect($content)->toContain('private function exportCollectedInvoiceSynchronously');
});
