<?php
// Define the root path
$rootPath = '/Users/nujoma/Desktop/Polakis 360/ENT/';

// Include the menu config file
require_once $rootPath . 'includes/menu_config.php';

// Define all roles to check
$roles = [
    'Super Admin',
    'Hospital Admin',
    'ENT Consultant',
    'ENT Doctor',
    'Resident Doctor',
    'Head Nurse',
    'ENT Nurse',
    'Audiologist',
    'Lab Technician',
    'Receptionist',
    'Billing Clerk',
    'Medical Records',
    'Patient'
];

$missingPages = [];

// Recursive function to extract URLs
function extractUrls($items, &$urls)
{
    foreach ($items as $item) {
        if (isset($item['url'])) {
            // Handle potential anchors like system_settings.php#mpesa
            $urlParts = explode('#', $item['url']);
            $urls[] = $urlParts[0];
        }
        if (isset($item['items']) && is_array($item['items'])) {
            extractUrls($item['items'], $urls);
        }
    }
}

foreach ($roles as $role) {
    echo "Checking role: $role\n";
    $menu = getMenuConfig($role);

    $urls = [];
    extractUrls($menu, $urls);

    foreach ($urls as $url) {
        if (empty($url) || $url === '#')
            continue;

        $filePath = $rootPath . $url;
        if (!file_exists($filePath)) {
            if (!isset($missingPages[$role])) {
                $missingPages[$role] = [];
            }
            if (!in_array($url, $missingPages[$role])) {
                $missingPages[$role][] = $url;
            }
        }
    }
}

// Check common pages (account menu is added to all roles, but let's check unique missing ones)
echo "\n--- Missing Pages Report ---\n";
foreach ($missingPages as $role => $pages) {
    echo "\nRole: $role\n";
    foreach ($pages as $page) {
        echo " - [MISSING] $page\n";
    }
}

if (empty($missingPages)) {
    echo "\nNo missing pages found!\n";
}
?>