<?php
/**
 * Savit Unified Sidebar — included by every engine header.
 *
 * Required variables (set by each engine's header before including this):
 *   $savitBase     — URL to Savit root    e.g. /Savit
 *   $activeEngine  — 'dashboard' | 'audit' | 'analytics' | 'content' | 'ppc'
 *   $engineSubnav  — array of ['label', 'icon', 'url', 'id'?] for this engine's pages
 *
 * Optional:
 *   $pageTitle     — page title for topbar
 *   $engineBadgeLabel  — e.g. 'Audit Engine'
 *   $engineBadgeColor  — inline CSS color pair e.g. 'rgba(8,145,178,.1);color:#0891b2;'
 */

$auditBase     = $savitBase . '/Audit/public';
$analyticsBase = $savitBase . '/Analytics/public';
$contentBase   = $savitBase . '/Content';
$ppcBase       = $savitBase . '/PPC/media_plan.php';

$engines = [
    ['key' => 'dashboard',  'label' => 'Dashboard',      'icon' => 'grid-3x3-gap-fill',    'url' => $savitBase . '/',    'color' => '#6366f1', 'dot' => '#818cf8'],
    ['key' => 'content',    'label' => 'Content Engine', 'icon' => 'lightning-fill',        'url' => $contentBase . '/',  'color' => '#059669', 'dot' => '#34d399', 'data' => ['engine' => 'content', 'base' => $contentBase]],
    ['key' => 'audit',      'label' => 'Audit Engine',   'icon' => 'search',                'url' => $auditBase . '/',    'color' => '#0891b2', 'dot' => '#67e8f9',  'data' => ['engine' => 'audit',    'base' => $auditBase]],
    ['key' => 'analytics',  'label' => 'Analytics',      'icon' => 'bar-chart-line-fill',   'url' => $analyticsBase .'/', 'color' => '#7c3aed', 'dot' => '#a78bfa', 'data' => ['id' => 'nav-analytics-link', 'engine' => 'analytics', 'base' => $analyticsBase]],
    ['key' => 'ppc',        'label' => 'PPC Engine',     'icon' => 'bar-chart-fill',        'url' => $ppcBase,            'color' => '#d97706', 'dot' => '#fbbf24'],
];
?>
<aside class="sidebar" id="sidebar">

  <!-- Brand -->
  <a href="<?= htmlspecialchars($savitBase) ?>/" class="sidebar-brand">
    <div class="sb-logo">S</div>
    <div>
      <div class="logo-text">Savit</div>
      <div class="logo-sub">Unified SEO Suite</div>
    </div>
  </a>

  <!-- Engine Switcher -->
  <div class="sidebar-section">Engines</div>
  <nav class="sidebar-nav">
    <?php foreach ($engines as $eng):
      $isActive = ($activeEngine ?? '') === $eng['key'];
      $extras   = '';
      foreach ($eng['data'] ?? [] as $attr => $val) {
        $extras .= ' data-' . $attr . '="' . htmlspecialchars($val) . '"';
      }
    ?>
    <a href="<?= htmlspecialchars($eng['url']) ?>"
       class="sb-engine-link <?= $isActive ? 'active' : '' ?>"
       <?= $extras ?>
       <?= isset($eng['data']['id']) ? 'id="' . $eng['data']['id'] . '"' : '' ?>>
      <i class="bi bi-<?= $eng['icon'] ?>"></i>
      <span><?= htmlspecialchars($eng['label']) ?></span>
      <?php if ($isActive): ?>
        <span class="sb-active-dot" style="background:<?= $eng['dot'] ?>;"></span>
      <?php endif; ?>
    </a>
    <?php endforeach; ?>
  </nav>

  <hr class="sidebar-divider">

  <!-- Engine-specific nav (injected by each engine) -->
  <?php if (!empty($engineSubnav)): ?>
  <div class="sidebar-section"><?= htmlspecialchars($engineBadgeLabel ?? ucfirst($activeEngine ?? 'Navigation')) ?></div>
  <nav class="sidebar-nav">
    <?php foreach ($engineSubnav as $item):
      $itemId = isset($item['id']) ? 'id="' . htmlspecialchars($item['id']) . '"' : '';
    ?>
    <a href="<?= htmlspecialchars($item['url']) ?>"
       <?= $itemId ?>
       class="<?= ($item['active'] ?? false) ? 'active' : '' ?>">
      <i class="bi bi-<?= htmlspecialchars($item['icon']) ?>"></i>
      <span><?= htmlspecialchars($item['label']) ?></span>
    </a>
    <?php endforeach; ?>
  </nav>
  <?php endif; ?>

  <!-- Footer -->
  <div class="sidebar-footer">
    <small>Savit v1.0.0 &middot; <?= htmlspecialchars($engineBadgeLabel ?? 'Suite') ?></small>
  </div>

</aside>

<!-- Project persistence + cross-engine deep-link JS -->
<script>
document.addEventListener('DOMContentLoaded', function () {
  try {
    var proj = JSON.parse(localStorage.getItem('savit_project') || 'null');
    if (!proj || !proj.id) return;
    var end   = new Date().toISOString().split('T')[0];
    var start = new Date(Date.now() - 30 * 864e5).toISOString().split('T')[0];

    document.querySelectorAll('[data-engine]').forEach(function (el) {
      var base = el.dataset.base || '';
      switch (el.dataset.engine) {
        case 'audit':     el.href = base + '/?p=' + proj.id; break;
        case 'analytics': el.href = base + '/report?project=' + proj.id + '&start=' + start + '&end=' + end + '&audience=client'; break;
        case 'content':   el.href = base + '/projects/' + proj.id + '/edit'; break;
      }
    });
  } catch (_) {}
});
</script>
