<?php
session_start();
require_once 'config.php';

$circleId    = (int)($_SESSION['circle_id'] ?? 1);
$currentUser = (int)($_SESSION['user_id'] ?? 0);

echo "user_id: $currentUser<br>";
echo "circle_id: $circleId<br><br>";

// Test canDelete
$canDelete = false;
try {
    $adminChk = $pdo->prepare("SELECT admin, tier FROM users WHERE id=?");
    $adminChk->execute([$currentUser]);
    $adminRow = $adminChk->fetch();
    $canDelete = $adminRow && ((int)$adminRow['admin'] === 1 || strtolower($adminRow['tier']) === 'admin');
    echo "canDelete (admin): " . ($canDelete ? 'OUI' : 'NON') . "<br>";
    if (!$canDelete) {
        $oracleChk = $pdo->prepare("SELECT oracle_id FROM circles WHERE id=?");
        $oracleChk->execute([$circleId]);
        $oracleRow = $oracleChk->fetch();
        $canDelete = $oracleRow && (int)$oracleRow['oracle_id'] === $currentUser;
        echo "canDelete (oracle): " . ($canDelete ? 'OUI' : 'NON') . "<br>";
    }
} catch (Exception $e) { echo "ERREUR: " . $e->getMessage() . "<br>"; }

// Test myReports
try {
    $rStmt = $pdo->query("SELECT message_id FROM chat_reports WHERE reporter_id = {$currentUser}");
    $myReports = array_column($rStmt->fetchAll(), 'message_id');
    echo "Mes signalements: " . count($myReports) . "<br><br>";
} catch (Exception $e) { echo "ERREUR chat_reports: " . $e->getMessage() . "<br>"; }

// Charger derniers messages
$stmt = $pdo->prepare("SELECT m.id, m.user_id, m.username, m.content FROM messages m WHERE m.circle_id=? ORDER BY m.id DESC LIMIT 5");
$stmt->execute([$circleId]);
$msgs = $stmt->fetchAll();

echo "<br>Derniers messages et boutons:<br>";
foreach ($msgs as $msg) {
    $msgId = $msg['id'];
    $isMe  = $msg['user_id'] == $currentUser;
    echo "#{$msgId} [{$msg['username']}]: ";
    if (!$isMe) {
        $reported = in_array($msgId, $myReports ?? []);
        echo $reported ? "[✓ Signalé]" : "[🚩 Flag dispo]";
    } else {
        echo "[Mon message - pas de flag]";
    }
    if ($canDelete) echo " [🗑 Delete dispo]";
    echo "<br>";
}
?>
