<?php
$uri = $_SERVER['REQUEST_URI'] ?? '';
$isRootNotifs = strpos($uri, '/notifications.php') !== false;
if (strpos($uri, '/admin/') !== false || strpos($uri, '/customer/') !== false || ($isRootNotifs && isset($_SESSION['user']))) {
  echo '</div></div>'; // close .content and .layout opened in header
}

// Email queue processing is now handled via background JS to prevent blocking page load

// Basic FAQ schema for homepage
if ($uri === '/' || $uri === '/index.php') {
  $faq = [
    "@context" => "https://schema.org",
    "@type" => "FAQPage",
    "mainEntity" => [
      [
        "@type" => "Question",
        "name" => "What is CAC and why should I register my business?",
        "acceptedAnswer" => [
          "@type" => "Answer",
          "text" => "The Corporate Affairs Commission (CAC) is Nigeria’s corporate registry. Registration gives legal recognition and credibility. Go Legal handles the end‑to‑end process accurately."
        ]
      ],
      [
        "@type" => "Question",
        "name" => "How much does it cost to register a business in Nigeria?",
        "acceptedAnswer" => [
          "@type" => "Answer",
          "text" => "Costs vary by Business Name, Limited Company, or NGO. Go Legal offers structured, all‑inclusive bundles that cover CAC fees and professional handling."
        ]
      ],
      [
        "@type" => "Question",
        "name" => "How long does business registration take?",
        "acceptedAnswer" => [
          "@type" => "Answer",
          "text" => "Most registrations finish within a few working days when filed correctly. We reduce delays by preventing name rejections and submission errors."
        ]
      ],
      [
        "@type" => "Question",
        "name" => "What documents are required?",
        "acceptedAnswer" => [
          "@type" => "Answer",
          "text" => "Valid ID, passport photo, business address; for companies, director/shareholder details. We provide a clear checklist tailored to your entity."
        ]
      ]
    ]
  ];
  echo '<script type="application/ld+json">'.json_encode($faq, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES).'</script>';
}
?>
</main>
<footer class="container" style="padding:48px 0; border-top: 1px solid var(--border); margin-top: 64px;">
  <div style="display:flex; align-items:center; justify-content:space-between; flex-wrap: wrap; gap: 20px;">
    <div style="display: flex; flex-direction: column; gap: 4px;">
        <span style="font-weight: 700; color: var(--text); font-size: 18px;">Go Legal</span>
        <span style="font-size: 13px; color: var(--muted);">© <?=date('Y')?> Go Legal. All rights reserved.</span>
        <span style="font-size: 13px; color: var(--muted);">A product of <a href="https://zutaa.com.ng/" style="color:var(--primary);text-decoration:none;">Zutaa Technologies Limited</a></span>
    </div>
    <div style="display: flex; gap: 24px; align-items: center;">
        <a href="<?=url('services.php')?>" style="text-decoration: none; color: var(--muted); font-size: 14px; font-weight: 500;">Services</a>
        <a href="<?=url('blog')?>" style="text-decoration: none; color: var(--muted); font-size: 14px; font-weight: 500;">Blog</a>
        <a href="<?=url('privacy.php')?>" style="text-decoration: none; color: var(--muted); font-size: 14px; font-weight: 500;">Privacy</a>
        <button id="install-app" class="btn secondary" style="display:none; padding: 6px 12px; font-size: 12px; border-radius: 8px;">Install App</button>
    </div>
  </div>
</footer>

<script>
(function(){
  var els = document.querySelectorAll('.reveal');
  if ('IntersectionObserver' in window) {
    var io = new IntersectionObserver(function(entries){
      entries.forEach(function(e){ if(e.isIntersecting){ e.target.classList.add('in'); io.unobserve(e.target);} });
    },{threshold:0.15});
    els.forEach(function(n){ io.observe(n); });
    // Pause testimonials columns when out of view to save CPU
    var cols = document.querySelectorAll('.tcol');
    var io2 = new IntersectionObserver(function(entries){
      entries.forEach(function(e){
        e.target.style.animationPlayState = e.isIntersecting ? 'running' : 'paused';
      });
    },{threshold:0.1});
    cols.forEach(function(c){ io2.observe(c); });
  } else {
    els.forEach(function(n){ n.classList.add('in'); });
  }
})();
// PWA install and service worker
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('<?=url('sw.js')?>');
}
let deferredPrompt=null;
window.addEventListener('beforeinstallprompt', (e)=>{
  e.preventDefault(); deferredPrompt = e;
  var btn = document.getElementById('install-app'); if(btn) btn.style.display='inline-flex';
});
document.getElementById('install-app')?.addEventListener('click', async function(){
  if (!deferredPrompt) return;
  deferredPrompt.prompt();
  const res = await deferredPrompt.userChoice;
  deferredPrompt = null;
  this.style.display='none';
});
// Request notification permission (optional)
if ('Notification' in window && Notification.permission==='default'){
  // avoid nagging; request on idle
  setTimeout(()=>{ Notification.requestPermission(); }, 3000);
}
// Fallback show for Android Chrome even if event hasn't fired yet
(function(){
  const btn = document.getElementById('install-app');
  if (!btn) return;
  const ua = navigator.userAgent || '';
  const isAndroidChrome = /Android/i.test(ua) && /Chrome/i.test(ua);
  const isStandalone = window.matchMedia('(display-mode: standalone)').matches || window.navigator.standalone;
  if (isAndroidChrome && location.protocol==='http:' || location.protocol==='https:') {
    if (!isStandalone) btn.style.display = 'inline-flex';
  }
  btn.addEventListener('click', function(){
    if (!deferredPrompt) {
      alert('To install, open the browser menu ⋮ and tap "Add to Home screen".');
    }
  });
})();
// In-app notifications via SSE (basic)
(function(){
  <?php
    $uri = $_SERVER['REQUEST_URI'] ?? '';
    $isAdminArea = strpos($uri, '/admin/') !== false;
    $isCustomerArea = strpos($uri, '/customer/') !== false;
  ?>
  <?php if (!empty($_SESSION['user']) && $isCustomerArea): ?>
  if (!!window.EventSource) {
    var lastId = 0;
    function listen(){
      var es = new EventSource('<?=url('api/notifications_sse.php')?>?last_id='+lastId);
      es.onmessage = function(e){
        try {
          var n = JSON.parse(e.data || '{}'); lastId = n.id || lastId;
          var toast = document.createElement('div');
          toast.className = 'card'; toast.style.position='fixed'; toast.style.right='16px'; toast.style.bottom='16px'; toast.style.zIndex='9999'; toast.style.maxWidth='320px';
          toast.innerHTML = '<strong>'+ (n.title || 'Notification') +'</strong><p class="muted" style="margin:6px 0 0">'+ (n.body || '') +'</p>';
          document.body.appendChild(toast); setTimeout(()=>{ toast.remove(); }, 5000);
          if ('Notification' in window && Notification.permission==='granted'){ new Notification(n.title || 'Notification', { body: n.body || '' }); }
        } catch(_) {}
      };
      es.onerror = function(){ es.close(); setTimeout(listen, 3000); };
    }
    listen();
  }
  <?php endif; ?>
  
  // Background email processing heartbeat
  setTimeout(() => {
    fetch('<?=url('api/process_queue.php')?>').catch(() => {});
  }, 2000); // Wait 2 seconds after load
})();
</script>

<?php include __DIR__ . '/chat_widget.php'; ?>
</body>
</html>
