<?php

/**
 * Prepare JSON files for Laravel AI Translator by removing untranslated keys
 * The AI translator only translates keys that don't exist in the target file
 */

$locale = $argv[1] ?? null;

if (!$locale || !in_array($locale, ['nl', 'de', 'es', 'fr'])) {
    echo "Usage: php prepare-for-ai-translator.php <locale>\n";
    echo "Available locales: nl, de, es, fr\n";
    exit(1);
}

$file = "resources/lang/{$locale}.json";
$translations = json_decode(file_get_contents($file), true);

// Create backup
copy($file, "{$file}.backup");

// Keep only translated keys (where value !== key)
$translatedOnly = [];
foreach ($translations as $key => $value) {
    if ($key !== $value) {
        $translatedOnly[$key] = $value;
    }
}

$before = count($translations);
$after = count($translatedOnly);
$removed = $before - $after;

ksort($translatedOnly);
file_put_contents($file, json_encode($translatedOnly, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL);

echo "Prepared {$locale}.json for AI translator:\n";
echo "  - Backup saved to {$locale}.json.backup\n";
echo "  - Removed {$removed} untranslated keys\n";
echo "  - Kept {$after} translated keys\n";
echo "\nNow run: php artisan ai-translator:translate-json --source=en --locale={$locale} --chunk=100\n";
