<?php

/**
 * Sync all translation files to have the same keys as en.json
 * Missing keys will use the English text as placeholder
 */

$locales = ['nl', 'de', 'es', 'fr'];
$enFile = 'resources/lang/en.json';

// Load English source
$en = json_decode(file_get_contents($enFile), true);
echo "Source (en.json): " . count($en) . " keys\n\n";

foreach ($locales as $locale) {
    $file = "resources/lang/{$locale}.json";

    // Load existing translations
    $translations = json_decode(file_get_contents($file), true);
    $before = count($translations);

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

    // Merge: keep existing translations, add missing keys with English value
    $synced = [];
    foreach ($en as $key => $value) {
        if (isset($translations[$key])) {
            // Keep existing translation
            $synced[$key] = $translations[$key];
        } else {
            // Add missing key with English value as placeholder
            $synced[$key] = $value;
        }
    }

    // Sort alphabetically by key
    ksort($synced);

    // Save synced file
    file_put_contents($file, json_encode($synced, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL);

    $after = count($synced);
    $added = $after - $before;

    echo "{$locale}.json:\n";
    echo "  Before: {$before} keys\n";
    echo "  After:  {$after} keys\n";
    echo "  Added:  {$added} keys (with English placeholders)\n";
    echo "  Backup: {$file}.backup\n\n";
}

echo "✓ All files synced to " . count($en) . " keys\n";
echo "\nNext step: Run AI translation to translate the placeholder keys\n";
