<?php require __DIR__ . '/header.php'; ?>

<div class="app-shell">
    <aside class="sidebar">
        <div class="sidebar-brand">
            <span class="sidebar-logo">L</span>
            <div>
                <strong>Locative</strong>
                <small>Gestion immobiliere</small>
            </div>
        </div>
        <?php
            $currentPath = current_path();
            $operationsOpen = active_route('/properties') || active_route('/maintenance/property-statuses') || $currentPath === '/reservations' || active_route('/reservations/calendar') || active_route('/operations/arrivals-departures') || active_route('/clients');
            $financeOpen = active_route('/payments') || active_route('/expenses');
            $longRentalsOpen = active_route('/long-rentals');
            $reportsOpen = active_route('/statistics') || active_route('/reports') || active_route('/advanced-reports') || active_route('/exports');
            $adminOpen = active_route('/notifications') || active_route('/notification-settings') || active_route('/users') || active_route('/activity-logs') || active_route('/backup') || active_route('/settings') || active_route('/profile');
        ?>
        <div class="sidebar-title">Menu</div>
        <a class="sidebar-home <?= active_route('/dashboard') ?>" href="<?= url('/dashboard') ?>"><span>Dashboard</span></a>

        <?php if (Auth::can('properties.view') || Auth::can('reservations.view') || Auth::can('clients.view')): ?>
            <details class="sidebar-group" <?= $operationsOpen ? 'open' : '' ?>>
                <summary>Operations</summary>
                <div class="sidebar-subnav">
                    <?php if (Auth::can('properties.view')): ?>
                        <a class="<?= active_route('/properties') ?>" href="<?= url('/properties') ?>">Biens</a>
                    <?php endif; ?>
                    <?php if (Auth::can('properties.edit')): ?>
                        <a class="<?= active_route('/maintenance/property-statuses') ?>" href="<?= url('/maintenance/property-statuses') ?>">Statuts biens</a>
                    <?php endif; ?>
                    <?php if (Auth::can('reservations.view')): ?>
                        <a class="<?= $currentPath === '/reservations' ? 'active' : '' ?>" href="<?= url('/reservations') ?>">Reservations</a>
                        <a class="<?= active_route('/reservations/calendar') ?>" href="<?= url('/reservations/calendar') ?>">Planning</a>
                        <a class="<?= active_route('/operations/arrivals-departures') ?>" href="<?= url('/operations/arrivals-departures') ?>">Arrivees / Departs</a>
                    <?php endif; ?>
                    <?php if (Auth::can('clients.view')): ?>
                        <a class="<?= active_route('/clients') ?>" href="<?= url('/clients') ?>">Clients</a>
                    <?php endif; ?>
                </div>
            </details>
        <?php endif; ?>

        <?php if (Auth::can('payments.view') || Auth::can('expenses.view')): ?>
            <details class="sidebar-group" <?= $financeOpen ? 'open' : '' ?>>
                <summary>Finance</summary>
                <div class="sidebar-subnav">
                    <?php if (Auth::can('payments.view')): ?>
                        <a class="<?= active_route('/payments') ?>" href="<?= url('/payments') ?>">Paiements</a>
                    <?php endif; ?>
                    <?php if (Auth::can('expenses.view')): ?>
                        <a class="<?= active_route('/expenses') ?>" href="<?= url('/expenses') ?>">Depenses</a>
                    <?php endif; ?>
                </div>
            </details>
        <?php endif; ?>

        <?php if (Auth::can('long_rentals.view')): ?>
            <details class="sidebar-group" <?= $longRentalsOpen ? 'open' : '' ?>>
                <summary>Longue duree</summary>
                <div class="sidebar-subnav">
                    <a class="<?= $currentPath === '/long-rentals' ? 'active' : '' ?>" href="<?= url('/long-rentals') ?>">Vue generale</a>
                    <a class="<?= active_route('/long-rentals/properties') ?>" href="<?= url('/long-rentals/properties') ?>">Biens LD</a>
                    <a class="<?= active_route('/long-rentals/tenants') ?>" href="<?= url('/long-rentals/tenants') ?>">Locataires LD</a>
                    <a class="<?= active_route('/long-rentals/contracts') ?>" href="<?= url('/long-rentals/contracts') ?>">Contrats LD</a>
                    <a class="<?= active_route('/long-rentals/payments') ?>" href="<?= url('/long-rentals/payments') ?>">Loyers LD</a>
                    <a class="<?= active_route('/long-rentals/situations') ?>" href="<?= url('/long-rentals/situations') ?>">Situations LD</a>
                </div>
            </details>
        <?php endif; ?>

        <?php if (Auth::can('statistics.view')): ?>
            <details class="sidebar-group" <?= $reportsOpen ? 'open' : '' ?>>
                <summary>Rapports</summary>
                <div class="sidebar-subnav">
                    <a class="<?= active_route('/statistics') ?>" href="<?= url('/statistics') ?>">Statistiques</a>
                    <a class="<?= active_route('/reports') ?>" href="<?= url('/reports') ?>">Rapports</a>
                    <a class="<?= active_route('/advanced-reports') ?>" href="<?= url('/advanced-reports') ?>">Rapports avances</a>
                    <a class="<?= active_route('/exports') ?>" href="<?= url('/exports') ?>">Exports</a>
                </div>
            </details>
        <?php else: ?>
            <a class="<?= active_route('/exports') ?>" href="<?= url('/exports') ?>">Exports</a>
        <?php endif; ?>

        <details class="sidebar-group" <?= $adminOpen ? 'open' : '' ?>>
            <summary>Administration</summary>
            <div class="sidebar-subnav">
                <a class="<?= active_route('/profile') ?>" href="<?= url('/profile') ?>">Mon compte</a>
                <a class="<?= active_route('/notifications') ?>" href="<?= url('/notifications') ?>">Notifications</a>
                <?php if (Auth::can('users.view')): ?>
                    <a class="<?= active_route('/notification-settings') ?>" href="<?= url('/notification-settings') ?>">Alertes admin</a>
                    <a class="<?= active_route('/users') ?>" href="<?= url('/users') ?>">Utilisateurs</a>
                    <a class="<?= active_route('/activity-logs') ?>" href="<?= url('/activity-logs') ?>">Historique</a>
                    <a class="<?= active_route('/backup') ?>" href="<?= url('/backup') ?>">Backup</a>
                    <a class="<?= active_route('/settings') ?>" href="<?= url('/settings') ?>">Parametres</a>
                <?php endif; ?>
            </div>
        </details>

        <?php $sidebarUser = Auth::user(); ?>
        <a class="sidebar-user-card" href="<?= url('/profile') ?>">
            <span><?= e(strtoupper(substr((string) ($sidebarUser['name'] ?? 'U'), 0, 1))) ?></span>
            <div>
                <strong><?= e($sidebarUser['name'] ?? 'Utilisateur') ?></strong>
                <small><?= e($sidebarUser['email'] ?? '') ?></small>
            </div>
        </a>
        <a class="sidebar-logout" href="<?= url('/logout') ?>">Deconnexion</a>
    </aside>

    <div class="app-main">
        <?php if (current_path() !== '/dashboard'): ?>
            <header class="app-navbar">
                <span>Gestion locative</span>
                <div class="navbar-user">
                    <?php $navUser = Auth::user(); ?>
                    <a href="<?= url('/profile') ?>"><?= e($navUser['name'] ?? 'Utilisateur') ?></a>
                </div>
            </header>
        <?php endif; ?>

        <main class="content">
