<?php
declare(strict_types=1);

require __DIR__ . '/../app/bootstrap.php';

if (PHP_SAPI !== 'cli') {
    fwrite(STDERR, "This script is intended for CLI use.\n");
    exit(1);
}

$limit = isset($argv[1]) ? (int) $argv[1] : 1000;
$source = $argv[2] ?? 'all';

$result = refresh_medication_sources([
    'limit' => $limit,
    'source' => $source,
]);

echo "Medication catalog refresh complete\n";
echo "Catalog count: {$result['catalog_count']}\n";
echo "Inserted: {$result['inserted']}\n";
echo "Updated: {$result['updated']}\n";

foreach ($result['sources'] as $sourceResult) {
    echo "- {$sourceResult['source']}: {$sourceResult['status']} ";
    echo "({$sourceResult['inserted']} inserted, {$sourceResult['updated']} updated) ";
    echo "{$sourceResult['message']}\n";
}
