<?php

require_once dirname(__DIR__) . '/db_bootstrap.php';
require_once dirname(__DIR__) . '/agent_legacy_migration.php';

$conn = ensure_db(true);
$first = ua_agent_migrate_legacy($conn);
$second = ua_agent_migrate_legacy($conn);
if (!$first['ok'] || !$second['ok'] || $second['transactions'] !== 0 || $second['opening_entries'] !== 0) {
  fwrite(STDERR, json_encode(['first'=>$first,'second'=>$second], JSON_UNESCAPED_UNICODE) . PHP_EOL);
  exit(1);
}
echo json_encode(['ok'=>true,'first'=>$first,'idempotent_second_run'=>$second], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . PHP_EOL;
