<?php
$u = current_user();
$unreadMsgs = 0;
if ($u && isset($db)) {
  try {
    $row = $db->one('SELECT COUNT(*) c FROM messages WHERE receiver_id=? AND is_read=0', [$u['id']]);
    $unreadMsgs = (int)($row['c'] ?? 0);
  } catch (Throwable $e) { $unreadMsgs = 0; }
}
$pathNow = trim(parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH), '/');
$base = trim(parse_url($config['app_url'] ?? '', PHP_URL_PATH), '/');
if ($base && str_starts_with($pathNow, $base)) {
  $pathNow = trim(substr($pathNow, strlen($base)), '/');
}
$seg = explode('/', $pathNow)[0] ?? '';
function nav_active($key, $seg) {
  return $seg === $key ? ' active' : '';
}
?><!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
<meta name="theme-color" content="#0c0b10">
<meta name="apple-mobile-web-app-capable" content="yes">
<title><?= e($config['app_name'] ?? 'Velora') ?></title>
<link rel="stylesheet" href="<?= asset('css/app.css') ?>">
</head>
<?php
$isHomeGuest = !$u && ($seg === '' || $seg === 'home');
$bodyClass = trim(($u ? 'has-bottom-nav' : '') . ($isHomeGuest ? ' landing-page' : ''));
?>
<body class="<?= e($bodyClass) ?>">

<?php
  $__isPaid = $u ? is_paid_member((int)$u['id']) : false;
  $__trialLeft = $u ? trial_hours_left((int)$u['id']) : 0;
  $__canMsg = $u ? can_message((int)$u['id']) : false;
?>
<?php if ($u && !$__isPaid && $__trialLeft > 0): ?>
<div class="trial-banner">
  <span><strong>Free trial:</strong> ~<?= (int)$__trialLeft ?>h left · Discover &amp; match included · <strong>Inbox unlocks after payment</strong></span>
  <a class="btn small" href="<?= url('premium') ?>">Unlock inbox — KES <?= number_format(monthly_price_kes()) ?></a>
</div>
<?php elseif ($u && !$__isPaid && $__trialLeft <= 0): ?>
<div class="trial-banner trial-ended">
  <span><strong>Trial ended.</strong> Activate Monthly Access to keep discovering and to message matches.</span>
  <a class="btn small" href="<?= url('premium') ?>">Pay KES <?= number_format(monthly_price_kes()) ?></a>
</div>
<?php endif; ?>

<nav class="nav">
  <a class="brand" href="<?= url($u ? 'discovery' : 'home') ?>">VELORA</a>
  <div class="navlinks">
    <?php if ($u): ?>
      <a class="<?= nav_active('discovery', $seg) ?>" href="<?= url('discovery') ?>">Discover</a>
      <a class="<?= nav_active('matches', $seg) ?>" href="<?= $__canMsg ? url('matches') : url('premium') ?>">Matches</a>
      <a class="<?= nav_active('chat', $seg) ?>" href="<?= $__canMsg ? url('matches') : url('premium') ?>" title="<?= $__canMsg ? 'Inbox' : 'Inbox unlocks after payment' ?>">Inbox<?= $__canMsg && $unreadMsgs ? ' ('.$unreadMsgs.')' : ($__canMsg ? '' : ' 🔒') ?></a>
      <a class="<?= nav_active('premium', $seg) ?>" href="<?= url('premium') ?>">Premium</a>
      <a class="<?= nav_active('profile', $seg) ?>" href="<?= url('profile') ?>">Profile</a>
      <a class="<?= nav_active('settings', $seg) ?>" href="<?= url('settings') ?>">Settings</a>
      <?php if (($u['role'] ?? '') === 'admin'): ?><a href="<?= url('admin') ?>">Admin</a><?php endif; ?>
      <a href="<?= url('logout') ?>">Logout</a>
    <?php else: ?>
      <a href="<?= url('login') ?>">Login</a>
      <a class="btn small" href="<?= url('register') ?>">Join</a>
    <?php endif; ?>
  </div>
  <div class="nav-right">
    <?php if ($u): ?>
      <a class="inbox-pill" href="<?= $__canMsg ? url('matches') : url('premium') ?>" title="<?= $__canMsg ? 'Messages' : 'Pay to unlock inbox' ?>">
        <span aria-hidden="true">💬</span>
        <span class="hide-mobile">Inbox</span>
        <span class="dot" data-count="<?= $unreadMsgs ?>"><?= $unreadMsgs > 0 ? ($unreadMsgs > 99 ? '99+' : $unreadMsgs) : '' ?></span>
      </a>
    <?php endif; ?>
  </div>
</nav>
<main class="container<?= in_array($seg, ['admin'], true) ? ' wide' : '' ?>">
<?php if ($m = flash('error')): ?><div class="alert error"><?= e($m) ?></div><?php endif; ?>
<?php if ($m = flash('success')): ?><div class="alert success"><?= e($m) ?></div><?php endif; ?>
