<?php
session_start();
require_once 'config.php';
require_once 'functions.php';
requireLogin();
$user = getConnectedUser($pdo);
requireAdmin($user);

echo "Avant header<br>";

// Tester chaque partie du header manuellement
echo "Test 1 - site_appearance: ";
try {
    $themeStmt = $pdo->query("SELECT * FROM site_appearance LIMIT 1");
    $siteTheme = $themeStmt ? ($themeStmt->fetch() ?: []) : [];
    echo "OK (" . count($siteTheme) . " cols)<br>";
} catch(Exception $e) {
    echo "ERREUR: " . $e->getMessage() . "<br>";
    $siteTheme = [];
}

echo "Test 2 - profile_bg_image: ";
try {
    $bgStmt = $pdo->prepare("SELECT profile_bg_image FROM users WHERE id = ?");
    $bgStmt->execute([$_SESSION['user_id']]);
    $bgRow = $bgStmt->fetchColumn();
    echo "OK - valeur: " . ($bgRow ?: 'NULL') . "<br>";
} catch(Exception $e) {
    echo "ERREUR: " . $e->getMessage() . "<br>";
}

echo "Test 3 - unread PM: ";
try {
    $pmStmt = $pdo->prepare("SELECT COUNT(*) FROM private_messages WHERE receiver_id=? AND is_read=0");
    $pmStmt->execute([$_SESSION['user_id']]);
    $unreadPM = $pmStmt->fetchColumn();
    echo "OK - $unreadPM non lus<br>";
} catch(Exception $e) {
    echo "ERREUR: " . $e->getMessage() . "<br>";
}

echo "Test 4 - circle info: ";
try {
    $circleId = $_SESSION['circle_id'] ?? 1;
    $cStmt = $pdo->prepare("SELECT name FROM circles WHERE id = ?");
    $cStmt->execute([$circleId]);
    $cName = $cStmt->fetchColumn();
    echo "OK - $cName<br>";
} catch(Exception $e) {
    echo "ERREUR: " . $e->getMessage() . "<br>";
}

echo "Test 5 - notifications: ";
try {
    $nStmt = $pdo->prepare("SELECT COUNT(*) FROM notifications WHERE user_id=? AND is_read=0");
    $nStmt->execute([$_SESSION['user_id']]);
    $nbNotif = $nStmt->fetchColumn();
    echo "OK - $nbNotif notifs<br>";
} catch(Exception $e) {
    echo "ERREUR: " . $e->getMessage() . "<br>";
}

echo "<br>Maintenant include header.php...<br>";
include 'header.php';
echo "<br>APRES HEADER OK<br>";
?>
