<?php

app_require('objects/economic_module_orders.php');

use objects\economic_module_orders;

final class EconomicModuleOrdersBatchHelpersTestDouble extends economic_module_orders
{
    /** @var int[] */
    public array $insertedOrderIds = [];
    /** @var int[] */
    public array $fetchedOrderIds = [];
    /** @var array<int, array<string, mixed>> */
    public array $rows = [];

    protected function performEnsureRowsInsert(array $orderIds): void
    {
        $this->insertedOrderIds = $orderIds;
    }

    protected function fetchRowsByOrderIds(array $orderIds): array
    {
        $this->fetchedOrderIds = $orderIds;
        return $this->rows;
    }
}

it('ensures economic module rows in one sanitized batch', function (): void {
    $object = new EconomicModuleOrdersBatchHelpersTestDouble();
    $object->ensureRowsForOrderIds([5, 0, -5, 3, 5, 2]);

    expect($object->insertedOrderIds)->toBe([2, 3, 5]);
});

it('returns id keyed economic module payload with null defaults', function (): void {
    $object = new EconomicModuleOrdersBatchHelpersTestDouble();
    $object->rows = [
        ['id' => 3, 'invoice_draft_id' => 17, 'invoice_id' => null],
        ['id' => 5, 'invoice_draft_id' => null, 'invoice_id' => 912],
    ];

    $result = $object->getByOrderIdsAsArray([5, 3, 7, 0, 5]);

    expect($object->fetchedOrderIds)->toBe([3, 5, 7]);
    expect($result[3])->toBe([
        'id' => 3,
        'invoice_draft_id' => 17,
        'invoice_id' => null,
    ]);
    expect($result[5])->toBe([
        'id' => 5,
        'invoice_draft_id' => null,
        'invoice_id' => 912,
    ]);
    expect($result[7])->toBe([
        'id' => 7,
        'invoice_draft_id' => null,
        'invoice_id' => null,
    ]);
});
