<?php
use App\Helpers\SessionHelper;
use App\Helpers\SettingHelper;

$user = SessionHelper::user();
$currentRoute = $_GET['route'] ?? ($user ? 'dashboard' : 'login');
$setting = SettingHelper::get();
$logoUrl = !empty($setting['logo_path'])
    ? $baseUrl . '/uploads/branding/' . ltrim($setting['logo_path'], '/')
    : null;

$navLink = function (string $route, string $label, string $icon) use ($baseUrl, $currentRoute) {
    $isActive = $currentRoute === $route ? 'active' : '';
    return sprintf(
        '<a class="sidebar-link %s" href="%s?route=%s"><span class="sidebar-icon">%s</span><span>%s</span></a>',
        $isActive,
        $baseUrl,
        htmlspecialchars($route, ENT_QUOTES, 'UTF-8'),
        $icon,
        htmlspecialchars($label, ENT_QUOTES, 'UTF-8')
    );
};
?>

<aside class="app-sidebar text-white d-flex flex-column">
    <div class="app-sidebar-brand px-3 py-3 d-flex align-items-center justify-content-between">
        <div class="d-flex align-items-center gap-3">
            <div class="sidebar-logo flex-shrink-0 <?= $logoUrl ? '' : 'placeholder-logo' ?>">
            <?php if ($logoUrl): ?>
                    <img src="<?= htmlspecialchars($logoUrl) ?>" alt="Logo" class="sidebar-logo-img">
            <?php else: ?>
                    <span>MG</span>
            <?php endif; ?>
            </div>
            <div>
                <div class="fw-bold text-uppercase"><?= htmlspecialchars($setting['company_name'] ?? 'Mutiara Group') ?></div>
                <small class="text-secondary"><?= htmlspecialchars($setting['tagline'] ?? 'Smart HR Portal') ?></small>
            </div>
        </div>
        <button class="btn btn-outline-light btn-sm d-lg-none" type="button" data-bs-toggle="collapse" data-bs-target="#appSidebarMenu">
            <span class="fw-bold">≡</span>
        </button>
    </div>

    <div class="sidebar-menu collapse d-lg-block" id="appSidebarMenu">
        <div class="px-3 px-lg-3 sidebar-scroll">
            <?php if ($user): ?>
                <div class="sidebar-section">
                    <div class="sidebar-section-title">
                        <span>UMUM</span>
                        <span class="sidebar-section-line"></span>
                    </div>
                    <ul class="sidebar-list list-unstyled">
                        <li><?= $navLink('dashboard', 'Dashboard', '🏠'); ?></li>
                        <?php if ($user['role'] === 'staff'): ?>
                            <li><?= $navLink('profile', 'Profil Saya', '👤'); ?></li>
                        <?php endif; ?>
                    </ul>
                </div>

                <?php if ($user['role'] === 'staff'): ?>
                    <div class="sidebar-section mt-3">
                        <div class="sidebar-section-title">
                            <span>Permohonan</span>
                            <span class="sidebar-section-line"></span>
                        </div>
                        <ul class="sidebar-list list-unstyled">
                            <li><?= $navLink('advance/create', 'Advance Salary', '💰'); ?></li>
                            <li><?= $navLink('leave/create', 'Cuti', '🗓️'); ?></li>
                            <li><?= $navLink('claim/create', 'Invoice & Claim', '📄'); ?></li>
                        </ul>
                    </div>
                <?php endif; ?>

                <?php if ($user['role'] === 'approver'): ?>
                    <div class="sidebar-section mt-3">
                        <div class="sidebar-section-title">
                            <span>Pelulus</span>
                            <span class="sidebar-section-line"></span>
                        </div>
                        <ul class="sidebar-list list-unstyled">
                            <li><?= $navLink('approver/pending', 'Pending Requests', '✅'); ?></li>
                        </ul>
                    </div>
                <?php endif; ?>

                <?php if ($user['role'] === 'admin'): ?>
                    <div class="sidebar-section mt-3">
                        <div class="sidebar-section-title">
                            <span>Admin</span>
                            <span class="sidebar-section-line"></span>
                        </div>
                        <ul class="sidebar-list list-unstyled">
                            <li><?= $navLink('admin/requests', 'Dashboard Admin', '📊'); ?></li>
                            <li>
                                <div class="text-uppercase small text-secondary ps-1">Pengurusan Pekerja</div>
                                <ul class="list-unstyled ms-3 mb-2">
                                    <li><?= $navLink('admin/employees/create', 'Daftar Pekerja', '➕'); ?></li>
                                    <li><?= $navLink('admin/employees', 'Senarai Pekerja', '📋'); ?></li>
                                </ul>
                            </li>
                            <li><?= $navLink('admin/users', 'Pengurusan Users', '👥'); ?></li>
                            <li><?= $navLink('admin/settings', 'Tetapan Syarikat', '⚙️'); ?></li>
                        </ul>
                    </div>
                <?php endif; ?>
            <?php else: ?>
                <div class="sidebar-section">
                    <div class="sidebar-section-title">
                        <span>Akses</span>
                        <span class="sidebar-section-line"></span>
                    </div>
                    <ul class="sidebar-list list-unstyled">
                        <li>
                            <a class="sidebar-link" href="<?= $baseUrl ?>?route=login">
                                <span class="sidebar-icon">🔐</span>
                                <span>Login</span>
                            </a>
                        </li>
                    </ul>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <div class="mt-auto px-3 py-3 border-top border-secondary-subtle">
        <?php if ($user): ?>
            <div class="sidebar-user mb-3">
                <div class="fw-semibold">Hi, <?= htmlspecialchars($user['name']) ?></div>
                <div class="text-secondary small"><?= htmlspecialchars(ucfirst($user['role'])) ?></div>
            </div>
            <div class="d-flex gap-2">
                <button class="btn btn-outline-light btn-sm flex-grow-1" id="theme-toggle">🌓 Tema</button>
                <a class="btn btn-warning btn-sm" href="<?= $baseUrl ?>?route=logout">Logout</a>
            </div>
        <?php else: ?>
            <a class="btn btn-success w-100" href="<?= $baseUrl ?>?route=login">Login</a>
        <?php endif; ?>
    </div>
</aside>

