<?php

declare(strict_types=1);

/** @var string $pageTitle */
/** @var string $currentNav */

$user = current_user();
$initial = strtoupper(substr($user['nombre'] ?? '?', 0, 1));
$perfilTxt = implode(', ', $user['perfiles'] ?? []);

$fa = static function (string $class, string $label = ''): string {
    $aria = $label !== '' ? ' aria-label="' . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . '"' : ' aria-hidden="true"';
    return '<i class="fa-solid ' . htmlspecialchars($class, ENT_QUOTES, 'UTF-8') . '"' . $aria . '></i>';
};

$navItems = [
    [
        'id' => 'dashboard',
        'label' => 'Panel',
        'href' => url('dashboard.php'),
        'icon' => $fa('fa-house', ''),
    ],
    [
        'id' => 'campeonatos',
        'label' => 'Campeonatos',
        'href' => url('pages/campeonatos.php'),
        'icon' => $fa('fa-trophy', ''),
    ],
    [
        'id' => 'equipos',
        'label' => 'Equipos',
        'href' => url('pages/equipos.php'),
        'icon' => $fa('fa-users', ''),
    ],
    [
        'id' => 'inscripciones',
        'label' => 'Inscripciones',
        'href' => url('pages/inscripciones.php'),
        'icon' => $fa('fa-clipboard-list', ''),
    ],
    [
        'id' => 'partidos',
        'label' => 'Partidos',
        'href' => url('pages/partidos.php'),
        'icon' => $fa('fa-calendar-days', ''),
    ],
    [
        'id' => 'posiciones',
        'label' => 'Tabla de posiciones',
        'href' => url('pages/posiciones.php'),
        'icon' => $fa('fa-list-ol', ''),
    ],
    [
        'id' => 'seguimiento',
        'label' => 'Seguimiento',
        'href' => url('pages/Seguimiento.php'),
        'icon' => $fa('fa-book-open-reader', ''),
    ],
    [
        'id' => 'jugadores',
        'label' => 'Jugadores',
        'href' => url('pages/jugadores.php'),
        'icon' => $fa('fa-user', ''),
    ],
    [
        'id' => 'plantilla',
        'label' => 'Plantillas',
        'href' => url('pages/plantilla.php'),
        'icon' => $fa('fa-users-between-lines', ''),
    ],
    [
        'id' => 'tarjetas',
        'label' => 'Tarjetas',
        'href' => url('pages/tarjetas.php'),
        'icon' => $fa('fa-id-card', ''),
    ],
    [
        'id' => 'estadios',
        'label' => 'Estadios',
        'href' => url('pages/estadios.php'),
        'icon' => $fa('fa-building-columns', ''),
    ],
];

if (can_manage_users()) {
    $navItems[] = [
        'id' => 'usuarios',
        'label' => 'Usuarios',
        'href' => url('pages/usuarios.php'),
        'icon' => $fa('fa-user-shield', ''),
    ];
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title><?= h($pageTitle) ?> · <?= h(APP_NAME) ?></title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" crossorigin="anonymous" referrerpolicy="no-referrer">
  <link rel="stylesheet" href="<?= h(url('assets/css/app.css')) ?>">
</head>
<body>
  <div class="sidebar-backdrop" aria-hidden="true"></div>
  <div class="app-shell">
    <aside class="sidebar" id="sidebar">
      <div class="sidebar-brand">
        <a href="<?= h(url('dashboard.php')) ?>">
          <span class="brand-icon">⚽</span>
          <div>
            <?= h(APP_NAME) ?>
            <span>Gestión deportiva</span>
          </div>
        </a>
      </div>
      <nav class="nav-section" aria-label="Principal">
        <div class="nav-label">Menú</div>
        <?php foreach ($navItems as $item) :
            $active = ($currentNav === $item['id']) ? ' active' : '';
            ?>
          <a class="nav-link<?= $active ?>" href="<?= h($item['href']) ?>">
            <?= $item['icon'] ?>
            <?= h($item['label']) ?>
          </a>
        <?php endforeach; ?>
      </nav>
      <div class="sidebar-footer">
        Sesión segura · <?= h(date('Y')) ?>
      </div>
    </aside>
    <div class="main-wrap">
      <header class="topbar">
        <button type="button" class="menu-toggle" aria-label="Abrir menú">
          <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M4 6h16M4 12h16M4 18h16"/></svg>
        </button>
        <h1 class="page-title"><?= h($pageTitle) ?></h1>
        <div class="user-pill">
          <div class="user-avatar" aria-hidden="true"><?= h($initial) ?></div>
          <div class="user-meta">
            <strong><?= h(trim(($user['nombre'] ?? '') . ' ' . ($user['apellidos'] ?? ''))) ?></strong>
            <small title="<?= h($perfilTxt) ?>"><?= h($user['email'] ?? '') ?></small>
          </div>
          <a class="btn btn-ghost" href="<?= h(url('logout.php')) ?>" style="padding:0.35rem 0.75rem;font-size:0.8rem;">Salir</a>
        </div>
      </header>
      <main class="content">
