<?php
// scripts/check_paths.php
// Scans the views/ directory for common absolute-root URL patterns

$root = __DIR__ . '/..';
$views = $root . '/views';
$patterns = [
    '/(href|src|action)\s*=\s*["\']\//i',
    '/fetch\(\s*["\']\//i',
    '/window\.location\.href\s*=\s*["\']\//i',
];

$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($views));
$matches = [];

foreach ($rii as $file) {
    if ($file->isDir()) continue;
    $path = $file->getPathname();
    if (!preg_match('/\.php$/i', $path)) continue;
    $lines = file($path);
    foreach ($lines as $num => $line) {
        foreach ($patterns as $pat) {
            if (preg_match($pat, $line)) {
                $matches[] = [
                    'file' => substr($path, strlen($root) + 1),
                    'line' => $num + 1,
                    'snippet' => trim($line),
                ];
            }
        }
    }
}

if (empty($matches)) {
    echo "No absolute-root URL patterns found in views/.\n";
    exit(0);
}

echo "Found absolute-root URL patterns:\n\n";
foreach ($matches as $m) {
    echo $m['file'] . ":" . $m['line'] . "\n    " . $m['snippet'] . "\n\n";
}

exit(0);
