<?php
// cli/scan.php

declare(strict_types=1);

$services = require __DIR__ . '/../app/bootstrap.php';

$jobs = $services['jobs'];
$scanner = $services['scanner'];

$jobId = $jobs->create('scan', 'Scan (CLI)', [
  'source' => 'cli',
]);

echo "Queued job id={$jobId}\n";

try {
  $jobs->start($jobId);
  $result = $scanner->runScanJob($jobId);
  $jobs->finish($jobId);

  echo "OK: scan finished\n";
  echo "Profiles: " . ($result['profiles'] ?? 0) . "\n";
  echo "Items seen: " . ($result['items_seen'] ?? 0) . "\n";
  exit(0);
} catch (Throwable $e) {
  $jobs->log($jobId, 'error', $e->getMessage());
  $jobs->fail($jobId, $e->getMessage());
  fwrite(STDERR, "ERROR: " . $e->getMessage() . "\n");
  exit(1);
}
