<?php
session_start();
require __DIR__ . '/auth.php';
require __DIR__ . '/includes/admin_lib.php';

$file = __DIR__ . '/../data/applications.txt';
$action = $_GET['action'] ?? '';
$idx = isset($_GET['i']) ? (int)$_GET['i'] : -1;

if ($action === 'delete' && $idx >= 0) {
  if (!$CAN_EDIT) die("<h1>Access Denied</h1><p>You do not have permission to delete entries.</p>");
  smurf_delete_block($file, $idx);
  header("Location: applications.php?saved=1");
  exit;
}
if ($action === 'clear') {
  if (!$CAN_EDIT) die("<h1>Access Denied</h1><p>You do not have permission to clear entries.</p>");
  smurf_clear_file($file);
  header("Location: applications.php?saved=1");
  exit;
}

$items = smurf_parse_blocks($file);
function thunder_field(array $item, string $key): string {
  return trim((string)($item[$key] ?? ''));
}

include __DIR__ . '/includes/admin_header.php';
?>
<section class="content-card">
  <div style="display:flex;justify-content:space-between;align-items:center;gap:10px;flex-wrap:wrap;">
    <div>
      <h1 class="admin-title" style="margin:0;">📝 Join Applications</h1>
      <p style="opacity:.82;margin:8px 0 0;">Applications submitted from the public Join page. File: <code>/data/applications.txt</code></p>
    </div>
    <div class="admin-actions">
      <?php if ($CAN_EDIT): ?><a class="admin-btn danger" href="applications.php?action=clear" onclick="return confirm('Clear ALL entries?');">Clear All</a><?php endif; ?>
      <a class="admin-btn" href="index.php">Back</a>
    </div>
  </div>

  <?php if (!empty($_GET['saved'])): ?><div class="msg success" style="margin-top:12px;">Saved.</div><?php endif; ?>

  <?php if (empty($items)): ?>
    <div class="content-card" style="margin-top:16px;text-align:left;">
      <p style="margin:0;">No applications yet.</p>
    </div>
  <?php else: ?>
    <div class="admin-entry-list" style="margin-top:18px;">
      <?php foreach ($items as $i => $item): ?>
        <?php
          $name = thunder_field($item, 'Name');
          $email = thunder_field($item, 'Email');
          $role = thunder_field($item, 'Role');
          $experience = thunder_field($item, 'Experience');
          $why = thunder_field($item, 'Why');
          $demo = thunder_field($item, 'Demo');
          $demoUrl = smurf_demo_url($demo);
        ?>
        <article class="content-card admin-entry-card">
          <div class="admin-entry-head">
            <div>
              <h3 style="margin:0 0 4px;"><?= smurf_h($name !== '' ? $name : ('Application #' . $i)) ?></h3>
              <div class="admin-entry-tags">
                <?php if ($role !== ''): ?><span class="admin-entry-tag"><?= smurf_h($role) ?></span><?php endif; ?>
                <?php if ($experience !== ''): ?><span class="admin-entry-tag"><?= smurf_h($experience) ?></span><?php endif; ?>
              </div>
            </div>
            <div class="admin-actions">
              <?php if ($CAN_EDIT): ?><a class="admin-btn" href="applications.php?action=delete&i=<?= $i ?>" onclick="return confirm('Delete this entry?');">Delete</a><?php endif; ?>
            </div>
          </div>

          <div class="admin-entry-grid">
            <?php if ($email !== ''): ?>
              <div class="admin-entry-field">
                <span class="admin-entry-label">Email</span>
                <div><?= smurf_h($email) ?></div>
              </div>
            <?php endif; ?>
            <?php if ($role !== ''): ?>
              <div class="admin-entry-field">
                <span class="admin-entry-label">Requested Role</span>
                <div><?= smurf_h($role) ?></div>
              </div>
            <?php endif; ?>
            <?php if ($experience !== ''): ?>
              <div class="admin-entry-field">
                <span class="admin-entry-label">Experience</span>
                <div><?= smurf_h($experience) ?></div>
              </div>
            <?php endif; ?>
            <?php if ($demo !== ''): ?>
              <div class="admin-entry-field">
                <span class="admin-entry-label">Demo File</span>
                <div style="word-break:break-word;"><?= smurf_h($demo) ?></div>
              </div>
            <?php endif; ?>
          </div>

          <?php if ($demoUrl !== ''): ?>
            <div class="admin-entry-body" style="margin-bottom:12px;">
              <span class="admin-entry-label">Demo Audio</span>
              <audio controls preload="none" style="width:100%;max-width:520px;display:block;">
                <source src="<?= smurf_h($demoUrl) ?>">
                Your browser cannot play this file.
              </audio>
              <div class="admin-actions" style="margin-top:12px;">
                <a class="admin-btn" href="<?= smurf_h($demoUrl) ?>" target="_blank" rel="noopener">Open Demo</a>
                <a class="admin-btn" href="<?= smurf_h($demoUrl) ?>" download>Download Demo</a>
              </div>
            </div>
          <?php endif; ?>

          <?php if ($why !== ''): ?>
            <div class="admin-entry-body">
              <span class="admin-entry-label">Why they want to join</span>
              <p><?= nl2br(smurf_h($why)) ?></p>
            </div>
          <?php endif; ?>

          <?php if (!empty($item['_raw'])): ?>
            <details class="admin-entry-raw">
              <summary>Show raw application block</summary>
              <pre><?= smurf_h($item['_raw']) ?></pre>
            </details>
          <?php endif; ?>
        </article>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>
</section>
<?php include __DIR__ . '/includes/admin_footer.php'; ?>
