<?php
/**
 * استفاده:
 * php scripts/import_old_runtime.php /full/path/to/old_bale_bot [--with-exports]
 */

require_once __DIR__ . '/../app/Core/bootstrap.php';

$source = $argv[1] ?? '';
$withExports = in_array('--with-exports', $argv, true);

if ($source === '' || !is_dir($source)) {
    fwrite(STDERR, "مسیر پروژه قدیمی معتبر نیست.\n");
    exit(1);
}

$app = app_config();
$runtimeSrc = rtrim($source, '/\\') . '/runtime';
$exportsSrc = rtrim($source, '/\\') . '/exports';

$runtimeFiles = [
    'smart_price_map.json',
    'smart_price_map_latest.csv',
    'latest_smart_review_rows.json',
    'final_smart_applied_rows.json',
    'variant_mapping.json',
    'polling_log.txt',
];

foreach ($runtimeFiles as $file) {
    $from = $runtimeSrc . '/' . $file;
    $to = $app['runtime_path'] . '/' . $file;
    if (is_file($from)) {
        copy($from, $to);
        echo "runtime copied: {$file}\n";
    }
}

if ($withExports && is_dir($exportsSrc)) {
    foreach (glob($exportsSrc . '/*') ?: [] as $file) {
        if (is_file($file)) {
            copy($file, $app['exports_path'] . '/' . basename($file));
            echo "export copied: " . basename($file) . "\n";
        }
    }
}
