<?php
/**
 * Fix encoding issues and broken symbols in the project.
 */

$directory = __DIR__;
$extensions = ['php', 'html', 'css', 'js'];

// Define replacements for various corrupted sequences
$replacements = [
    // Corrupted Trademark
    'â„¢' => '&trade;',
    'MileTruth<superscript>&trade;</superscript>' => 'MileTruth&trade;',
    'MileTruth<superscript>™</superscript>' => 'MileTruth&trade;',
    'MileTruth™' => 'MileTruth&trade;',
    
    // Corrupted quotes and dashes (UTF-8 bytes misinterpreted as ISO-8859-1 and then saved as UTF-8)
    'â€“' => '&ndash;',
    'â€”' => '&mdash;',
    'â€™' => '&rsquo;',
    'â€œ' => '&ldquo;',
    'â€' => '&rdquo;',
    'â€¦' => '&hellip;',
    'â€¢' => '&bull;',
    
    // Corrupted spaces and symbols
    'Â©' => '&copy;',
    'Â ' => ' ', // Often Â + nbsp
    'Â' => '',   // Lone Â artifact
    'ðŸ' => '',  // Broken emoji
    
    // Fix existing trade entities if needed
    '<sup>&trade;</sup>' => '&trade;',
    '<superscript>&trade;</superscript>' => '&trade;',
    '<superscript>™</superscript>' => '&trade;',
];

/**
 * Recursively find all files with specific extensions.
 * 
 * @param string $dir
 * @param array $extensions
 * @return array
 */
function get_all_files(string $dir, array $extensions): array {
    $files = [];
    $items = scandir($dir);
    foreach ($items as $item) {
        if ($item == '.' || $item == '..' || $item == '.git' || $item == 'non-used' || $item == 'fix_encoding.php') continue;
        $path = $dir . DIRECTORY_SEPARATOR . $item;
        if (is_dir($path)) {
            $files = array_merge($files, get_all_files($path, $extensions));
        } else {
            $ext = pathinfo($path, PATHINFO_EXTENSION);
            if (in_array($ext, $extensions)) {
                $files[] = $path;
            }
        }
    }
    return $files;
}

$files = get_all_files($directory, $extensions);

echo "Processing " . count($files) . " files...\n";

foreach ($files as $file) {
    $content = file_get_contents($file);
    if ($content === false) continue;
    
    $original_content = $content;

    // Apply replacements multiple times to handle nested issues
    $changed = true;
    while ($changed) {
        $old_content = $content;
        foreach ($replacements as $search => $replace) {
            $content = str_replace($search, $replace, $content);
        }
        if ($old_content === $content) {
            $changed = false;
        }
    }

    if ($content !== $original_content) {
        file_put_contents($file, $content);
        echo "Fixed: $file\n";
    }
}

echo "Done.\n";
