<?php
// Debug Script to Check DB Column and Insert Test Message
require 'db.php';

$output = "Debugging Start...\n";

try {
    // 1. Check Column
    $stmt = $pdo->query("SHOW COLUMNS FROM messages LIKE 'reply_to'");
    $col = $stmt->fetch();
    if ($col) {
        $output .= "Column 'reply_to' EXISTS.\n";
    } else {
        $output .= "Column 'reply_to' MISSING. Attempting to add...\n";
        $pdo->exec("ALTER TABLE messages ADD COLUMN reply_to INT NULL DEFAULT NULL");
        $output .= "Column added.\n";
    }

    // 2. Check content of recent messages to see if reply_to is being saved
    $stmt = $pdo->query("SELECT id, content, reply_to FROM messages ORDER BY id DESC LIMIT 5");
    $msgs = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $output .= "Recent Messages:\n";
    foreach ($msgs as $m) {
        $output .= "ID: {$m['id']}, ReplyTo: " . ($m['reply_to'] ?? 'NULL') . "\n";
    }

} catch (PDOException $e) {
    $output .= "Error: " . $e->getMessage() . "\n";
}

file_put_contents('debug_result.txt', $output);
echo "Debug complete. Check debug_result.txt";
?>
