<?php

use helpers\xlvask_usage_log;
use objects\xlvask_usage_logs_o;

it('serializes empty ignore metadata as SQL null values for new usage logs', function (): void {
    $log = new xlvask_usage_log();
    $data = $log->toArray();

    expect($data)
        ->toHaveKey('ignored_at')
        ->toHaveKey('ignored_by')
        ->toHaveKey('ignored_reason')
        ->and($data['ignored_at'])->toBeNull()
        ->and($data['ignored_by'])->toBeNull()
        ->and($data['ignored_reason'])->toBeNull()
        ->and($data['Updated'])->toBe('');
});

it('accepts persisted ignore metadata from xlvask usage log rows', function (): void {
    $log = new xlvask_usage_log();

    $log->setProperties([
        'WashId' => 'wash-ignored-1',
        'CustomerId' => '35131752',
        'Customer' => 'BHS Logistics A/S',
        'VatNumber' => '35255156',
        'Location' => 'Aarhus C',
        'Hall' => 'AarhusC_1',
        'HallId' => 'hall-1',
        'StartTime' => '2026-05-11T08:23:23.000',
        'FinishTime' => '2026-05-11T08:31:23.000',
        'RegistrationNumber' => 'EX4451',
        'VehicleType' => 'Truck',
        'IdentificationType' => 'LPR',
        'IdentificationId' => 'EX4451',
        'Info' => 'EX4451',
        'Updated' => null,
        'Prepaid' => false,
        'FinishStatus' => 1,
        'CustomerGuid' => 'customer-guid-1',
        'VehicleId' => 'vehicle-id-1',
        'WashItems' => [],
        'ignored_at' => '2026-05-11 09:00:00',
        'ignored_by' => '42',
        'ignored_reason' => 'Already handled in period review',
    ]);

    expect($log->ignored_at)->toBe('2026-05-11 09:00:00')
        ->and($log->ignored_by)->toBe(42)
        ->and($log->ignored_reason)->toBe('Already handled in period review');
});

it('calculates XL Vask amount summaries without hydrating order item previews', function (): void {
    $summary = xlvask_usage_logs_o::calculateAmountSummaryFromWashItems(json_encode([
        [
            'OriginalProductName' => 'Stor bil',
            'PriceIncVat' => '625.00',
            'Vat' => '125.00',
        ],
        [
            'OriginalProductName' => 'Skylning',
            'PriceIncVat' => '125,00',
            'Vat' => '25,00',
        ],
    ], JSON_THROW_ON_ERROR));

    expect($summary)
        ->toMatchArray([
            'total_net_amount' => 600.0,
            'primary_product_name' => 'Stor bil',
        ]);
});

it('formats date-only XL Vask usage import start dates for the upstream API', function (): void {
    $method = new ReflectionMethod(xlvask_usage_logs_o::class, 'formatImportDateFrom');

    expect($method->invoke(null, '2026-03-01'))->toBe('2026-03-01T00:00:00.000');
});

it('filters fetched XL Vask usage logs inclusively to the requested import end date', function (): void {
    $keep = new xlvask_usage_log(['StartTime' => '2026-03-31T23:59:59.000']);
    $drop = new xlvask_usage_log(['StartTime' => '2026-04-01T00:00:00.000']);
    $method = new ReflectionMethod(xlvask_usage_logs_o::class, 'filterUsageLogsUntil');

    $result = $method->invoke(null, [$keep, $drop], '2026-03-31');

    expect($result)
        ->toHaveCount(1)
        ->and($result[0])->toBe($keep);
});
