<?php
// test_kirim_telegram.php
// Test kirim Telegram ke admin dan siswa

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

header('Content-Type: text/html; charset=utf-8');

echo "<h2>🧪 TEST KIRIM TELEGRAM</h2>";
echo "<hr>";

// 1. Test kirim ke admin
echo "<h3>1. Kirim ke Admin (Chat ID: " . TELEGRAM_CHAT_ID . ")</h3>";
$pesan_admin = "🧪 *TEST KE ADMIN*\n\n";
$pesan_admin .= "Waktu: " . date('d-m-Y H:i:s') . "\n";
$pesan_admin .= "Chat ID: " . TELEGRAM_CHAT_ID . "\n";
$pesan_admin .= "Status: Test dari sistem";

$result_admin = kirimTelegram($pesan_admin);
echo "<p>Hasil: <strong>" . $result_admin . "</strong></p>";

// 2. Test kirim ke siswa
echo "<h3>2. Kirim ke Siswa (ID: 8611512964)</h3>";
$pesan_siswa = "🧪 *TEST KE SISWA*\n\n";
$pesan_siswa .= "Waktu: " . date('d-m-Y H:i:s') . "\n";
$pesan_siswa .= "Chat ID: 8611512964\n";
$pesan_siswa .= "Status: Test dari sistem";

$result_siswa = kirimTelegramToUser($pesan_siswa, '8611512964');
echo "<p>Hasil: <strong>" . $result_siswa . "</strong></p>";

// 3. Cek status bot
echo "<h3>3. Cek Status Bot</h3>";
$url = "https://api.telegram.org/bot" . TELEGRAM_BOT_TOKEN . "/getMe";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);

$botInfo = json_decode($response, true);
if ($botInfo && $botInfo['ok']) {
    echo "<p style='color:green'>✅ Bot aktif: <strong>{$botInfo['result']['first_name']}</strong> (@{$botInfo['result']['username']})</p>";
} else {
    echo "<p style='color:red'>❌ Bot tidak aktif</p>";
}

echo "<hr>";
echo "<h3>📌 Catatan:</h3>";
echo "<ul>";
echo "<li>Pastikan bot sudah di-start: cari @NotifikasiSekolahBot dan kirim /start</li>";
echo "<li>Pastikan Anda sudah mengirim pesan ke bot</li>";
echo "<li>Cek getUpdates: <a href='https://api.telegram.org/bot" . TELEGRAM_BOT_TOKEN . "/getUpdates' target='_blank'>klik di sini</a></li>";
echo "</ul>";

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