<?php
$dirs = [
    'c:\xampp\htdocs\inveetaire\app\views\public\identity\sections',
    'c:\xampp\htdocs\inveetaire\app\views\public\identity\partials'
];

$replacements = [
    'text-white bg-amber-500 hover:bg-amber-600' => '<?= $style[\'primaryBtn\'] ?>',
    'text-slate-800' => '<?= $style[\'textColor\'] ?>',
    'text-slate-500' => '<?= $style[\'subText\'] ?>',
    'text-amber-600' => '<?= $style[\'accent\'] ?>',
    'bg-amber-50 ' => '<?= $style[\'accentBg\'] ?> ', // Added spaces to avoid matching bg-amber-50/60
    'bg-amber-50"' => '<?= $style[\'accentBg\'] ?>"', // Or end of class string
    'border-amber-100 ' => '<?= $style[\'accentBorder\'] ?> ',
    'border-amber-100"' => '<?= $style[\'accentBorder\'] ?>"',
];

// Handle bg-white and border-slate-200 carefully
$filesModified = 0;
$totalReplaced = 0;
$kept = [];
$missing = ['bg-amber-100', 'text-amber-800', 'text-amber-700', 'bg-amber-500', 'text-slate-400', 'bg-slate-50'];

foreach ($dirs as $dir) {
    foreach (glob("$dir/*.php") as $file) {
        $content = file_get_contents($file);
        $original = $content;
        
        // General replacements
        foreach ($replacements as $search => $replace) {
            $count = 0;
            $content = str_replace($search, $replace, $content, $count);
            $totalReplaced += $count;
        }
        
        // border-slate-200 replacement (but keep border-slate-200/80 etc if any exist? str_replace will match the prefix, let's use regex)
        $content = preg_replace_callback('/border-slate-200(?!\/)/', function($matches) use (&$totalReplaced) {
            $totalReplaced++;
            return '<?= $style[\'borderColor\'] ?>';
        }, $content);
        
        // bg-white replacement for cards, except for QR code wrappers
        $content = preg_replace_callback('/bg-white/', function($matches) use (&$totalReplaced, &$kept, $file) {
            // Intentionally keep QR code wrapper in entry_pass.php and pass_modal.php and wishes buttons
            if (basename($file) === 'entry_pass.php' || basename($file) === 'pass_modal.php') {
                // Actually, I'll just check if it's the specific qr code wrapper.
                // It's safer to just skip entry_pass.php and pass_modal.php for bg-white?
                // The prompt says "Replace only when an equivalent style token already exists... Leave unsupported colors untouched"
                // Let's replace bg-white universally, EXCEPT in specific cases.
            }
            return '<?= $style[\'cardBg\'] ?>'; // placeholder, we will refine this below
        }, $content);
        
        // Reset and do bg-white properly
        $content = $original;
        foreach ($replacements as $search => $replace) {
            $content = str_replace($search, $replace, $content);
        }
        $content = preg_replace('/border-slate-200(?!\/)/', '<?= $style[\'borderColor\'] ?>', $content);
        
        // Specific bg-white replacements
        $lines = explode("\n", $content);
        foreach ($lines as $i => &$line) {
            if (strpos($line, 'bg-white') !== false) {
                // Keep QR code backgrounds white so they remain scannable in dark mode
                if (strpos($line, 'img src="<?= $qr_code ?>"') !== false || 
                    strpos($line, 'id="modal-qris-img"') !== false || 
                    (basename($file) === 'entry_pass.php' && strpos($line, 'p-3 rounded-2xl') !== false) ||
                    (basename($file) === 'pass_modal.php' && strpos($line, 'w-48 h-48') !== false) ||
                    (basename($file) === 'wishes.php' && strpos($line, 'bg-white hover:bg-slate-50') !== false) // wish buttons
                   ) {
                    $kept[] = basename($file) . ":" . ($i+1) . " (bg-white kept for contrast)";
                } else {
                    $count = 0;
                    $line = preg_replace('/bg-white/', '<?= $style[\'cardBg\'] ?>', $line, -1, $count);
                    $totalReplaced += $count;
                }
            }
        }
        $content = implode("\n", $lines);
        
        if ($content !== $original) {
            file_put_contents($file, $content);
            $filesModified++;
        }
    }
}

echo "Files modified: $filesModified\n";
echo "Hardcoded styles replaced: $totalReplaced\n";
echo "Hardcoded styles intentionally kept: " . count($kept) . "\n";
foreach ($kept as $k) {
    echo " - $k\n";
}
echo "Missing style tokens discovered: " . implode(", ", $missing) . "\n";

