<?php

declare(strict_types=1);

/**
 * Test offline delle operazioni feed (ListFeeds/GetFeed), sul path /catalog/v1.
 */

test('listFeeds colpisce /catalog/v1/listFeeds e ritorna i feed', function () {
    $payload = [
        'feeds' => [
            ['feedName' => 'it-bestsellers', 'size' => 10485760, 'md5' => 'abc', 'lastUpdated' => '2026-07-30T03:00:00Z'],
            ['feedName' => 'it-deals', 'size' => 2097152, 'md5' => 'def', 'lastUpdated' => '2026-07-30T03:05:00Z'],
        ],
    ];

    $client = mockedCreatorsClient([jsonStubResponse(200, $payload)], $history);

    $response = $client->listFeeds();

    $request = lastMockRequest($history);

    expect($request->getMethod())->toBe('POST');
    expect($request->getUri()->getPath())->toBe('/catalog/v1/listFeeds');
    expect((string) $request->getBody())->toBe('');
    expect($response['feeds'])->toHaveCount(2);
    expect($response['feeds'][0]['feedName'])->toBe('it-bestsellers');
})->group('unit');

test('getFeed invia feedName e ritorna la url', function () {
    $client = mockedCreatorsClient([
        jsonStubResponse(200, ['url' => 'https://creatorsapi-feeds.example.invalid/it-bestsellers.tsv.gz?sig=stub']),
    ], $history);

    $response = $client->getFeed('it-bestsellers');

    $request = lastMockRequest($history);
    $body = json_decode((string) $request->getBody(), true);

    expect($request->getUri()->getPath())->toBe('/catalog/v1/getFeed');
    expect($body['feedName'])->toBe('it-bestsellers');
    expect($response['url'])->toContain('it-bestsellers.tsv.gz');
})->group('unit');

test('getFeed rifiuta un feedName vuoto', function () {
    $client = mockedCreatorsClient([], $history);

    expect(fn () => $client->getFeed(''))
        ->toThrow(InvalidArgumentException::class);
})->group('unit');
