<?php

it('keeps queue guards on economic invoice queue status and retry routes', function (): void {
    $content = file_get_contents(app_path('routes/economicInvoiceRoute.php'));

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

    $guard_count = preg_match_all('/\$this->ensureEconomicTransferQueueIsAvailable\(\);/', (string)$content);
    $queue_init_count = preg_match_all('/new economic_transfer_queue\(\);/', (string)$content);
    expect($guard_count)->toBe(4);
    expect($queue_init_count)->toBe(6);

    $queue_endpoint_patterns = [
        "/\\\$this->get\\('\\/economic\\/invoice\\/draft\\/export\\/status'.*?\\\$this->ensureEconomicTransferQueueIsAvailable\\(\\);.*?new economic_transfer_queue\\(\\);/s",
        "/\\\$this->post\\('\\/economic\\/invoice\\/draft\\/export\\/retry'.*?\\\$this->ensureEconomicTransferQueueIsAvailable\\(\\);.*?new economic_transfer_queue\\(\\);/s",
        "/\\\$this->get\\('\\/economic\\/invoice\\/export\\/status'.*?\\\$this->ensureEconomicTransferQueueIsAvailable\\(\\);.*?new economic_transfer_queue\\(\\);/s",
        "/\\\$this->post\\('\\/economic\\/invoice\\/export\\/retry'.*?\\\$this->ensureEconomicTransferQueueIsAvailable\\(\\);.*?new economic_transfer_queue\\(\\);/s",
    ];

    foreach ($queue_endpoint_patterns as $pattern) {
        expect(preg_match($pattern, (string)$content))->toBe(1);
    }
});

it('supports synchronous fallback branches before queue enqueue on economic invoice export routes', function (): void {
    $content = file_get_contents(app_path('routes/economicInvoiceRoute.php'));

    expect($content)->not->toBeFalse();
    expect($content)->toContain("if (!\$this->isEconomicTransferQueueAvailable()) {");
    expect($content)->toContain("'mode' => 'synchronous_fallback'");
    expect($content)->toContain('ECONOMIC_INVOICE_DRAFT_EXPORT_FALLBACK');
    expect($content)->toContain('ECONOMIC_INVOICE_EXPORT_FALLBACK');

    $draft_start = strpos((string)$content, "\$this->post('/economic/invoice/draft/export'");
    $draft_end = strpos((string)$content, "\$this->delete('/economic/invoice/draft/delete'");
    expect($draft_start)->not->toBeFalse();
    expect($draft_end)->not->toBeFalse();
    expect($draft_end)->toBeGreaterThan($draft_start);
    $draft_block = substr((string)$content, (int)$draft_start, (int)$draft_end - (int)$draft_start);
    expect($draft_block)->toContain("if (!\$this->isEconomicTransferQueueAvailable()) {");
    expect($draft_block)->toContain('exportOrderDraftSynchronously');
    expect($draft_block)->toContain("'mode' => 'synchronous_fallback'");
    expect($draft_block)->toContain('new economic_transfer_queue();');

    $invoice_start = strpos((string)$content, "\$this->post('/economic/invoice/export'");
    $invoice_end = strpos((string)$content, "\$this->get('/economic/invoice/draft/export/status'");
    expect($invoice_start)->not->toBeFalse();
    expect($invoice_end)->not->toBeFalse();
    expect($invoice_end)->toBeGreaterThan($invoice_start);
    $invoice_block = substr((string)$content, (int)$invoice_start, (int)$invoice_end - (int)$invoice_start);
    expect($invoice_block)->toContain("if (!\$this->isEconomicTransferQueueAvailable()) {");
    expect($invoice_block)->toContain('exportOrderInvoiceSynchronously');
    expect($invoice_block)->toContain("'mode' => 'synchronous_fallback'");
    expect($invoice_block)->toContain('new economic_transfer_queue();');
});

