<?php
$guideDir = __DIR__ . "/Guide";
$premiumCssFile = "/css/premium-guide.css"; // create this file separately

if (!is_dir($guideDir)) {
    die("Guide folder not found.");
}

$files = glob($guideDir . "/*.html");

foreach ($files as $file) {
    $html = file_get_contents($file);

    // Backup first
    copy($file, $file . ".bak");

    // Remove all <style>...</style> blocks
    $html = preg_replace('/<style\b[^>]*>.*?<\/style>/is', '', $html);

    // Remove duplicate Google font links if already there
    $html = preg_replace('/<link[^>]+fonts\.googleapis\.com[^>]+>/i', '', $html);
    $html = preg_replace('/<link[^>]+fonts\.gstatic\.com[^>]+>/i', '', $html);

    // Add premium CSS before <meta property="og:site_name" content="Tracker Prices South Africa">
</head>
    $insert = '
<!-- Premium Guide Styling -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&amp;display=swap" rel="stylesheet">
<link rel="stylesheet" href="' . $premiumCssFile . '">
';

    $html = str_ireplace('<meta property="og:site_name" content="Tracker Prices South Africa">
</head>', $insert . "\n<meta property="og:site_name" content="Tracker Prices South Africa">
</head>", $html);

    file_put_contents($file, $html);

    echo "Fixed: " . basename($file) . "<br>";
}

echo "<hr>Done. Backups created as .bak files.";
?>