<?php
$current  = '/' . trim(substr(parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH), strlen(BASE_URL)), '/');
$isActive = function(string $prefix) use ($current): string {
    return (str_starts_with($current, $prefix) || $current === $prefix) ? 'active' : '';
};
$_savitBase   = dirname(BASE_URL);            // /Savit
$auditUrl     = $_savitBase . '/Audit/public';
$analyticsUrl = $_savitBase . '/Analytics/public';
$ppcUrl       = $_savitBase . '/PPC/media_plan.php';
?>
<aside class="sidebar" id="sidebar">

  <!-- Brand -->
  <a href="<?= BASE_URL ?>/" 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($_savitBase) ?>/" class="sb-engine-link">
      <i class="bi bi-grid-3x3-gap"></i>
      <span>Dashboard</span>
    </a>
    <a href="<?= BASE_URL ?>/" class="sb-engine-link active">
      <i class="bi bi-lightning-fill"></i>
      <span>Content Engine</span>
      <span class="sb-active-dot"></span>
    </a>
    <a href="<?= htmlspecialchars($auditUrl) ?>"
       class="sb-engine-link" data-engine="audit" data-base="<?= htmlspecialchars($auditUrl) ?>">
      <i class="bi bi-search"></i><span>Audit Engine</span>
    </a>
    <a href="<?= htmlspecialchars($analyticsUrl) ?>/"
       class="sb-engine-link" id="content-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">

  <!-- Content Engine nav -->
  <div class="sidebar-section">Setup</div>
  <nav class="sidebar-nav">
    <a href="<?= BASE_URL ?>/projects" class="<?= $isActive('/projects') ?>">
      <i class="bi bi-briefcase"></i><span>Projects</span>
    </a>
    <a href="<?= BASE_URL ?>/page-types" class="<?= $isActive('/page-types') ?>">
      <i class="bi bi-file-earmark-richtext"></i><span>Page Types</span>
    </a>
  </nav>

  <div class="sidebar-section">Content</div>
  <nav class="sidebar-nav">
    <a href="<?= BASE_URL ?>/topics"
       class="<?= ($isActive('/topics') && !str_starts_with($current, '/topics/seed') && !str_starts_with($current, '/topics/bulk')) ? 'active' : '' ?>">
      <i class="bi bi-tags"></i><span>Topics</span>
    </a>
    <a href="<?= BASE_URL ?>/topics/seed-generate" class="<?= $isActive('/topics/seed-generate') ?>">
      <i class="bi bi-stars"></i><span>AI Topics Generator</span>
    </a>
    <a href="<?= BASE_URL ?>/research" class="<?= $isActive('/research') ?>">
      <i class="bi bi-search-heart"></i><span>AI Prompt Research</span>
    </a>
    <a href="<?= BASE_URL ?>/content/generate" class="<?= $isActive('/content/generate') ?>">
      <i class="bi bi-lightning"></i><span>Generate</span>
    </a>
    <a href="<?= BASE_URL ?>/content"
       class="<?= ($isActive('/content') && !str_starts_with($current, '/content/generate')) ? 'active' : '' ?>">
      <i class="bi bi-collection"></i><span>Content Library</span>
    </a>
    <a href="<?= BASE_URL ?>/image-optimization" class="<?= $isActive('/image-optimization') ?>">
      <i class="bi bi-images"></i><span>Image Optimization</span>
    </a>
  </nav>

  <!-- Footer -->
  <div class="sidebar-footer">
    <small>Savit v1.0.0 &nbsp;&middot;&nbsp; Content 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>
