<?php

declare(strict_types=1);

/**
 * Apply word-level umlaut mapping to the dev real-estate Blade templates so
 * UI labels stay consistent with the JSON content.
 */
$bladeFiles = [
    __DIR__.'/../../resources/views/web/dev/immobilien-azizi.blade.php',
    __DIR__.'/../../resources/views/web/dev/immobilien-azizi-show.blade.php',
];

$wordMap = [
    'oeffnen' => 'öffnen',
    'fuer' => 'für',
    'Fuer' => 'Für',
    'Verfuegbarkeit' => 'Verfügbarkeit',
    'verfuegbar' => 'verfügbar',
    'Zurueck' => 'Zurück',
    'Projektuebersicht' => 'Projektübersicht',
    'schliessen' => 'schließen',
    'geprueft' => 'geprüft',
    'prueft' => 'prüft',
    'pruefen' => 'prüfen',
    'pruefe' => 'prüfe',
    'Pruefen' => 'Prüfen',
    'Naechstes' => 'Nächstes',
    'naechstes' => 'nächstes',
    'Naechster' => 'Nächster',
    'Uebergabe' => 'Übergabe',
    'abhaengig' => 'abhängig',
    'Broschuere' => 'Broschüre',
    'dafuer' => 'dafür',
    'koennen' => 'können',
    'muessen' => 'müssen',
    'ueber' => 'über',
    'Ueber' => 'Über',
];

$keys = array_keys($wordMap);
usort($keys, fn ($a, $b) => strlen($b) <=> strlen($a));
$pattern = '/\b('.implode('|', array_map('preg_quote', $keys)).')\b/u';

foreach ($bladeFiles as $bladeFile) {
    if (! is_file($bladeFile)) {
        echo 'Skip (nicht gefunden): '.$bladeFile.PHP_EOL;

        continue;
    }
    $replacements = 0;
    $content = file_get_contents($bladeFile);
    $new = preg_replace_callback($pattern, function ($m) use ($wordMap, &$replacements) {
        $replacements++;

        return $wordMap[$m[1]];
    }, $content);
    file_put_contents($bladeFile, $new);
    echo basename($bladeFile).': '.$replacements.' Ersetzungen'.PHP_EOL;
}
