<?php

it('guards against empty order ids in net amount calculation', function (): void {
    $ordersFile = app_path('objects/orders_o.php');
    $content = file_get_contents($ordersFile);

    expect($content)->not->toBeFalse();
    expect($content)->toContain('public function getNetAmountForOrders(array $order_ids): array');
    expect($content)->toContain('if (empty($order_ids)) {');
    expect($content)->toContain('return [];');
});

it('guards against empty customer arrays in date-range transaction fetches', function (): void {
    $ordersFile = app_path('objects/orders_o.php');
    $content = file_get_contents($ordersFile);

    expect($content)->not->toBeFalse();
    expect($content)->toContain('public function getTransactionsForCustomersInDateRange(array $customers, string $dateFrom, string $dateTo): array');
    expect($content)->toContain('if (empty($customers)) {');
    expect($content)->toContain('return [];');
});

it('uses centralized duplicate filtering for possible duplicate detection', function (): void {
    $ordersFile = app_path('objects/orders_o.php');
    $content = file_get_contents($ordersFile);

    expect($content)->not->toBeFalse();
    expect($content)->toContain('invoicing_period_utils::filterPossibleDuplicates($orders, 86400)');
});

it('provides a batched plain-row transaction query for invoicing period responses', function (): void {
    $ordersFile = app_path('objects/orders_o.php');
    $content = file_get_contents($ordersFile);

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

    expect($content)->toContain('public function getPeriodTransactionsForCustomersInDateRange(?array $customers, string $dateFrom, string $dateTo): array')
        ->and($content)->toContain('COALESCE(SUM(CASE WHEN oi.include_in_invoice = 1 THEN oi.price * oi.quantity ELSE 0 END), 0) AS net_amount')
        ->and($content)->toContain('COALESCE(coi.booked_invoice_id, 0)')
        ->and($content)->toContain('COALESCE(emo.invoice_id, 0)')
        ->and($content)->toContain('department_flags.exclude_from_invoicing')
        ->and($content)->not->toContain('$order->select((int)$row[\'id\']);' . PHP_EOL . '            $transactions[$customerNumber][]');
});

it('adds guarded composite indexes for invoicing period lookups', function (): void {
    $schemaFile = app_path('classes/orders_schema_bootstrap.php');
    $content = file_get_contents($schemaFile);

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

    expect($content)->toContain("self::ensureIndex(\$db, 'orders', 'idx_orders_period_customer_created_deleted', 'customer_id, created_at, deleted_at')")
        ->and($content)->toContain("self::ensureIndex(\$db, 'orders', 'idx_orders_period_created_deleted_customer', 'created_at, deleted_at, customer_id')")
        ->and($content)->toContain("self::ensureIndex(\$db, 'order_items', 'idx_order_items_order_deleted', 'order_id, deleted_at')")
        ->and($content)->toContain("self::ensureIndex(\$db, 'customer_attributes', 'idx_customer_attributes_attribute_user', 'attribute, user_id')")
        ->and($content)->toContain('private static function indexExists(object $db, string $table, string $index): bool');
});
