#!/usr/bin/env php
<?php
declare(strict_types=1);
require dirname(__DIR__) . '/app/Services/DB.php';
require dirname(__DIR__) . '/app/Services/Asm3Analyzer.php';
require dirname(__DIR__) . '/app/Services/Asm3MigrationPlanner.php';
$source = $argv[1] ?? null;
$output = $argv[2] ?? null;
if (!$source) {
    fwrite(STDERR, "Usage : php scripts/asm3-plan.php <dump.sql> [rapport.json]\n");
    exit(2);
}
try {
    $plan = Asm3MigrationPlanner::plan($source, DB::pdo());
    $json = json_encode($plan, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
} catch (Throwable $e) {
    fwrite(STDERR, "Erreur : {$e->getMessage()}\n");
    exit(1);
}
if ($output) {
    if (file_put_contents($output, $json) === false) {
        fwrite(STDERR, "Impossible d’écrire le rapport.\n");
        exit(1);
    }
    echo "Plan écrit dans {$output}\n";
} else {
    echo $json;
}
