<?php
$user       = $user ?? null;
$currentUri = $current_uri ?? '';
$roleCodes  = $user && !empty($user['roles']) ? array_column($user['roles'], 'role_code') : [];
$isAdmin    = in_array('ADMIN', $roleCodes, true);
$isParent   = in_array('ORANG_TUA', $roleCodes, true);

$navClassActive   = 'flex items-center gap-3 px-4 py-3 rounded-lg bg-primary/10 text-primary font-medium';
$navClassInactive = 'flex items-center gap-3 px-4 py-3 rounded-lg text-gray-600 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700';

function nav_is_active(string $currentUri, string $path, bool $exact = true): bool {
    if ($exact) {
        return $currentUri === $path;
    }
    return $currentUri === $path || strpos($currentUri, $path . '/') === 0;
}
?>
<aside id="sidebar" class="w-64 min-h-screen bg-white dark:bg-gray-800 border-r border-gray-200 dark:border-gray-700 transform -translate-x-full lg:translate-x-0 transition-transform duration-200 fixed lg:static inset-y-0 left-0 z-40">
    <div class="flex flex-col h-full">
        <div class="flex items-center h-16 px-6 border-b border-gray-200 dark:border-gray-700">
            <a href="<?= base_url('dashboard') ?>" class="text-xl font-bold text-primary dark:text-primary-400">SMAN 1 Garut</a>
        </div>
        <nav class="flex-1 p-4 space-y-1 overflow-y-auto">
            <a href="<?= base_url('dashboard') ?>" class="<?= $currentUri === 'dashboard' ? $navClassActive : $navClassInactive ?>">
                <i class="bx bx-home-alt text-xl"></i>
                <span>Dashboard</span>
            </a>
            <a href="<?= base_url('dashboard/profile') ?>" class="<?= nav_is_active($currentUri, 'dashboard/profile') ? $navClassActive : $navClassInactive ?>">
                <i class="bx bx-user text-xl"></i>
                <span>Profil</span>
            </a>
            <a href="<?= base_url('dashboard/schedule/today') ?>" class="<?= nav_is_active($currentUri, 'dashboard/schedule/today') ? $navClassActive : $navClassInactive ?>">
                <i class="bx bx-calendar text-xl"></i>
                <span>Jadwal Hari Ini</span>
            </a>
            <a href="<?= base_url('dashboard/attendance/reports') ?>" class="<?= nav_is_active($currentUri, 'dashboard/attendance/reports') ? $navClassActive : $navClassInactive ?>">
                <i class="bx bx-calendar-check text-xl"></i>
                <span>Laporan Absensi</span>
            </a>
            <a href="<?= base_url('dashboard/discipline') ?>" class="<?= nav_is_active($currentUri, 'dashboard/discipline') ? $navClassActive : $navClassInactive ?>">
                <i class="bx bx-error-alt text-xl"></i>
                <span>Poin Pelanggaran</span>
            </a>
            <?php if ($isParent): ?>
            <a href="<?= base_url('dashboard/parent') ?>" class="<?= nav_is_active($currentUri, 'dashboard/parent') ? $navClassActive : $navClassInactive ?>">
                <i class="bx bx-user-circle text-xl"></i>
                <span>Portal Orang Tua</span>
            </a>
            <?php endif; ?>
            <?php if ($isAdmin): ?>
            <a href="<?= base_url('dashboard/presence-settings') ?>" class="<?= nav_is_active($currentUri, 'dashboard/presence-settings') ? $navClassActive : $navClassInactive ?>">
                <i class="bx bx-map-pin text-xl"></i>
                <span>Pengaturan Presensi</span>
            </a>
            <a href="<?= base_url('dashboard/academic/settings') ?>" class="<?= nav_is_active($currentUri, 'dashboard/academic', false) ? $navClassActive : $navClassInactive ?>">
                <i class="bx bx-cog text-xl"></i>
                <span>Pengaturan Academic</span>
            </a>
            <a href="<?= base_url('dashboard/devices') ?>" class="<?= nav_is_active($currentUri, 'dashboard/devices') ? $navClassActive : $navClassInactive ?>">
                <i class="bx bx-devices text-xl"></i>
                <span>Device Absen</span>
            </a>
            <?php endif; ?>
        </nav>
    </div>
</aside>
<div id="sidebar-backdrop" class="fixed inset-0 bg-black/50 z-30 lg:hidden hidden" aria-hidden="true"></div>