it('keeps queue status endpoints guarded while collected invoice export routes support synchronous fallback', function (): void {
    $content = file_get_contents(app_path('routes/orderInvoicesRoute.php'));

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

    $guard_count = preg_match_all('/\$this->ensureEconomicTransferQueueIsAvailable\(\);/', (string)$content);
    $queue_init_count = preg_match_all('/new economic_transfer_queue\(\);/', (string)$content);
    expect($guard_count)->toBe(7);
    expect($queue_init_count)->toBe(9);

    $queue_endpoint_patterns = [
        "/\\\$this->get\\('\\/collected-invoices\\/economic\\/queue'.*?\\\$this->ensureEconomicTransferQueueIsAvailable\\(\\);.*?new economic_transfer_queue\\(\\);/s",
        "/\\\$this->get\\('\\/collected-invoices\\/economic\\/queue\\/monitor'.*?\\\$this->ensureEconomicTransferQueueIsAvailable\\(\\);.*?new economic_transfer_queue\\(\\);/s",
        "/\\\$this->get\\('\\/collected-invoices\\/economic\\/queue\\/status'.*?\\\$this->ensureEconomicTransferQueueIsAvailable\\(\\);.*?new economic_transfer_queue\\(\\);/s",
        "/\\\$this->post\\('\\/collected-invoices\\/economic\\/queue\\/dismiss'.*?\\\$this->ensureEconomicTransferQueueIsAvailable\\(\\);.*?new economic_transfer_queue\\(\\);/s",
        "/\\\$this->post\\('\\/collected-invoices\\/economic\\/queue\\/dismiss-terminal'.*?\\\$this->ensureEconomicTransferQueueIsAvailable\\(\\);.*?new economic_transfer_queue\\(\\);/s",
        "/\\\$this->post\\('\\/collected-invoices\\/economic\\/queue\\/retry'.*?\\\$this->ensureEconomicTransferQueueIsAvailable\\(\\);.*?new economic_transfer_queue\\(\\);/s",
        "/\\\$this->post\\('\\/collected-invoices\\/economic\\/queue\\/run'.*?\\\$this->ensureEconomicTransferQueueIsAvailable\\(\\);.*?new economic_transfer_queue\\(\\);/s",
    ];

    foreach ($queue_endpoint_patterns as $pattern) {
        expect(preg_match($pattern, (string)$content))->toBe(1);
    }

    $collected_start = strpos((string)$content, "\$this->post('/collected-invoices/economic'");
    $collected_end = strpos((string)$content, "\$this->get('/collected-invoices/economic/queue'");
    expect($collected_start)->not->toBeFalse();
    expect($collected_end)->not->toBeFalse();
    expect($collected_end)->toBeGreaterThan($collected_start);
    $collected_block = substr((string)$content, (int)$collected_start, (int)$collected_end - (int)$collected_start);
    expect($collected_block)->toContain("if (!\$this->isEconomicTransferQueueAvailable()) {");
    expect($collected_block)->toContain('exportCollectedInvoiceSynchronously');
    expect($collected_block)->toContain("'mode' => 'synchronous_fallback'");
    expect($collected_block)->toContain('new economic_transfer_queue();');

    $stripe_start = strpos((string)$content, "\$this->post('/collected-invoices/stripe/book'");
    $stripe_end = strpos((string)$content, "\$this->post('/collected-invoices/vehicle-subscriptions'");
    expect($stripe_start)->not->toBeFalse();
    expect($stripe_end)->not->toBeFalse();
    expect($stripe_end)->toBeGreaterThan($stripe_start);
    $stripe_block = substr((string)$content, (int)$stripe_start, (int)$stripe_end - (int)$stripe_start);
    expect($stripe_block)->toContain("if (!\$this->isEconomicTransferQueueAvailable()) {");
    expect($stripe_block)->toContain('exportCollectedInvoiceSynchronously');
    expect($stripe_block)->toContain("'mode' => 'synchronous_fallback'");
    expect($stripe_block)->toContain('new economic_transfer_queue();');

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

it('uses a consistent unavailable-service contract for queue status lifecycle endpoints', function (): void {
    $invoice_route_content = file_get_contents(app_path('routes/economicInvoiceRoute.php'));
    $order_invoices_route_content = file_get_contents(app_path('routes/orderInvoicesRoute.php'));

    expect($invoice_route_content)->not->toBeFalse();
    expect($order_invoices_route_content)->not->toBeFalse();

    expect($invoice_route_content)->toContain('private function isEconomicTransferQueueAvailable(): bool');
    expect($invoice_route_content)->toContain("class_exists('\\\\classes\\\\economic_transfer_executor')");
    expect($invoice_route_content)->toContain("class_exists('\\\\classes\\\\economic_transfer_queue_schema_bootstrap')");
    expect($invoice_route_content)->toContain('class_exists(economic_transfer_queue::class)');
    expect($invoice_route_content)->toContain("Economic transfer queue is unavailable in this deployment', 503");

    expect($order_invoices_route_content)->toContain('private function isEconomicTransferQueueAvailable(): bool');
    expect($order_invoices_route_content)->toContain("class_exists('\\\\classes\\\\economic_transfer_executor')");
    expect($order_invoices_route_content)->toContain("class_exists('\\\\classes\\\\economic_transfer_queue_schema_bootstrap')");
    expect($order_invoices_route_content)->toContain('class_exists(economic_transfer_queue::class)');
    expect($order_invoices_route_content)->toContain("Economic transfer queue is unavailable in this deployment', 503");
});
