<?php
// Re-use basePath computed in header.php (already in scope from require chain)
$currentPath = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?? '/';
$isActive    = function(string $prefix) use ($currentPath): string {
    return str_starts_with($currentPath, $prefix) ? 'active' : '';
};

// Cross-engine URLs derived from Savit root
$contentUrl    = $savitBase . '/Content';                    // Content Engine home
$auditUrl      = $basePath;                                  // This app (Audit Engine)
$analyticsUrl  = $savitBase . '/Analytics/public';           // Analytics Engine
$ppcUrl        = $savitBase . '/PPC/media_plan.php';         // PPC Engine
?>
<aside class="sidebar" id="sidebar">

  <!-- Brand -->
  <a href="<?= htmlspecialchars($contentUrl) ?>/" 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">
    <a href="<?= htmlspecialchars(dirname(dirname($basePath))) ?>/" class="sb-engine-link">
      <i class="bi bi-grid-3x3-gap"></i>
      <span>Dashboard</span>
    </a>
    <a href="<?= htmlspecialchars($contentUrl) ?>/"
       class="sb-engine-link" data-engine="content" data-base="<?= htmlspecialchars($contentUrl) ?>">
      <i class="bi bi-lightning-fill"></i><span>Content Engine</span>
    </a>
    <a href="<?= htmlspecialchars($auditUrl) ?>" class="sb-engine-link active">
      <i class="bi bi-search"></i><span>Audit Engine</span>
      <span class="sb-active-dot" style="background:#67e8f9;"></span>
    </a>
    <a href="<?= htmlspecialchars($analyticsUrl) ?>/"
       class="sb-engine-link" id="audit-analytics-link"
       data-engine="analytics" data-base="<?= htmlspecialchars($analyticsUrl) ?>">
      <i class="bi bi-bar-chart-line-fill"></i><span>Analytics</span>
    </a>
    <a href="<?= htmlspecialchars($ppcUrl) ?>" class="sb-engine-link">
      <i class="bi bi-bar-chart-fill"></i><span>PPC Engine</span>
    </a>
  </nav>

  <hr class="sidebar-divider">

  <!-- Audit Engine nav -->
  <div class="sidebar-section">Audit</div>
  <nav class="sidebar-nav">
    <a href="<?= htmlspecialchars($auditUrl) ?>/projects"
       class="<?= str_ends_with($currentPath, '/projects') ? 'active' : '' ?>">
      <i class="bi bi-grid-3x3-gap"></i><span>Health Board</span>
    </a>
    <a href="<?= htmlspecialchars($auditUrl) ?>"
       class="<?= ($currentPath === $basePath || $currentPath === $basePath . '/') ? 'active' : '' ?>">
      <i class="bi bi-globe2"></i><span>Website Audit</span>
    </a>
  </nav>

  <!-- Footer -->
  <div class="sidebar-footer">
    <small>Savit v1.0.0 &nbsp;&middot;&nbsp; Audit Engine</small>
  </div>

</aside>
<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>
