<?php

/**
 * Shared admin layout. Include after setting:
 *   $pageTitle  (string)
 *   $activeNav  (string, one of the nav keys below)
 * Requires the including page to have already called Auth::requireLogin()
 * (or requireRole()) so $currentAdmin-equivalent data is available via Auth::user().
 */

use KeyLair\Auth;
use KeyLair\Helpers;
use KeyLair\I18n;

$__admin = Auth::user();

$__navItems = [
    'dashboard' => ['label' => I18n::t('nav.dashboard'), 'href' => 'index.php'],
    'campaigns' => ['label' => I18n::t('nav.campaigns'), 'href' => 'campaigns.php'],
    'products' => ['label' => I18n::t('nav.products'), 'href' => 'products.php'],
    'platforms' => ['label' => I18n::t('nav.platforms'), 'href' => 'platforms.php'],
    'keys' => ['label' => I18n::t('nav.keys'), 'href' => 'keys.php'],
    'claim_links' => ['label' => I18n::t('nav.claim_links'), 'href' => 'claim_links.php'],
    'users' => ['label' => I18n::t('nav.users'), 'href' => 'users.php'],
    'creator_groups' => ['label' => I18n::t('nav.creator_groups'), 'href' => 'creator_groups.php'],
    'redemptions' => ['label' => I18n::t('nav.redemptions'), 'href' => 'redemptions.php'],
    'analytics' => ['label' => I18n::t('nav.analytics'), 'href' => 'analytics.php'],
    'design' => ['label' => I18n::t('nav.design'), 'href' => 'design.php'],
];

$__navIcons = [
    'dashboard' => '<rect x="3" y="3" width="8" height="8" rx="1.5"/><rect x="13" y="3" width="8" height="8" rx="1.5"/><rect x="3" y="13" width="8" height="8" rx="1.5"/><rect x="13" y="13" width="8" height="8" rx="1.5"/>',
    'campaigns' => '<rect x="3" y="5" width="18" height="14" rx="2"/><path d="M3 9h18"/>',
    'products' => '<path d="M4 7l8-4 8 4-8 4-8-4z"/><path d="M4 7v10l8 4 8-4V7"/><path d="M12 11v10"/>',
    'platforms' => '<path d="M12 3l9 5-9 5-9-5 9-5z"/><path d="M3 13l9 5 9-5"/>',
    'keys' => '<rect x="4" y="9" width="16" height="10" rx="2"/><path d="M8 9V6a4 4 0 018 0v3"/>',
    'claim_links' => '<path d="M10 13a5 5 0 007 0l3-3a5 5 0 00-7-7l-1.5 1.5"/><path d="M14 11a5 5 0 00-7 0l-3 3a5 5 0 007 7l1.5-1.5"/>',
    'users' => '<circle cx="12" cy="8" r="4"/><path d="M4 21c1.5-5 4.5-7 8-7s6.5 2 8 7"/>',
    'creator_groups' => '<circle cx="9" cy="8" r="3"/><path d="M2 20c1-4 3.5-6 7-6s6 2 7 6"/><circle cx="17" cy="8" r="2.5"/><path d="M16 14.2c2.7.4 4.2 2.2 5 5.8"/>',
    'redemptions' => '<path d="M4 12l5 5L20 6"/>',
    'analytics' => '<path d="M4 20V10M12 20V4M20 20v-7"/>',
    'design' => '<path d="M12 19l7-7 3 3-7 7-3-3z"/><path d="M18 13l-1.5-7.5L2 2l3.5 14.5L13 18l5-5z"/><path d="M2 2l7.586 7.586"/><circle cx="11" cy="11" r="2"/>',
    'account' => '<circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.7 1.7 0 00.34 1.87l.06.06a2 2 0 11-2.83 2.83l-.06-.06a1.7 1.7 0 00-1.87-.34 1.7 1.7 0 00-1 1.55V21a2 2 0 11-4 0v-.09a1.7 1.7 0 00-1-1.55 1.7 1.7 0 00-1.87.34l-.06.06a2 2 0 11-2.83-2.83l.06-.06a1.7 1.7 0 00.34-1.87 1.7 1.7 0 00-1.55-1H3a2 2 0 110-4h.09a1.7 1.7 0 001.55-1 1.7 1.7 0 00-.34-1.87l-.06-.06a2 2 0 112.83-2.83l.06.06a1.7 1.7 0 001.87.34H9a1.7 1.7 0 001-1.55V3a2 2 0 114 0v.09a1.7 1.7 0 001 1.55 1.7 1.7 0 001.87-.34l.06-.06a2 2 0 112.83 2.83l-.06.06a1.7 1.7 0 00-.34 1.87V9a1.7 1.7 0 001.55 1H21a2 2 0 110 4h-.09a1.7 1.7 0 00-1.55 1z"/>',
];

