<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Sanitize session variables before use
$editor_type = isset($_SESSION['editor_type']) ? htmlspecialchars($_SESSION['editor_type'], ENT_QUOTES, 'UTF-8') : '';

// Setting toggles (passed in by callers if needed)
$blindeditor = isset($blindeditor) ? $blindeditor : 'no';
$contesttab  = isset($contesttab)  ? $contesttab  : 'no';

// Role hierarchy: each role inherits the tools of those below it.
$role_rank = ['' => 0, 'reader' => 1, 'editor' => 2, 'admin' => 3, 'superadmin' => 4];
$cur_rank  = $role_rank[$editor_type] ?? 0;
$at_least  = function (string $role) use ($cur_rank, $role_rank): bool {
    return $cur_rank >= ($role_rank[$role] ?? 999);
};
?>

<nav class="editor-nav" aria-label="Editorial navigation">
  <?php if ($editor_type === 'reader'): ?>

    <div class="editor-nav__group">
      <a href="readers-main.php" class="editor-nav__item">Reader Panel</a>
      <?php
      // Reader guidelines URL (sm_settings var 'readertos_url')
      $guidelines_url = '#';
      if (isset($pdo)) {
          try {
              $stmt = $pdo->prepare("SELECT value FROM sm_settings WHERE var = 'readertos_url'");
              $stmt->execute();
              $val = $stmt->fetchColumn();
              if (is_string($val) && $val !== '') {
                  $guidelines_url = htmlspecialchars($val, ENT_QUOTES, 'UTF-8');
              }
          } catch (Throwable $e) { /* keep '#' */ }
      }
      ?>
      <a href="<?= $guidelines_url ?>" target="_blank" rel="noopener" class="editor-nav__item">Guidelines</a>
    </div>

  <?php elseif ($at_least('editor')): ?>

    <!-- Workflow group (editor and up) -->
    <div class="editor-nav__group">
      <a href="editors-main.php" class="editor-nav__item">Dashboard</a>

      <?php if ($blindeditor == 'yes'): ?>
        <a href="editors-blind.php" class="editor-nav__item">Slush Pile</a>
      <?php else: ?>
        <a href="editors-subs.php" class="editor-nav__item">Slush Pile</a>
      <?php endif; ?>

      <a href="editors-editions.php" class="editor-nav__item">Editions</a>

      <?php if ($blindeditor == 'yes'): ?>
        <a href="editors-blind-contest.php" class="editor-nav__item">Contest</a>
      <?php elseif ($contesttab == 'yes'): ?>
        <a href="editors-submissions-contest.php" class="editor-nav__item">Contest</a>
      <?php endif; ?>

      <?php if ($blindeditor == 'no'): ?>
        <a href="editors-search.php" class="editor-nav__item">Search</a>
      <?php endif; ?>

      <a href="editors-approved.php"      class="editor-nav__item">Accepted</a>
      <a href="editors-rejected.php"      class="editor-nav__item">Rejected</a>
      <a href="editors-othertypes.php"    class="editor-nav__item">Other</a>
      <a href="revisions-in-progress.php" class="editor-nav__item">Revisions</a>
    </div>

    <!-- Admin group -->
    <?php if ($at_least('admin')): ?>
      <div class="editor-nav__group editor-nav__group--admin">
        <a href="editors-credits.php"       class="editor-nav__item editor-nav__item--admin">Credits</a>
        <a href="editors-authors.php"       class="editor-nav__item editor-nav__item--admin">Contributors</a>
        <a href="mail-center.php"           class="editor-nav__item editor-nav__item--admin">Mail</a>
        <?php if ($at_least('superadmin')): ?>
          <a href="editors.php"             class="editor-nav__item editor-nav__item--admin">Staff</a>
        <?php endif; ?>
        <a href="fine_settings.php"         class="editor-nav__item editor-nav__item--admin">Settings</a>
        <a href="mail_settings.php"         class="editor-nav__item editor-nav__item--admin">SMTP</a>
        <a href="paypal_settings.php"       class="editor-nav__item editor-nav__item--admin">PayPal</a>
        <a href="admin_guidelines.php"      class="editor-nav__item editor-nav__item--admin">Guidelines</a>
      </div>
    <?php endif; ?>

  <?php endif; ?>
</nav>
