<?php

app_require('classes/economic_transfer_executor.php');

use classes\economic_transfer_executor;

it('skips order items with zero quantity for e-conomic export', function (): void {
    $shouldSkip = economic_transfer_executor::shouldSkipOrderItemForInvoice([
        'quantity' => 0,
        'price' => 199,
    ], 0);

    expect($shouldSkip)->toBeTrue();
});

it('skips order items with zero price for e-conomic export', function (): void {
    $shouldSkip = economic_transfer_executor::shouldSkipOrderItemForInvoice([
        'quantity' => 2,
        'price' => 0,
    ], 2);

    expect($shouldSkip)->toBeTrue();
});

it('keeps positive quantity and price items billable for e-conomic export', function (): void {
    $shouldSkip = economic_transfer_executor::shouldSkipOrderItemForInvoice([
        'quantity' => 2,
        'price' => 149,
    ], 2);

    expect($shouldSkip)->toBeFalse();
});

it('skips malformed order-item payloads for e-conomic export safety', function (): void {
    expect(economic_transfer_executor::shouldSkipOrderItemForInvoice(null, 1))->toBeTrue();
    expect(economic_transfer_executor::shouldSkipOrderItemForInvoice('invalid', 1))->toBeTrue();
    expect(economic_transfer_executor::shouldSkipOrderItemForInvoice([
        'quantity' => 1,
    ], 1))->toBeTrue();
    expect(economic_transfer_executor::shouldSkipOrderItemForInvoice([
        'quantity' => 1,
        'price' => 'abc',
    ], 1))->toBeTrue();
});

it('wires zero-cost and zero-quantity skip guard into transfer line builder', function (): void {
    $content = file_get_contents(app_path('classes/economic_transfer_executor.php'));

    expect($content)->not->toBeFalse();
    expect($content)->toContain('if (self::shouldSkipOrderItemForInvoice($order_item, $quantity))');
    expect($content)->toContain('$economic_module_orders = (new economic_module_orders())->getByOrderId($order_id);');
    expect($content)->toContain("throw new Exception('An invoice has already been created, invoice ID: ' . \$invoice_id);");
    expect($content)->toContain("throw new Exception('No billable order items found');");
    expect($content)->toContain("throw new Exception('Order item is missing economic product id');");
});