if (Auth::canEdit()) {
    $__navItems['bug_reporting'] = ['label' => I18n::t('nav.bug_reporting'), 'href' => 'bug_reporting.php'];
    $__navIcons['bug_reporting'] = '<path d="M21 11.5a8.38 8.38 0 01-.9 3.8 8.5 8.5 0 01-7.6 4.7 8.38 8.38 0 01-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 01-.9-3.8 8.5 8.5 0 014.7-7.6 8.38 8.38 0 013.8-.9h.5a8.48 8.48 0 018 8v.5z"/>';
}

$__adminNavItems = [];

// Demo previews every admin-tier page too -- canViewAdminTier() admits it
// the same as canManageUsers() -- it just can't submit anything once
// it's there (each page enforces that server-side on its own POST
// handler). One flat list: every page here treats 'admin' and
// 'master_admin' identically, so there's no separate, more-restricted
// tier to split Migrations/Updates/Mail Server/API Access into.
if (Auth::canViewAdminTier()) {
    $__adminNavItems['system_users'] = ['label' => I18n::t('nav.system_users'), 'href' => 'system_users.php'];
    $__navIcons['system_users'] = '<circle cx="12" cy="8" r="4"/><path d="M4 21c1.5-5 4.5-7 8-7s6.5 2 8 7"/><path d="M19 8h3M20.5 6.5v3"/>';
    $__adminNavItems['branding'] = ['label' => I18n::t('nav.branding'), 'href' => 'branding.php'];
    $__navIcons['branding'] = '<rect x="3" y="3" width="18" height="18" rx="3"/><circle cx="8.5" cy="8.5" r="1.8"/><path d="M21 15l-5-5-9 9"/>';
    $__adminNavItems['legal'] = ['label' => I18n::t('nav.legal'), 'href' => 'legal.php'];
    $__navIcons['legal'] = '<path d="M12 3v18M7 7l-4 8a4 4 0 008 0l-4-8zM17 7l-4 8a4 4 0 008 0l-4-8z"/><path d="M5 21h14"/>';
    $__adminNavItems['migrations'] = ['label' => I18n::t('nav.migrations'), 'href' => 'migrations.php'];
    $__navIcons['migrations'] = '<ellipse cx="12" cy="5" rx="8" ry="3"/><path d="M4 5v6c0 1.7 3.6 3 8 3s8-1.3 8-3V5"/><path d="M4 11v6c0 1.7 3.6 3 8 3s8-1.3 8-3v-6"/>';
    $__adminNavItems['deploy'] = ['label' => I18n::t('nav.deploy'), 'href' => 'deploy.php'];
    $__navIcons['deploy'] = '<path d="M12 3v12M6 9l6 6 6-6"/><path d="M4 21h16"/>';
    $__adminNavItems['mail_settings'] = ['label' => I18n::t('nav.mail_settings'), 'href' => 'mail_settings.php'];
    $__navIcons['mail_settings'] = '<rect x="3" y="5" width="18" height="14" rx="2"/><path d="M3 7l9 6 9-6"/>';
    $__adminNavItems['api_access'] = ['label' => I18n::t('nav.api_access'), 'href' => 'api_access.php'];
    $__navIcons['api_access'] = '<rect x="4" y="11" width="16" height="10" rx="2"/><path d="M8 11V7a4 4 0 118 0v4"/><circle cx="12" cy="16" r="1.5"/>';
}

