<?php

app_require('classes/customer_name_cache_payload_builder.php');
app_require('objects/users_o.php');

use classes\customer_name_cache_payload_builder;
use objects\users_o;

it('builds customer name cache payloads from economic data or display-name fallbacks', function (): void {
    expect(customer_name_cache_payload_builder::build(
        (object)['name' => 'Truckwash ApS'],
        'Fallback Name'
    ))->toBe(['name' => 'Truckwash ApS']);

    expect(customer_name_cache_payload_builder::build(
        '{"customer":{"name":"Nested Truckwash ApS"}}',
        'Fallback Name'
    ))->toBe(['name' => 'Nested Truckwash ApS']);

    expect(customer_name_cache_payload_builder::build(
        ['customer_name' => 'Array Truckwash ApS'],
        'Fallback Name'
    ))->toBe(['name' => 'Array Truckwash ApS']);

    expect(customer_name_cache_payload_builder::build(
        null,
        'Fallback Name'
    ))->toBe(['name' => 'Fallback Name']);

    expect(customer_name_cache_payload_builder::build(
        (object)['name' => '   '],
        null
    ))->toBeNull();
});

it('guards bulk customer-name cache writes behind a resolved payload check', function (): void {
    $usersFile = app_path('objects/users_o.php');
    $content = file_get_contents($usersFile);

    expect($content)->not->toBeFalse();
    expect($content)->toContain('use classes\customer_name_cache_payload_builder;');
    expect($content)->toContain('use classes\system_search_economic_customer_index;');
    expect($content)->toContain('return customer_name_cache_payload_builder::build($cached_name, $fallback_name);');
    expect($content)->toContain('$cache_payload = self::buildCustomerNameCachePayload($cached_name, $fallback_name);');
    expect($content)->toContain('if ($cache_payload !== null) {');
    expect($content)->toContain("\$this->cache('economic_customer_name', \$cache_payload, \$customer_number);");
});

it('allows callers to resolve customer names without e-conomic fallback', function (): void {
    $usersFile = app_path('objects/users_o.php');
    $content = file_get_contents($usersFile);

    expect($content)->not->toBeFalse();
    expect($content)->toContain('public function getCustomerNames(array $customer_numbers, bool $allowExternalFetch = true): array');
    expect($content)->toContain('if (!$allowExternalFetch) {');
    expect($content)->toContain('$local_cached_names = $this->getCachedEconomicCustomerNamesByCustomerNumber($customer_numbers_to_fetch);');
    expect($content)->toContain("\$customer_names[(string)\$customer_number] = \$local_cached_names[\$customer_number] ?? \$fallback_names[\$customer_number] ?? 'Unknown Customer';");
    expect($content)->toContain('private function getLocalDisplayNamesByCustomerNumber(array $customer_numbers): array');
    expect($content)->toContain('private function getCachedEconomicCustomerNamesByCustomerNumber(array $customer_numbers): array');
    expect($content)->toContain("\$cached_names = \$this->getCachedForMultipleObjects('economic_customer', array_values(\$user_ids_by_customer_number));");
    expect($content)->toContain('system_search_economic_customer_index::TABLE');
});

it('returns an empty customer name map without touching cache for empty input', function (): void {
    $users = new users_o();

    expect($users->getCustomerNames([]))->toBe([]);
});

it('returns no rows for empty array field filters', function (): void {
    $users = new users_o();

    expect($users->getFieldsWhere(['id' => []], ['customer_number']))->toBe([]);
});
