<?php

it('exposes direct linked order metadata on XL Vask usage order rows', function (): void {
    $route = file_get_contents(WD . '/routes/xlvaskUsageLogsRoute.php');

    expect($route)->not->toBeFalse();

    $route = (string)$route;

    expect($route)
        ->toContain('$linked_order_ids_by_wash_id = []')
        ->and($route)->toContain('array_key_exists($wash_id, $linked_order_ids_by_wash_id)')
        ->and($route)->toContain('selectByWashId($wash_id)')
        ->and($route)->toContain("'linked_order_id' => \$linked_order_id")
        ->and($route)->toContain("'usage_log_id' => \$id");
});

it('does not execute XL Vask usage automation while listing usage order rows', function (): void {
    $route = file_get_contents(WD . '/routes/xlvaskUsageLogsRoute.php');

    expect($route)->not->toBeFalse();

    $route = (string)$route;

    expect($route)
        ->toContain('$automation = $automation_service->evaluateUsageLogRow($log, (int)$user->id, false);')
        ->and($route)->not->toContain('$automation = $automation_service->evaluateUsageLogRow($log, (int)$user->id, true);')
        ->and($route)->toContain("requirePermission('manage_xlvask_usage_automation')");
});

it('returns cached amount summaries on XL Vask usage order rows without widening the usage-log object payload', function (): void {
    $route = file_get_contents(WD . '/routes/xlvaskUsageLogsRoute.php');

    expect($route)->not->toBeFalse();

    $route = (string)$route;

    expect($route)
        ->toContain('$amount_summary = $xlvask_usage_logs->getCachedAmountSummaryFromRow($log)')
        ->and($route)->toContain('$usage_log_payload = array_intersect_key($log, array_flip([')
        ->and($route)->toContain('$tmp->setProperties($usage_log_payload)')
        ->and($route)->toContain("\$tmp_res['order']['total_net_amount'] = \$amount_summary['total_net_amount']")
        ->and($route)->toContain("\$tmp_res['order']['xlvask_primary_product_name'] = \$amount_summary['primary_product_name']")
        ->and($route)->toContain("\$tmp_res['order']['xlvask_amount_cached'] = \$amount_summary['cached']");
});

it('scopes manual XL Vask usage import and automation to optional period dates', function (): void {
    $route = file_get_contents(WD . '/routes/moduleXLVaskRoute.php');
    $automation = file_get_contents(WD . '/classes/xlvask_automation_service.php');

    expect($route)
        ->not->toBeFalse()
        ->and($automation)->not->toBeFalse();

    $route = (string)$route;
    $automation = (string)$automation;

    expect($route)
        ->toContain("getParameter('dateFrom')")
        ->toContain("getParameter('dateTo')")
        ->toContain('$xlvask_usage_logs_o->importUsageLogs($dateFrom, $dateTo)')
        ->toContain('runPending($dateFrom, $dateTo, [], 100, null)')
        ->and($automation)->toContain("STR_TO_DATE(REPLACE(SUBSTRING(StartTime, 1, 19), 'T', ' '), '%Y-%m-%d %H:%i:%s')");
});
