<?php

declare(strict_types=1);

require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/db.php';

// 🎀 Demarre la session puis force l'authentification avant affichage du profil 🎀
startSecureSession();
requireLogin();

$userId = (int)$_SESSION['user_id'];
$user = null;

try {
    // 🎀 Recharge les donnees utilisateur depuis la base pour afficher un profil a jour 🎀
    $pdo = getPdo();
    $stmt = $pdo->prepare('SELECT id, name, email, created_at FROM users WHERE id = :id LIMIT 1');
    $stmt->execute(['id' => $userId]);
    $user = $stmt->fetch();
} catch (Throwable $e) {
    $user = null;
}

// 🎀 Si le compte n'est plus accessible, deconnecte proprement la session 🎀
if (!$user) {
    header('Location: logout.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Profil | MindGallery</title>
    <link rel="stylesheet" href="../css/style.css">
    <link rel="stylesheet" href="../css/login.css">
</head>
<body>
    <main class="auth-layout">
        <h1>Mon profil</h1>
        <p class="auth-text"><strong>Nom :</strong> <?= htmlspecialchars((string)$user['name'], ENT_QUOTES, 'UTF-8') ?></p>
        <p class="auth-text"><strong>Email :</strong> <?= htmlspecialchars((string)$user['email'], ENT_QUOTES, 'UTF-8') ?></p>
        <p class="auth-text"><strong>Compte cree le :</strong> <?= htmlspecialchars((string)$user['created_at'], ENT_QUOTES, 'UTF-8') ?></p>
        <a class="nav-pill" href="index.php">Retour accueil</a>
        <a class="auth-back" href="logout.php">Se deconnecter</a>
    </main>
</body>
</html>
