<?php
// /esp32/dashboard/header.php
// Disclaimer: This example code is part of a research prototype.
// It is provided “as is”, without any warranty of any kind.
// Please review, adapt, and test carefully before using it in production.

require_once __DIR__ . '/auth.php';

// Navigation: list of all sites.
$sites = pdo()
    ->query('SELECT id, name, slug, background_path FROM sites ORDER BY name')
    ->fetchAll();

// $activeSite can be provided by the including script (e.g. site.php).
$activeSite = $activeSite ?? null;
$bg = $activeSite['background_path'] ?? null;
?><!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title><?= htmlspecialchars($page_title ?? 'ESP32 Sensor Dashboard') ?></title>
  <link rel="stylesheet" href="<?= BASE_URL ?>/assets/css/app.css?v=3">
  <script>
    // Expose BASE_URL so front-end scripts can build API URLs.
    window.BASE_URL = "<?= BASE_URL ?>";
  </script>
  <script src="https://cdn.jsdelivr.net/npm/chart.js@4"></script>
  <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns@3"></script>
</head>
<body class="<?= $bg ? 'has-bg' : '' ?>">
<header class="topbar">
  <div class="left">
    <a class="brand" href="<?= BASE_URL ?>/">
      <span class="brand__title">ESP32 Dashboard</span>
    </a>
    <nav class="nav-sites">
      <?php foreach ($sites as $s): ?>
        <a class="nav-sites__item <?= ($activeSite && $activeSite['id'] == $s['id']) ? 'is-active' : '' ?>"
           href="<?= BASE_URL ?>/site.php?site=<?= urlencode($s['slug']) ?>">
          <?= htmlspecialchars($s['name']) ?>
        </a>
      <?php endforeach; ?>
    </nav>
  </div>
  <div class="right">
    <?php if (current_user()): ?>
      <span class="user"><?= htmlspecialchars(current_user()['email']) ?></span>
      <?php if (current_user()['role'] === 'admin'): ?>
        <a class="btn ghost" href="<?= BASE_URL ?>/admin/site_edit.php">Admin</a>
      <?php endif; ?>
      <a class="btn" href="<?= BASE_URL ?>/logout.php">Logout</a>
    <?php endif; ?>
  </div>
</header>
<main class="page">
