<?php
require_once JLFC_ROOT . '/app/core/auth.php';

$user = current_user();
$navigation = [
    [
        'label' => 'Dashboard',
        'icon' => 'bi-grid-1x2-fill',
        'path' => 'dashboard.php',
        'active_paths' => ['dashboard.php'],
        'permission' => 'view_dashboard',
    ],
    [
        'label' => 'Users',
        'icon' => 'bi-people-fill',
        'path' => 'app/modules/users/index.php',
        'active_paths' => [
            'app/modules/users/index.php',
            'app/modules/users/create.php',
            'app/modules/users/edit.php',
            'app/modules/users/view.php',
            'app/modules/users/delete.php',
        ],
        'permission' => 'manage_users',
    ],
    [
        'label' => 'Roles',
        'icon' => 'bi-shield-lock-fill',
        'path' => 'app/modules/roles/index.php',
        'active_paths' => [
            'app/modules/roles/index.php',
            'app/modules/roles/create.php',
            'app/modules/roles/edit.php',
            'app/modules/roles/view.php',
            'app/modules/roles/delete.php',
        ],
        'permission' => 'manage_roles',
    ],
    [
        'label' => 'Seasons',
        'icon' => 'bi-calendar-range-fill',
        'path' => 'app/modules/seasons/index.php',
        'active_paths' => [
            'app/modules/seasons/index.php',
            'app/modules/seasons/create.php',
            'app/modules/seasons/edit.php',
            'app/modules/seasons/view.php',
            'app/modules/seasons/delete.php',
        ],
        'permission' => 'manage_seasons',
    ],
    [
        'label' => 'Competitions',
        'icon' => 'bi-trophy-fill',
        'path' => 'app/modules/competitions/index.php',
        'active_paths' => [
            'app/modules/competitions/index.php',
            'app/modules/competitions/create.php',
            'app/modules/competitions/edit.php',
            'app/modules/competitions/view.php',
            'app/modules/competitions/delete.php',
        ],
        'permission' => 'manage_competitions',
    ],
    [
        'label' => 'Clubs',
        'icon' => 'bi-shield-fill-check',
        'path' => 'app/modules/clubs/index.php',
        'active_paths' => [
            'app/modules/clubs/index.php',
            'app/modules/clubs/create.php',
            'app/modules/clubs/edit.php',
            'app/modules/clubs/view.php',
            'app/modules/clubs/delete.php',
        ],
        'permission' => 'manage_clubs',
    ],
    [
        'label' => 'Club Staff',
        'icon' => 'bi-person-badge-fill',
        'path' => 'app/modules/staff/index.php',
        'active_paths' => [
            'app/modules/staff/index.php',
            'app/modules/staff/create.php',
            'app/modules/staff/edit.php',
            'app/modules/staff/view.php',
            'app/modules/staff/delete.php',
        ],
        'permission' => 'manage_club_staff',
    ],
    [
        'label' => 'Players',
        'icon' => 'bi-people-fill',
        'path' => 'app/modules/players/index.php',
        'active_paths' => [
            'app/modules/players/index.php',
            'app/modules/players/create.php',
            'app/modules/players/bulk.php',
            'app/modules/players/template.php',
            'app/modules/players/edit.php',
            'app/modules/players/view.php',
            'app/modules/players/delete.php',
        ],
        'permission' => 'manage_players',
    ],
    [
        'label' => 'Licences',
        'icon' => 'bi-person-vcard-fill',
        'path' => 'app/modules/licenses/index.php',
        'active_paths' => [
            'app/modules/licenses/index.php',
            'app/modules/licenses/view.php',
            'app/modules/licenses/print.php',
            'app/modules/licenses/actions.php',
        ],
        'permission' => 'manage_player_licenses',
    ],
    [
        'label' => 'Licence Verify',
        'icon' => 'bi-patch-check-fill',
        'path' => 'app/modules/licenses/verify.php',
        'active_paths' => [
            'app/modules/licenses/verify.php',
            'app/modules/licenses/actions.php',
        ],
        'permission' => 'verify_player_licenses',
    ],
    [
        'label' => 'Venues',
        'icon' => 'bi-building-fill-check',
        'path' => 'app/modules/venues/index.php',
        'active_paths' => [
            'app/modules/venues/index.php',
            'app/modules/venues/create.php',
            'app/modules/venues/edit.php',
            'app/modules/venues/view.php',
            'app/modules/venues/delete.php',
        ],
        'permission' => 'manage_venues',
    ],
    [
        'label' => 'Referees',
        'icon' => 'bi-whistle',
        'path' => 'app/modules/referees/index.php',
        'active_paths' => [
            'app/modules/referees/index.php',
            'app/modules/referees/create.php',
            'app/modules/referees/edit.php',
            'app/modules/referees/view.php',
            'app/modules/referees/delete.php',
        ],
        'permission' => 'manage_referees',
    ],
    [
        'label' => 'Fixtures',
        'icon' => 'bi-calendar-event-fill',
        'path' => 'app/modules/fixtures/index.php',
        'active_paths' => [
            'app/modules/fixtures/index.php',
            'app/modules/fixtures/create.php',
            'app/modules/fixtures/edit.php',
            'app/modules/fixtures/view.php',
            'app/modules/fixtures/delete.php',
            'app/modules/fixtures/generate.php',
        ],
        'permission' => 'manage_fixtures',
    ],
    [
        'label' => 'Match Prep',
        'icon' => 'bi-diagram-3-fill',
        'path' => 'app/modules/fixtures/preparation.php',
        'active_paths' => [
            'app/modules/fixtures/preparation.php',
            'app/modules/fixtures/preparation-actions.php',
        ],
        'permission' => 'manage_match_preparation',
    ],
    [
        'label' => 'Match Results',
        'icon' => 'bi-clipboard-data-fill',
        'path' => 'app/modules/matches/index.php',
        'active_paths' => [
            'app/modules/matches/index.php',
            'app/modules/matches/view.php',
        ],
        'permission' => 'submit_results',
    ],
    [
        'label' => 'League Table',
        'icon' => 'bi-table',
        'path' => 'app/modules/table/index.php',
        'active_paths' => [
            'app/modules/table/index.php',
        ],
        'permission' => 'manage_table',
    ],
    [
        'label' => 'Statistics',
        'icon' => 'bi-graph-up-arrow',
        'path' => 'app/modules/statistics/index.php',
        'active_paths' => [
            'app/modules/statistics/index.php',
        ],
        'permission' => 'manage_statistics',
    ],
    [
        'label' => 'Discipline',
        'icon' => 'bi-shield-exclamation',
        'path' => 'app/modules/discipline/index.php',
        'active_paths' => [
            'app/modules/discipline/index.php',
            'app/modules/discipline/suspension.php',
            'app/modules/discipline/case.php',
        ],
        'permission' => 'manage_discipline',
    ],
    [
        'label' => 'Protests',
        'icon' => 'bi-megaphone-fill',
        'path' => 'app/modules/protests/index.php',
        'active_paths' => [
            'app/modules/protests/index.php',
            'app/modules/protests/create.php',
            'app/modules/protests/edit.php',
            'app/modules/protests/view.php',
            'app/modules/protests/delete.php',
        ],
        'permission' => 'manage_protests',
    ],
    [
        'label' => 'Finance',
        'icon' => 'bi-cash-coin',
        'path' => 'app/modules/finance/index.php',
        'active_paths' => [
            'app/modules/finance/index.php',
            'app/modules/finance/create.php',
            'app/modules/finance/edit.php',
            'app/modules/finance/view.php',
            'app/modules/finance/delete.php',
        ],
        'permission' => 'manage_finance',
    ],
    [
        'label' => 'Awards',
        'icon' => 'bi-trophy',
        'path' => 'app/modules/awards/index.php',
        'active_paths' => [
            'app/modules/awards/index.php',
            'app/modules/awards/create.php',
            'app/modules/awards/edit.php',
            'app/modules/awards/view.php',
            'app/modules/awards/delete.php',
        ],
        'permission' => 'manage_awards',
    ],
    [
        'label' => 'Media',
        'icon' => 'bi-camera-reels',
        'path' => 'app/modules/media/index.php',
        'active_paths' => [
            'app/modules/media/index.php',
            'app/modules/media/create-news.php',
            'app/modules/media/edit-news.php',
            'app/modules/media/view-news.php',
            'app/modules/media/delete-news.php',
            'app/modules/media/create-gallery.php',
            'app/modules/media/edit-gallery.php',
            'app/modules/media/view-gallery.php',
            'app/modules/media/delete-gallery.php',
        ],
        'permission' => 'manage_media',
    ],
    [
        'label' => 'Transfers',
        'icon' => 'bi-arrow-left-right',
        'path' => 'app/modules/transfers/index.php',
        'active_paths' => [
            'app/modules/transfers/index.php',
            'app/modules/transfers/create-interest.php',
            'app/modules/transfers/view.php',
            'app/modules/transfers/windows-report.php',
        ],
        'permission' => 'manage_transfers',
    ],
    [
        'label' => 'Reports',
        'icon' => 'bi-file-earmark-bar-graph',
        'path' => 'app/modules/reports/index.php',
        'active_paths' => [
            'app/modules/reports/index.php',
            'app/modules/reports/export.php',
        ],
        'permission' => 'view_reports',
    ],
    [
        'label' => 'Analytics',
        'icon' => 'bi-activity',
        'path' => 'app/modules/analytics/index.php',
        'active_paths' => [
            'app/modules/analytics/index.php',
        ],
        'permission' => 'view_dashboard',
        'super_admin_only' => true,
    ],
    [
        'label' => 'Settings',
        'icon' => 'bi-sliders',
        'path' => 'app/modules/settings/index.php',
        'active_paths' => [
            'app/modules/settings/index.php',
            'app/modules/settings/actions.php',
        ],
        'permission' => 'manage_settings',
    ],
];
?>
<aside class="sidebar-panel" id="sidebarPanel">
    <div class="sidebar-brand">
        <div class="brand-mark <?php echo league_logo_path() !== null ? 'brand-mark-image' : ''; ?>">
            <?php if (league_logo_path() !== null): ?>
                <img src="<?php echo e(base_url((string) league_logo_path())); ?>" alt="<?php echo e(league_short_name()); ?> logo">
            <?php else: ?>
                JL
            <?php endif; ?>
        </div>
        <div>
            <span class="brand-name"><?php echo e(league_short_name()); ?></span>
            <span class="brand-subtitle">League Engine</span>
        </div>
    </div>

    <div class="sidebar-section-label">Core</div>
    <nav class="nav flex-column sidebar-nav">
        <?php foreach ($navigation as $item): ?>
            <?php if ((!($item['super_admin_only'] ?? false) || is_super_admin()) && can($item['permission'])): ?>
                <a class="sidebar-link <?php echo e(is_active_route($item['active_paths'] ?? $item['path'])); ?>" href="<?php echo e(base_url($item['path'])); ?>">
                    <i class="bi <?php echo e($item['icon']); ?>"></i>
                    <span><?php echo e($item['label']); ?></span>
                </a>
            <?php endif; ?>
        <?php endforeach; ?>
    </nav>

    <div class="sidebar-card mt-auto">
        <span class="sidebar-card-label">Signed in as</span>
        <strong><?php echo e($user['full_name'] ?? 'User'); ?></strong>
        <span class="text-white-50 small"><?php echo e($user['role_name'] ?? 'Staff'); ?></span>
        <a class="btn btn-outline-light btn-sm mt-3" href="<?php echo e(base_url('app/public/index.php')); ?>">
            <i class="bi bi-globe2"></i>
            Public site
        </a>
    </div>
</aside>
<div class="admin-main">