<?php
declare(strict_types=1);

/**
 * Shared admin chrome. Expects $pageTitle, optional $activeNav.
 */
function admin_header(string $pageTitle, string $activeNav = ''): void
{
    $base = defined('APP_BASE') ? APP_BASE : '';
    $tenant = function_exists('get_tenant') ? get_tenant() : null;
    $publicQs = function_exists('tenant_qs') ? tenant_qs($tenant) : '';
    $nav = [
        'dashboard' => ['Dashboard', $base . '/admin/index.php'],
        'help' => ['Getting Started', $base . '/admin/help.php'],
        'calendar' => ['Calendar', $base . '/admin/calendar.php'],
        'users' => ['Clients & Reminders', $base . '/admin/users.php'],
        'policy' => ['Client Policy', $base . '/admin/policy.php'],
        'payments' => ['Payments', $base . '/admin/payments.php'],
        'products' => ['Products', $base . '/admin/products.php'],
        'media' => ['Media', $base . '/admin/media.php'],
        'content' => ['Page Content', $base . '/admin/content.php'],
        'styles' => ['Styles Gallery', $base . '/admin/styles.php'],
        'services' => ['Services', $base . '/admin/services.php'],
        'staff' => ['Staff', $base . '/admin/staff.php'],
        'branding' => ['Branding', $base . '/admin/branding.php'],
    ];
    ?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title><?= htmlspecialchars($pageTitle) ?> · BarberShop Admin</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@400;500;600&family=Space+Grotesk:wght@500;700&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.6.2/cropper.min.css">
  <link rel="stylesheet" href="<?= htmlspecialchars($base) ?>/assets/admin.css?v=20260727a">
</head>
<body class="admin">
  <aside class="sidebar">
    <div class="brand">
      <span class="brand-mark">BS</span>
      <div>
        <strong>BarberShop</strong>
        <small><?= htmlspecialchars(shop_company_name(null, $tenant ?? [])) ?></small>
      </div>
    </div>
    <nav>
      <?php foreach ($nav as $key => [$label, $href]): ?>
        <a class="<?= $activeNav === $key ? 'active' : '' ?>" href="<?= htmlspecialchars($href) ?>"><?= htmlspecialchars($label) ?></a>
      <?php endforeach; ?>
    </nav>
    <div class="sidebar-foot">
      <a href="<?= htmlspecialchars($base) ?>/<?= htmlspecialchars($publicQs) ?>" target="_blank">View public site</a>
      <a href="<?= htmlspecialchars($base) ?>/admin/logout.php">Log out</a>
    </div>
  </aside>
  <main class="main">
    <header class="main-head">
      <h1><?= htmlspecialchars($pageTitle) ?></h1>
    </header>
    <div class="main-body">
<?php
}

function admin_footer(string $extraScript = ''): void
{
    require_once __DIR__ . '/sn_credit.php';
    $base = defined('APP_BASE') ? APP_BASE : '';
    ?>
    </div>
    <footer class="admin-sn-footer">
      <?php sn_credit_render(); ?>
    </footer>
  </main>
  <script>window.APP_BASE = <?= json_encode($base) ?>; window.TENANT_SLUG = <?= json_encode(function_exists('tenant_param') ? tenant_param() : '') ?>;</script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.6.2/cropper.min.js"></script>
  <script src="<?= htmlspecialchars($base) ?>/assets/admin.js?v=20260727a"></script>
  <?= $extraScript ?>
</body>
</html>
<?php
}
