<?php
session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once 'config.php';
require_once 'functions.php';

requireLogin();
$userId = (int)$_SESSION['user_id'];
$user = getConnectedUser($pdo);
$circleId = $_SESSION['circle_id'] ?? 1;

try {
    $cStmt = $pdo->prepare("SELECT name, invite_code, oracle_id FROM circles WHERE id = ?");
    $cStmt->execute([$circleId]);
    $circle = $cStmt->fetch();
} catch (Exception $e) {
    $cStmt = $pdo->prepare("SELECT name, invite_code FROM circles WHERE id = ?");
    $cStmt->execute([$circleId]);
    $circle = $cStmt->fetch();
}

$isOracleOfCircle = $circle && (int)($circle['oracle_id'] ?? 0) === (int)$user['id'];
$canSeeCode = isAdmin($user) || $isOracleOfCircle;
$success = ''; $error = '';

include 'header.php';
echo "<div class='container mt-4'><h5 class='text-white'>TEST OK après header</h5>";

// Test section par section
echo "<p class='text-success'>Section 1 OK</p>";

if ($circle && $canSeeCode) {
    echo "<p class='text-success'>Section CODE CERCLE visible</p>";
    echo "<p class='text-info'>Code: " . htmlspecialchars($circle['invite_code'] ?? 'vide') . "</p>";
}

echo "<p class='text-success'>Section 2 OK</p>";

// Tester colonnes
$cols = $pdo->query("SHOW COLUMNS FROM users")->fetchAll(PDO::FETCH_COLUMN);
$hasFontPref = in_array('font_preference', $cols);
$hasBgImage  = in_array('profile_bg_image', $cols);
echo "<p class='text-info'>font_preference: " . ($hasFontPref ? 'OUI' : 'NON') . "</p>";
echo "<p class='text-info'>profile_bg_image: " . ($hasBgImage ? 'OUI' : 'NON') . "</p>";

echo "</div>";
include 'footer.php';
?>
