<?php

/**
 * Dump the exact JSON Torob would receive from Product API v3 (no JWT).
 *
 * Usage:
 *   php engine/tests/torob_api_dump.php
 *   php engine/tests/torob_api_dump.php page 1 date_added_desc
 *   php engine/tests/torob_api_dump.php unique 12_34
 *   php engine/tests/torob_api_dump.php url "https://biato.shop/product/slug/?v=34"
 */

require __DIR__ . '/../vendor/autoload.php';

$app = require __DIR__ . '/../bootstrap/app.php';
$app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();

use App\Services\TorobCatalogService;

$svc = app(TorobCatalogService::class);
$mode = $argv[1] ?? 'page';

$result = match ($mode) {
    'page' => $svc->paginate(
        max(1, (int) ($argv[2] ?? 1)),
        (string) ($argv[3] ?? 'date_added_desc')
    ),
    'unique' => $svc->byUniques([(string) ($argv[2] ?? '')]),
    'url' => $svc->byUrls([(string) ($argv[2] ?? '')]),
    default => [
        'error' => 'Usage: php torob_api_dump.php [page|unique|url] ...',
    ],
};

echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL;
