<?php

it('routes collected invoice draft line uploads through the multi-order batch endpoint', function (): void {
    $content = file_get_contents(dirname(__DIR__, 3) . '/objects/collected_order_invoices_o.php');

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

    $start = strpos($content, 'public function addInvoicesToDraft');
    $end = strpos($content, 'public function getLastEconomicTransferMetrics');
    expect($start)->not->toBeFalse();
    expect($end)->not->toBeFalse();
    expect($end)->toBeGreaterThan($start);

    $methodBlock = substr($content, (int)$start, (int)$end - (int)$start);
    expect($methodBlock)->toContain('$order_objects = [];')
        ->and($methodBlock)->toContain('$metrics = (new economic())->invoices->draft->add_orders($draft_id, $order_objects, $currency, 500, $use_itemized_discounts);')
        ->and($methodBlock)->toContain('...$metrics')
        ->and($methodBlock)->not->toContain('self::addInvoiceToDraft($order[\'id\'], true, $draft_id, $currency);');
});

it('keeps single-order draft uploads as a wrapper around the batch endpoint', function (): void {
    $content = file_get_contents(dirname(__DIR__, 3) . '/modules/economic/endpoints/invoices/draft/economic_invoices_draft_endpoint.php');

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

    $singleStart = strpos($content, 'public function add_order');
    $singleEnd = strpos($content, 'public function add_orders');
    expect($singleStart)->not->toBeFalse();
    expect($singleEnd)->not->toBeFalse();
    expect($singleEnd)->toBeGreaterThan($singleStart);

    $singleBlock = substr($content, (int)$singleStart, (int)$singleEnd - (int)$singleStart);
    expect($singleBlock)->toContain('$this->add_orders($invoiceDraftId, [$order], $currency);');

    $batchBlock = substr($content, (int)$singleEnd);
    expect($batchBlock)->toContain('$draftInvoice->flushLinesInBatches($line_batch_size);')
        ->and($batchBlock)->toContain('$draftInvoice->addOrderItemLines($order, $use_itemized_discounts);')
        ->and($batchBlock)->toContain("'orders_with_invoice_lines' => \$orders_with_invoice_lines");
});

it('selects itemized discount mode for collected invoice batch transfers', function (): void {
    $content = file_get_contents(dirname(__DIR__, 3) . '/objects/collected_order_invoices_o.php');

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

    expect($content)->toContain('resolveInvoiceLayoutNumber')
        ->and($content)->toContain('invoice_discount_layout')
        ->and($content)->toContain('hasDiscountedIncludedInvoiceItems')
        ->and($content)->toContain('orderItemHasBillableDiscount')
        ->and($content)->toContain('$metrics = (new economic())->invoices->draft->add_orders($draft_id, $order_objects, $currency, 500, $use_itemized_discounts);');
});

it('includes collected invoice batch transfer metrics in queue results when available', function (): void {
    $content = file_get_contents(dirname(__DIR__, 3) . '/classes/economic_transfer_executor.php');

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

    expect($content)->toContain('$transfer_metrics = $collected_order_invoices->getLastEconomicTransferMetrics();')
        ->and($content)->toContain("\$result['economic_transfer_metrics'] = \$transfer_metrics;");
});
