<?php
require_once __DIR__ . '/../includes/redirect_helper.php';
session_start();
include(__DIR__ . '/../includes/auth.php');
requireRole(['master','admin','dj']);
?>
<?php include('../header.php'); ?>
<?php include('../menu.php'); ?>
<link rel="stylesheet" href="../css/style.css?v=1">

<main class="main-container">
  <section class="admin-card">
    <h2 class="text-warning mb-4">💬 Manage Comments</h2>

    <?php
    $comments_file = __DIR__ . '/../data/comments.json';
    $comments = file_exists($comments_file) ? json_decode(file_get_contents($comments_file), true) : [];

    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
      $action = $_POST['action'] ?? '';
      $index  = (int)($_POST['index'] ?? -1);

      if ($action === 'clear_all') {
        $comments = [];
      } elseif ($action === 'approve' && isset($comments[$index])) {
        $comments[$index]['approved'] = true;
      } elseif ($action === 'unapprove' && isset($comments[$index])) {
        $comments[$index]['approved'] = false;
      } elseif ($action === 'delete' && isset($comments[$index])) {
        array_splice($comments, $index, 1);
      } elseif ($action === 'reply' && isset($comments[$index])) {
        $comments[$index]['reply'] = trim($_POST['reply']);
      }

      file_put_contents($comments_file, json_encode(array_values($comments), JSON_PRETTY_PRINT));
      ah_redirect_back('/admin/comments.php', ['saved' => 1]);
    }
    ?>

    <div class="d-flex flex-wrap gap-2 mb-3">
      <a href="dashboard.php" class="btn btn-dark btn-sm">← Back to Dashboard</a>
      <form method="post" class="d-inline" onsubmit="return confirm('Clear all comments? This cannot be undone.');">
        <input type="hidden" name="return_to" value="<?= htmlspecialchars($_SERVER['REQUEST_URI'] ?? '/admin/comments.php') ?>">
        <button type="submit" name="action" value="clear_all" class="btn btn-danger btn-sm">Clear All Comments</button>
      </form>
    </div>

    <?php if (!empty($comments)): ?>
      <div class="comments-grid">
        <?php foreach ($comments as $i => $comment): ?>
          <div class="admin-comment-card mb-4">
            <h4><?= htmlspecialchars($comment['name']) ?></h4>
            <p><?= nl2br(htmlspecialchars($comment['message'])) ?></p>
            <em class="text-muted">(<?= htmlspecialchars($comment['timestamp']) ?>)</em>
            <p>Status: <?= !empty($comment['approved']) ? '✅ Approved' : '⏳ Pending' ?></p>

            <?php if (!empty($comment['reply'])): ?>
              <div class="admin-comment-reply mt-2"><strong>Reply:</strong> <?= nl2br(htmlspecialchars($comment['reply'])) ?></div>
            <?php endif; ?>

            <?php if (empty($comment['approved'])): ?>
              <form method="post" class="d-inline">
                <input type="hidden" name="return_to" value="<?= htmlspecialchars($_SERVER['REQUEST_URI'] ?? '/admin/comments.php') ?>">
                <input type="hidden" name="index" value="<?= $i ?>">
                <button type="submit" name="action" value="approve" class="btn btn-warning btn-sm">Approve</button>
              </form>
            <?php else: ?>
              <form method="post" class="d-inline">
                <input type="hidden" name="return_to" value="<?= htmlspecialchars($_SERVER['REQUEST_URI'] ?? '/admin/comments.php') ?>">
                <input type="hidden" name="index" value="<?= $i ?>">
                <button type="submit" name="action" value="unapprove" class="btn btn-secondary btn-sm">Unapprove</button>
              </form>
            <?php endif; ?>

            <form method="post" class="d-inline">
              <input type="hidden" name="return_to" value="<?= htmlspecialchars($_SERVER['REQUEST_URI'] ?? '/admin/comments.php') ?>">
              <input type="hidden" name="index" value="<?= $i ?>">
              <button type="submit" name="action" value="delete" class="btn btn-danger btn-sm">Delete</button>
            </form>

            <form method="post" class="mt-2">
              <input type="hidden" name="return_to" value="<?= htmlspecialchars($_SERVER['REQUEST_URI'] ?? '/admin/comments.php') ?>">
              <input type="hidden" name="index" value="<?= $i ?>">
              <input type="text" name="reply" value="<?= htmlspecialchars($comment['reply'] ?? '') ?>" class="admin-input mb-2" placeholder="Write a reply...">
              <button type="submit" name="action" value="reply" class="btn btn-dark btn-sm">Reply</button>
            </form>
          </div>
        <?php endforeach; ?>
      </div>
    <?php else: ?>
      <p class="text-muted">No comments yet. The mountain is quiet…</p>
    <?php endif; ?>
  </section>
</main>

<?php include('../footer.php'); ?>
