<?php
// debug_check.php
// Cek status sistem

include "config.php";
include "telegram_config.php";

echo "<h2>🔍 DEBUG CHECK</h2>";
echo "<hr>";

// 1. Cek Session
echo "<h3>1. Session</h3>";
session_start();
echo "<pre>";
print_r($_SESSION);
echo "</pre>";

// 2. Cek Data Siswa
echo "<h3>2. Data Siswa</h3>";
$nisn = '9876543210';
$query = mysqli_query($conn, "SELECT id, nama, kelas, no_wa, telegram_id FROM siswa WHERE nisn='$nisn' AND status='aktif'");
$siswa = mysqli_fetch_assoc($query);
if ($siswa) {
    echo "<pre>";
    print_r($siswa);
    echo "</pre>";
    echo "Telegram ID: <strong>" . ($siswa['telegram_id'] ?: 'KOSONG') . "</strong><br>";
} else {
    echo "<p style='color:red'>❌ Siswa tidak ditemukan!</p>";
}

// 3. Test Kirim Telegram
echo "<h3>3. Test Kirim Telegram</h3>";
$test_pesan = "🧪 TEST DARI DEBUG\nWaktu: " . date('d-m-Y H:i:s');

$result_admin = kirimTelegram($test_pesan);
echo "Admin: " . $result_admin . "<br>";

if ($siswa && !empty($siswa['telegram_id'])) {
    $result_siswa = kirimTelegram($test_pesan, $siswa['telegram_id']);
    echo "Siswa: " . $result_siswa . "<br>";
}

// 4. Cek File Log
echo "<h3>4. File Log (debug_telegram.log)</h3>";
$log_file = __DIR__ . '/debug_telegram.log';
if (file_exists($log_file)) {
    $content = file_get_contents($log_file);
    $lines = explode("\n", $content);
    $last_lines = array_slice($lines, -30);
    echo "<pre style='background:#f5f5f5;padding:10px;max-height:300px;overflow:auto;font-size:12px;'>";
    echo htmlspecialchars(implode("\n", $last_lines));
    echo "</pre>";
    echo "<a href='debug_telegram.log' target='_blank'>Download log</a>";
} else {
    echo "<p>Belum ada file log</p>";
}

echo "<hr>";
echo "<p><a href='scan_wa_api.php' class='btn btn-primary'>Kembali ke Scan</a></p>";
?>