<?php

define('BASE_PATH', realpath(__DIR__ . '/../..'));

$layoutFiles = [
    BASE_PATH . '/app/views/layouts/reviewer_layout.php',
    BASE_PATH . '/app/views/layouts/author_layout.php',
    BASE_PATH . '/app/views/layouts/admin_layout.php',
    BASE_PATH . '/app/views/layouts/track_chair_layout.php',
    BASE_PATH . '/app/views/layouts/organizer_layout.php',
    BASE_PATH . '/app/views/layouts/org_admin_layout.php',
    BASE_PATH . '/app/views/layouts/chair_layout.php',
    BASE_PATH . '/app/views/layouts/sidebar_links.php',
    BASE_PATH . '/app/views/partials/unified_role_layout_links.php',
];

$routesFile = BASE_PATH . '/config/routes.php';

$routes = is_file($routesFile)
    ? file_get_contents($routesFile)
    : '';

echo "SIDEBAR ROUTE AUDIT\n\n";

foreach ($layoutFiles as $file) {

    if (!is_file($file)) {
        continue;
    }

    $txt = file_get_contents($file);

    preg_match_all(
        "#/easychair/[a-zA-Z0-9_./?=&-]+#",
        $txt,
        $m
    );

    $urls = array_values(array_unique($m[0]));

    sort($urls);

    echo "FILE: " . str_replace(BASE_PATH . '/', '', $file) . "\n";

    foreach ($urls as $url) {

        $route = preg_replace("#^/easychair#", "", $url);
        $route = preg_replace("#\\?.*$#", "", $route);

        if ($route === '') {
            $route = '/';
        }

        $exists =
            strpos($routes, "'" . $route . "'") !== false
            ||
            strpos($routes, '"' . $route . '"') !== false;

        echo ($exists ? " OK      " : " MISSING ") . $url . "\n";
    }

    echo "\n";
}

echo "DONE\n";
