<?php

app_require('traits/economic_endpoint_t.php');

use traits\economic_endpoint_t;

if (!class_exists('EconomicEndpointUrlEncodingProbe')) {
    class EconomicEndpointUrlEncodingProbe
    {
        use economic_endpoint_t {
            build_request_url as public buildRequestUrl;
        }
    }
}

beforeEach(function (): void {
    global $ECONOMIC_API;

    $ECONOMIC_API = [
        'app_secret_token' => 'test-secret',
        'app_access_grant' => 'test-grant',
        'app_access_grant2' => 'test-grant-2',
    ];
});

it('encodes raw filter query values that include timestamps', function (): void {
    $probe = new EconomicEndpointUrlEncodingProbe();

    $url = $probe->buildRequestUrl(
        '/invoices/booked/?filter=(date$gte:2026-01-01 00:00:00$and:date$lte:2026-01-31 23:59:59)&pageSize=100&skipPages=0'
    );

    expect($url)->toBe(
        'https://restapi.e-conomic.com/invoices/booked/?filter=%28date%24gte%3A2026-01-01%2000%3A00%3A00%24and%3Adate%24lte%3A2026-01-31%2023%3A59%3A59%29&pageSize=100&skipPages=0'
    );
});

it('avoids double encoding query values that are already escaped', function (): void {
    $probe = new EconomicEndpointUrlEncodingProbe();

    $url = $probe->buildRequestUrl(
        '/invoices/booked/?filter=references.other%24eq%3AEXT%20123&pageSize=100'
    );

    expect($url)->toBe(
        'https://restapi.e-conomic.com/invoices/booked/?filter=references.other%24eq%3AEXT%20123&pageSize=100'
    );
});
