<?php
// /var/www/html/profile_view.php
session_start();
require_once 'config.php';
include 'header.php';

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) {
    echo "Utilisateur non spécifié.";
    exit;
}

$stmt = $pdo->prepare("SELECT id, username, bio, profile_picture FROM utilisateurs WHERE id = ?");
$stmt->execute([$id]);
$u = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$u) { echo "Utilisateur introuvable."; exit; }

$stmt2 = $pdo->prepare("SELECT id, title, content, image_url, date_creation FROM posts WHERE user_id = ? ORDER BY date_creation DESC");
$stmt2->execute([$id]);
$posts = $stmt2->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="container" style="max-width:900px;margin:40px auto;">
  <h2>Profil de <?= htmlspecialchars($u['username']) ?></h2>
  <?php if (!empty($u['profile_picture'])): ?>
    <img src="<?= htmlspecialchars($u['profile_picture']) ?>" style="width:100px;height:100px;border-radius:50%;object-fit:cover;border:2px solid #ff91a4;">
  <?php else: ?>
    <div style="width:100px;height:100px;border-radius:50%;display:inline-flex;align-items:center;justify-content:center;font-size:40px;background:#fff4f7;border:2px solid #ff91a4;">👤</div>
  <?php endif; ?>
  <p><?= nl2br(htmlspecialchars($u['bio'] ?? '')) ?></p>

  <h3>Articles publiés</h3>
  <?php if ($posts): foreach($posts as $p): ?>
    <div style="border-bottom:1px solid #eee;padding:10px 0;">
      <a href="article.php?id=<?= (int)$p['id'] ?>"><?= htmlspecialchars($p['title']) ?></a>
      <div style="font-size:13px;color:#666;"><?= htmlspecialchars($p['date_creation']) ?></div>
    </div>
  <?php endforeach; else: ?>
    <p>Aucun article publié.</p>
  <?php endif; ?>
</div>
<?php include 'footer.php'; ?>
