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

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}

$user_id = $_SESSION['user_id'];

// Fetch user
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch();

// Fetch user's posts
$postStmt = $pdo->prepare("
    SELECT id, title, file_path, type, nsfw, created_at
    FROM posts
    WHERE user_id = ?
    ORDER BY id DESC
");
$postStmt->execute([$user_id]);
$posts = $postStmt->fetchAll();
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Your Profile</title>
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-slate-50">

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

  <h1 class="text-2xl font-semibold mb-4">Your Profile</h1>

  <p class="text-sm mb-2"><strong>Username:</strong> <?= htmlspecialchars($user['username']) ?></p>
  <p class="text-sm mb-4"><strong>18+:</strong> <?= $user['is_over_18'] ? "Yes" : "No" ?></p>

  <a href="profile_edit.php"
     class="inline-block px-3 py-1 bg-teal-600 text-white rounded text-sm hover:bg-teal-700 mb-6">
     Edit Profile
  </a>

  <h2 class="text-xl font-semibold mb-3">Your Uploads</h2>

  <div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4">
    <?php foreach ($posts as $p): ?>
      <a href="post.php?id=<?= $p['id'] ?>" class="block bg-slate-100 rounded-lg overflow-hidden">
        <?php if ($p['type'] === 'image'): ?>
          <img src="<?= $p['file_path'] ?>" class="w-full h-48 object-cover">
        <?php else: ?>
          <video src="<?= $p['file_path'] ?>" class="w-full h-48 object-cover"></video>
        <?php endif; ?>
        <div class="p-2 text-sm"><?= htmlspecialchars($p['title']) ?></div>
      </a>
    <?php endforeach; ?>
  </div>

</div>

</body>
</html>