<?php
session_start();
require_once "../db.php";

// Simple admin check
if (!isset($_SESSION['user_id']) || $_SESSION['username'] !== "admin") {
    die("Access denied.");
}

// Fetch posts with all metadata
$stmt = $pdo->query("
    SELECT 
        p.id,
        p.title,
        p.file_path,
        p.thumbnail_path,
        p.type,
        p.nsfw,
        p.camera_model,
        p.gps_lat,
        p.gps_lng,
        p.location_region,
        p.created_at,
        GROUP_CONCAT(t.name) AS tags,
        u.username,
        u.id AS user_id
    FROM posts p
    JOIN users u ON p.user_id = u.id
    LEFT JOIN post_tags pt ON p.id = pt.post_id
    LEFT JOIN tags t ON pt.tag_id = t.id
    GROUP BY p.id
    ORDER BY p.id DESC
");
$posts = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Admin Moderation</title>
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-slate-50">

<div class="max-w-6xl mx-auto mt-10 bg-white p-6 rounded-xl shadow">

  <h1 class="text-3xl font-semibold mb-6">Admin Moderation Panel</h1>

  <table class="w-full text-sm">
    <tr class="border-b bg-slate-100">
      <th class="p-2 text-left">Preview</th>
      <th class="p-2 text-left">Info</th>
      <th class="p-2 text-left">Metadata</th>
      <th class="p-2 text-left">Actions</th>
    </tr>

    <?php foreach ($posts as $p): ?>
      <tr class="border-b align-top">

        <!-- Thumbnail -->
        <td class="p-2">
          <?php if ($p['type'] === 'image'): ?>
            <img src="../<?= htmlspecialchars($p['thumbnail_path']) ?>" 
                 class="w-24 h-24 object-cover rounded border">
          <?php else: ?>
            <video src="../<?= htmlspecialchars($p['thumbnail_path']) ?>" 
                   class="w-24 h-24 object-cover rounded border"></video>
          <?php endif; ?>
        </td>

        <!-- Basic Info -->
        <td class="p-2">
          <div class="font-semibold text-slate-900"><?= htmlspecialchars($p['title']) ?></div>
          <div class="text-xs text-slate-500">by <?= htmlspecialchars($p['username']) ?></div>
          <div class="text-xs mt-1">
            <span class="px-2 py-0.5 rounded-full text-white text-[10px] 
              <?= $p['nsfw'] ? 'bg-red-600' : 'bg-green-600' ?>">
              <?= $p['nsfw'] ? 'NSFW' : 'SFW' ?>
            </span>
          </div>
          <div class="text-xs text-slate-400 mt-1">
            <?= date("M j, Y", strtotime($p['created_at'])) ?>
          </div>
        </td>

        <!-- Metadata -->
        <td class="p-2 text-xs text-slate-700">
          <?php if ($p['camera_model']): ?>
            <div><strong>Camera:</strong> <?= htmlspecialchars($p['camera_model']) ?></div>
          <?php endif; ?>

          <?php if ($p['gps_lat'] && $p['gps_lng']): ?>
            <div class="mt-1">
              <strong>GPS:</strong> 
              <?= $p['gps_lat'] ?>, <?= $p['gps_lng'] ?>
              <br>
              <a href="https://www.google.com/maps?q=<?= $p['gps_lat'] ?>,<?= $p['gps_lng'] ?>" 
                 target="_blank" class="text-blue-600 underline">
                View on Maps
              </a>
            </div>
          <?php endif; ?>

          <?php if ($p['location_region']): ?>
            <div class="mt-1"><strong>Region:</strong> <?= htmlspecialchars($p['location_region']) ?></div>
          <?php endif; ?>

          <?php if ($p['tags']): ?>
            <div class="mt-2">
              <strong>Tags:</strong><br>
              <?php foreach (explode(",", $p['tags']) as $tag): ?>
                <span class="inline-block bg-teal-100 text-teal-700 px-2 py-0.5 rounded text-[10px] mr-1">
                  #<?= htmlspecialchars($tag) ?>
                </span>
              <?php endforeach; ?>
            </div>
          <?php endif; ?>
        </td>

        <!-- Actions -->
        <td class="p-2 w-40">
          <div class="flex flex-col gap-2">

            <!-- Edit Post -->
            <a href="edit_post.php?id=<?= $p['id'] ?>"
               class="px-2 py-1 bg-blue-600 text-white rounded text-xs text-center">
              Edit Post
            </a>

            <!-- Ban User -->
            <form action="../api/admin_ban_user.php" method="POST"
                  onsubmit="return confirm('Ban this user?');">
              <input type="hidden" name="user_id" value="<?= $p['user_id'] ?>">
              <button class="px-2 py-1 bg-black text-white rounded text-xs w-full">
                Ban User
              </button>
            </form>

            <!-- Toggle NSFW -->
            <form action="../api/admin_nsfw_toggle.php" method="POST">
              <input type="hidden" name="post_id" value="<?= $p['id'] ?>">
              <button class="px-2 py-1 bg-yellow-500 text-white rounded text-xs w-full">
                Toggle NSFW
              </button>
            </form>

            <!-- Delete -->
            <form action="../api/admin_delete.php" method="POST"
                  onsubmit="return confirm('Delete this post?');">
              <input type="hidden" name="post_id" value="<?= $p['id'] ?>">
              <button class="px-2 py-1 bg-red-600 text-white rounded text-xs w-full">
                Delete
              </button>
            </form>

          </div>
        </td>

      </tr>
    <?php endforeach; ?>

  </table>

</div>

</body>
</html>