<?php
$scriptDir     = rtrim(str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'] ?? '/')), '/');
$savitBase     = dirname(dirname($scriptDir)); // /Savit  (two levels up from /Analytics/public)
$analyticsBase = $scriptDir;
$currentPath   = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?? '/';

// Shared sidebar variables
$activeEngine     = 'analytics';
$engineBadgeLabel = 'Analytics Engine';
$engineBadgeColor = 'rgba(167,139,250,.12);color:#7c3aed;';

$isAct = fn(string $p) => str_contains($currentPath, $p) ? true : false;
$engineSubnav = [
    [
        'label'  => 'Dashboard',
        'icon'   => 'speedometer2',
        'url'    => $analyticsBase . '/',
        'active' => rtrim($currentPath, '/') === rtrim($analyticsBase, '/'),
    ],
    [
        'id'     => 'nav-reports',
        'label'  => 'Reports',
        'icon'   => 'file-earmark-bar-graph',
        'url'    => $analyticsBase . '/report',
        'active' => $isAct('/report'),
    ],
    [
        'id'     => 'nav-ai-overview',
        'label'  => 'AI Overview',
        'icon'   => 'robot',
        'url'    => $analyticsBase . '/ai-overview/0',
        'active' => $isAct('/ai-overview'),
    ],
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="base-url" content="<?= htmlspecialchars($analyticsBase) ?>">
  <title><?= isset($pageTitle) ? htmlspecialchars($pageTitle) . ' — ' : '' ?>Savit Analytics</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=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
  <link rel="stylesheet" href="https://cdn.datatables.net/1.13.8/css/dataTables.bootstrap5.min.css">
  <link rel="stylesheet" href="<?= htmlspecialchars($savitBase) ?>/Common/assets/css/savit.css?v=4">
  <script src="<?= htmlspecialchars($savitBase) ?>/Common/assets/js/project-persistence.js" defer></script>
  <style>
    .health-dot { width:10px;height:10px;border-radius:50%;display:inline-block;flex-shrink:0; }
    .delta-pos  { color:#22c55e;font-size:.75rem; }
    .delta-neg  { color:#ef4444;font-size:.75rem; }
    .delta-neu  { color:#94a3b8;font-size:.75rem; }
  </style>
</head>
<body>
<div class="d-flex" id="app-wrapper">

  <?php require SAVIT_ROOT . '/Common/views/layout/sidebar.php'; ?>

  <!-- Main content -->
  <div class="flex-grow-1 d-flex flex-column min-vh-100" id="main-content">
    <!-- Topbar -->
    <div class="savit-topbar">
      <div class="topbar-left">
        <button class="topbar-toggle" id="sidebar-toggle">
          <i class="bi bi-list" style="font-size:18px;"></i>
        </button>
        <span class="topbar-title"><?= htmlspecialchars($pageTitle ?? 'Analytics') ?></span>
      </div>
      <div class="topbar-right">
        <span class="topbar-engine-badge" style="background:<?= $engineBadgeColor ?>">
          <i class="bi bi-bar-chart-line-fill me-1"></i><?= htmlspecialchars($engineBadgeLabel) ?>
        </span>
      </div>
    </div>
    <!-- Page content -->
    <div class="p-4 flex-grow-1">

<?php
// Update the Reports nav link to pre-select the last used project
$end   = date('Y-m-d');
$start = date('Y-m-d', strtotime('-30 days'));
?>
<script>
(function(){
  var stored = localStorage.getItem('savit_project');
  var proj   = stored ? JSON.parse(stored) : null;
  if (!proj) return;
  var base = <?= json_encode($analyticsBase) ?>;
  var end  = <?= json_encode($end) ?>;
  var start= <?= json_encode($start) ?>;
  var reportsLink = document.getElementById('nav-reports');
  if (reportsLink) {
    reportsLink.href = base + '/report?project=' + proj.id + '&start=' + start + '&end=' + end + '&audience=client';
  }
  var aiLink = document.getElementById('nav-ai-overview');
  if (aiLink) {
    aiLink.href = base + '/ai-overview/' + proj.id;
  }
})();
</script>
