<?php
$files = [
    'customers/index.php',
    'products/index.php',
    'products/create.php',
    'products/edit.php',
    'products/import.php',
    'products/import-preview.php',
    'price-list/index.php',
    'reports/index.php',
    'settings/index.php',
    'settings/quotation-branding.php',
];

$base = 'c:/xampp/htdocs/Fortunes_Quotation/public/';

foreach ($files as $file) {
    $path = $base . $file;
    if (file_exists($path)) {
        $lines = file($path);
        if ($lines !== false) {
            $rule = null;
            $headerLineIndex = -1;
            
            foreach ($lines as $i => $line) {
                if (strpos($line, 'require_role_access') !== false || strpos($line, 'require_admin') !== false) {
                    if ($i === 1) { // it's at line 2 right after <?php
                        $rule = trim($line);
                        $lines[$i] = ""; // clear the line
                    }
                }
                if (strpos($line, 'app/includes/header.php') !== false) {
                    $headerLineIndex = $i;
                }
            }
            
            if ($rule && $headerLineIndex !== -1) {
                $lines[$headerLineIndex] = rtrim($lines[$headerLineIndex]) . " " . $rule . "\n";
                file_put_contents($path, implode("", $lines));
                echo "Fixed $file\n";
            }
        }
    }
}
