<?php
require_once __DIR__ . '/../vendor/autoload.php';
$app = require __DIR__ . '/../app/bootstrap.php';

use App\Services\ActivityTypeRepairService;
use App\Utils\Logger;

if (PHP_SAPI !== 'cli') {
    http_response_code(400);
    echo "CLI only\n";
    exit(1);
}

$backupPath = $argv[1] ?? (__DIR__ . '/../latest db backup.sql');
$apply = in_array('--apply', $argv, true);

if (!is_file($backupPath)) {
    fwrite(STDERR, "Backup file not found: {$backupPath}\n");
    exit(1);
}

$repairService = new ActivityTypeRepairService($app->db);
$report = $apply ? $repairService->apply($backupPath) : $repairService->preview($backupPath);

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

Logger::logInfo('Activity type repair from backup executed', $report);
