<?php

app_require('routes/economicPaymentTermsRoute.php');

use routes\economicPaymentTermsRoute;

function economic_payment_terms_route_extract_collection(mixed $response): array
{
    $reflection = new ReflectionClass(economicPaymentTermsRoute::class);
    $target = $reflection->getMethod('extractPaymentTermsCollection');

    return $target->invokeArgs(null, [$response]);
}

it('uses collection when present', function (): void {
    $response = (object)[
        'collection' => [
            (object)['paymentTermsNumber' => 1],
            (object)['paymentTermsNumber' => 2],
        ],
    ];

    expect(economic_payment_terms_route_extract_collection($response))
        ->toBe($response->collection);
});

it('falls back to paymentTerms when collection is missing', function (): void {
    $response = (object)[
        'paymentTerms' => [
            (object)['paymentTermsNumber' => 10],
            (object)['paymentTermsNumber' => 20],
        ],
    ];

    expect(economic_payment_terms_route_extract_collection($response))
        ->toBe($response->paymentTerms);
});

it('supports top-level array payloads', function (): void {
    $response = [
        (object)['paymentTermsNumber' => 100],
        (object)['paymentTermsNumber' => 200],
    ];

    expect(economic_payment_terms_route_extract_collection($response))
        ->toBe($response);
});

it('returns an empty array when no known collection shape exists', function (): void {
    $response = (object)[
        'message' => 'unexpected shape',
    ];

    expect(economic_payment_terms_route_extract_collection($response))
        ->toBe([]);
    expect(economic_payment_terms_route_extract_collection(null))
        ->toBe([]);
});
