<?php include 'includes/header.php'; ?>
<?php include 'includes/menu.php'; ?>

<?php
$requestsFile = __DIR__ . '/data/requests.txt';
$bannedFile = __DIR__ . '/data/banned_ips.txt';

// Handle deletion
if (isset($_GET['delete'])) {
  $deleteId = $_GET['delete'];
  $lines = file($requestsFile, FILE_IGNORE_NEW_LINES);
  $updated = [];

  foreach ($lines as $line) {
    $parts = explode('|', $line);
    if (count($parts) < 1) continue;
    $id = $parts[0];
    if ($id !== $deleteId) {
      $updated[] = $line;
    }
  }

  file_put_contents($requestsFile, implode("\n", $updated));
  header('Location: admin-requests.php');
  exit;
}

// Handle IP ban
if (isset($_GET['ban'])) {
  $ipToBan = $_GET['ban'];
  $banned = file_exists($bannedFile) ? file($bannedFile, FILE_IGNORE_NEW_LINES) : [];
  if (!in_array($ipToBan, $banned)) {
    file_put_contents($bannedFile, $ipToBan . "\n", FILE_APPEND);
    echo "<div class='alert alert-warning'>IP $ipToBan has been banned.</div>";
  }
}

// Handle approval
if (isset($_GET['approve'])) {
  $approveId = $_GET['approve'];
  $lines = file($requestsFile, FILE_IGNORE_NEW_LINES);
  $updated = [];

  foreach ($lines as $line) {
    $parts = explode('|', $line);
    if (count($parts) === 7) {
      [$id, $name, $song, $message, $ip, $timestamp, $approved] = $parts;
      if ($id === $approveId) {
        $updated[] = "$id|$name|$song|$message|$ip|$timestamp|yes";
      } else {
        $updated[] = $line;
      }
    }
  }

  file_put_contents($requestsFile, implode("\n", $updated));
  echo "<div class='alert alert-success'>Request #$approveId approved.</div>";
}

$requests = file_exists($requestsFile) ? file($requestsFile, FILE_IGNORE_NEW_LINES) : [];
$bannedIPs = file_exists($bannedFile) ? file($bannedFile, FILE_IGNORE_NEW_LINES) : [];
?>

<div class="container content-box mt-5">
  <div class="text-center mb-5">
    <h1>🛠️ Request Moderation</h1>
    <p class="subtext">Approve, delete, or ban IPs from submitted song requests</p>
  </div>

  <?php if (empty($requests)): ?>
    <p>No requests found.</p>
  <?php else: ?>
    <?php foreach (array_reverse($requests) as $line): ?>
      <?php
      $parts = explode('|', $line);
      if (count($parts) === 7) {
        [$id, $name, $song, $message, $ip, $timestamp, $approved] = $parts;
      ?>
        <div class="comment-box border rounded p-3 mb-4">
          <p><strong><?= htmlspecialchars($name) ?></strong> requested <em><?= htmlspecialchars($song ?: '—') ?></em></p>
          <p><?= nl2br(htmlspecialchars($message)) ?></p>
          <small>
            <?= date('M j, Y g:i A', strtotime($timestamp)) ?> |
            IP: <?= $ip ?>
            <?php if (in_array($ip, $bannedIPs)): ?>
              <span class="badge bg-danger ms-2">BANNED</span>
            <?php endif; ?>
            <?php if ($approved === 'yes'): ?>
              <span class="badge bg-success ms-2">APPROVED</span>
            <?php else: ?>
              <span class="badge bg-secondary ms-2">PENDING</span>
            <?php endif; ?>
          </small>
          <div class="mt-2">
            <?php if ($approved !== 'yes'): ?>
              <a href="?approve=<?= $id ?>" class="btn btn-sm btn-success me-2">Approve</a>
            <?php endif; ?>
            <a href="?delete=<?= $id ?>" class="btn btn-sm btn-danger me-2" onclick="return confirm('Delete this request?')">Delete</a>
            <?php if (!in_array($ip, $bannedIPs)): ?>
              <a href="?ban=<?= $ip ?>" class="btn btn-sm btn-warning" onclick="return confirm('Ban this IP?')">Ban IP</a>
            <?php endif; ?>
          </div>
        </div>
      <?php } ?>
    <?php endforeach; ?>
  <?php endif; ?>
</div>

<?php include 'includes/footer.php'; ?>
