<?php

/**
 * Sync all language files to have the same keys as en.json
 */

echo "=== SYNCING TRANSLATION KEYS ===\n\n";

$enFile = 'resources/lang/en.json';
$en = json_decode(file_get_contents($enFile), true);
$enCount = count($en);

echo "Source (en.json): {$enCount} keys\n\n";

$locales = ['nl', 'de', 'es', 'fr'];

foreach ($locales as $locale) {
    $file = "resources/lang/{$locale}.json";
    $data = json_decode(file_get_contents($file), true);
    $before = count($data);

    // Add missing keys from en.json
    $added = 0;
    foreach ($en as $key => $value) {
        if (!isset($data[$key])) {
            $data[$key] = $key; // Use English as placeholder
            $added++;
        }
    }

    // Remove keys not in en.json
    $removed = 0;
    foreach (array_keys($data) as $key) {
        if (!isset($en[$key])) {
            unset($data[$key]);
            $removed++;
        }
    }

    $after = count($data);

    // Sort alphabetically
    ksort($data);

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

    echo "{$locale}.json: {$before} → {$after} keys (+{$added} -{$removed})\n";
}

echo "\n✓ All language files synced!\n";