$__adminGroupActive = array_key_exists($activeNav, $__adminNavItems);
?>
<!DOCTYPE html>
<html lang="<?= Helpers::e(I18n::locale()) ?>">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>html,body{background:#17212e;}</style>
<title><?= Helpers::e(\KeyLair\Settings::siteName()) ?> — <?= Helpers::e($pageTitle ?? 'Admin') ?></title>
<?= \KeyLair\Settings::faviconTag() ?>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@500;600;700&family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@500;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="/assets/css/tokens.css?admin=1">
<link rel="stylesheet" href="/assets/css/admin-dashboard.css?admin=1">
<?php if (!empty($extraAdminCss)): ?>
<link rel="stylesheet" href="/assets/css/<?= Helpers::e($extraAdminCss) ?>.css?admin=1">
<?php endif; ?>
</head>
<body>
  <aside class="sidebar">
    <div class="brand"><?= \KeyLair\Settings::brandIcon() ?><?= Helpers::e(\KeyLair\Settings::siteName()) ?></div>
    <nav>
      <?php foreach ($__navItems as $key => $item): ?>
        <a href="<?= Helpers::e($item['href']) ?>" class="nav-item<?= $activeNav === $key ? ' active' : '' ?>" style="text-decoration:none;">
          <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7"><?= $__navIcons[$key] ?></svg>
          <span class="label"><?= Helpers::e($item['label']) ?></span>
        </a>
      <?php endforeach; ?>
      <?php if (!empty($__adminNavItems)): ?>
      <button type="button" class="nav-item nav-group-toggle" data-toggle-target="sidebar-admin-group" aria-expanded="<?= $__adminGroupActive ? 'true' : 'false' ?>">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7"><path d="M12 3l7 3v6c0 4.5-3 7.5-7 9-4-1.5-7-4.5-7-9V6l7-3z"/></svg>
        <span class="label"><?= Helpers::e(I18n::t('nav.admin_group')) ?></span>
        <svg class="nav-group-chevron" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7"><path d="M6 9l6 6 6-6"/></svg>
      </button>
      <div id="sidebar-admin-group" class="nav-group<?= $__adminGroupActive ? ' open' : '' ?>">
        <?php foreach ($__adminNavItems as $key => $item): ?>
          <a href="<?= Helpers::e($item['href']) ?>" class="nav-item<?= $activeNav === $key ? ' active' : '' ?>" style="text-decoration:none;">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7"><?= $__navIcons[$key] ?></svg>
            <span class="label"><?= Helpers::e($item['label']) ?></span>
          </a>
        <?php endforeach; ?>
      </div>
      <?php endif; ?>
    </nav>
    <div class="sidebar-bottom">
      <div id="sidebar-account-menu" class="sidebar-account-group">
        <a href="account.php" class="nav-item<?= $activeNav === 'account' ? ' active' : '' ?>" style="text-decoration:none;">
          <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7"><?= $__navIcons['account'] ?></svg>
          <span class="label"><?= Helpers::e(I18n::t('nav.settings')) ?></span>
        </a>
        <a href="logout.php" class="nav-item" style="text-decoration:none;">
          <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7"><path d="M9 21H5a2 2 0 01-2-2V5a2 2 0 012-2h4"/><path d="M16 17l5-5-5-5"/><path d="M21 12H9"/></svg>
          <span class="label"><?= Helpers::e(I18n::t('nav.logout')) ?></span>
        </a>
      </div>
      <button type="button" class="sidebar-footer" data-toggle-target="sidebar-account-menu" aria-expanded="false">
        <div class="avatar"></div>
        <div class="who">
          <div class="name"><?= Helpers::e($__admin['username']) ?></div>
          <div class="role"><?= Helpers::e(Auth::roleLabel($__admin['role'])) ?></div>
        </div>
      </button>
    </div>
  </aside>

  <main class="main">
    <?php if (!empty($_SESSION['flash_error'])): ?>
    <div class="panel" style="border-color:var(--danger); margin-bottom:16px;"><span style="color:var(--danger); font-size:13px;"><?= Helpers::e($_SESSION['flash_error']) ?></span></div>
    <?php unset($_SESSION['flash_error']); endif; ?>
