<?php
define('BOT_TOKEN', '8203311972:AAHJ9SpoeuJ35WVMGeRp1gq23DsgxSRY8yw');
define('API_URL', 'https://api.telegram.org/bot' . BOT_TOKEN . '/');

$content = file_get_contents('php://input');
file_put_contents(__DIR__ . '/log.txt', $content . PHP_EOL, FILE_APPEND);
$update = json_decode($content, true);

if (!$update) { http_response_code(200); exit(); }

if (isset($update['message']['chat']['id'])) {
    $chat_id = $update['message']['chat']['id'];
    $text = trim($update['message']['text']);
    $file = __DIR__ . '/users.txt';
    if (!file_exists($file)) file_put_contents($file, '');
    $chat_ids = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    if (!in_array($chat_id, $chat_ids)) {
        file_put_contents($file, $chat_id . PHP_EOL, FILE_APPEND);
    }

    switch ($text) {
        case '/start': sendMainMenu($chat_id); break;
        case '/giris': sendGuncelGiris($chat_id); break;
        case '/istatistik': sendStats($chat_id); break;
        default:
            sendMessage($chat_id, "🎯 Komutlar:\n/start\n/giris\n/istatistik");
    }
}

function buildKeyboard() {
    $buttons = [
        ['text' => '💸 ALOBET 💸', 'url' => 'https://t2m.io/Alobot10'],
        ['text' => '🚀 ENBET 🚀', 'url' => 'https://t2m.io/enbot10'],
        ['text' => '✨ BAHİSFAİR ✨', 'url' => 'https://t2m.io/bfbot10'],
        ['text' => '🍒 VIPSLOT 🍒', 'url' => 'https://r2m.io/vipbot10'],
        ['text' => '🌟 TAMBET 🌟', 'url' => 'https://t2m.io/tbot10'],
        ['text' => '🔸 ATLASBET 🔸', 'url' => 'https://t2m.io/Bot10'],
        ['text' => '🌓 SONBAHİS 🌓', 'url' => 'https://t2m.io/Sonbot10'],
        ['text' => '🔰 CASİBOX 🔰', 'url' => 'https://t2m.io/cboxbot10'],
        ['text' => '🐳 MEYBET 🐳', 'url' => 'https://t2m.io/meybot10'],
        ['text' => '🎲 CASINOBONANZA 🎲', 'url' => 'https://t2m.io/cbbot10com'],
        ['text' => '⚜️ CASINOPOP ⚜️', 'url' => 'https://t2m.io/popbot10'],
        ['text' => '🔱 BETASUS 🔱', 'url' => 'https://t2m.io/Asusbot10'],
        ['text' => '🧞 CASINOROYAL 🧞', 'url' => 'https://t2m.io/royalbot10'],
        ['text' => '🍀 NESİNECASINO 🍀', 'url' => 'https://t2m.io/nsnbot10'],
        ['text' => '💫 BETCIO 💫', 'url' => 'https://t2m.io/ciobot10'],
        ['text' => '💙 BETZULA 💙', 'url' => 'https://t2m.io/bot10'],
        ['text' => '🇹🇷 MİLLİBAHİS 🇹🇷', 'url' => 'https://r2m.io/millibot10'],
        ['text' => '🔮 CASİNOMİLYON 🔮', 'url' => 'https://t2m.co/cmbot10'],
        ['text' => '🦋 BETPLAY 🦋', 'url' => 'https://adresplay.com/bot10'],
        ['text' => '💎 LUXBET 💎', 'url' => 'https://t2m.io/luxbbb'],
        ['text' => '🐺 GAMEOFBET 🐺', 'url' => 'https://t2m.io/Gobbot10']
    ];
    shuffle($buttons);
    $rows = [];
    while ($chunk = array_splice($buttons, 0, 2)) {
        $rows[] = $chunk;
    }
    return ['inline_keyboard' => $rows];
}

function sendMainMenu($chat_id) {
    $text = "Casino ve Canlı Bahis Tutkunlarına Özel\n".
             "En Popüler ve Editör Onaylı Güvenilir 21 Site Bağlantısı\n\n".
             "🏆  7/24 Kesintisiz En Yüksek RTP ve Canlı Bahis Oranları\n".
             "💸 Özel Bonuslar ve Promosyon Kodları\n".
             "🎁 Günlük Ödüller : Ücretsiz Çark ve Nakit ödüllerle dolu.\n".
             "🛡 Sadece Lisanslı Markalar\n\n".
             "📣 Etkinlik kanalımız : <a href=\"https://t.me/+Ps_Ao5Kv77g3OWJi\">Güncel Giriş</a>\n".
             "⚡️ Anlık proma : <a href=\"https://t2m.io/guncelgirisi\">Web Sitemiz için Giriş</a>\n\n".
             "🔥 En iyi 21 Lisanslı Site ve Fırsatlara Göz Atın! 👇🏻";
    sendMessage($chat_id, $text, buildKeyboard(), true);
}

function sendGuncelGiris($chat_id) {
    sendMessage($chat_id, "🔗 <b>Güncel Giriş:</b>\nhttps://t2m.io/guncelgirisi", null, true);
}

function sendStats($chat_id) {
    $file = __DIR__ . '/users.txt';
    if (!file_exists($file)) {
        sendMessage($chat_id, "📊 Kullanıcı verisi yok.");
        return;
    }
    $users = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $count = count($users);
    sendMessage($chat_id, "📊 Toplam kullanıcı: <b>$count</b>", null, true);
}

function sendMessage($chat_id, $text, $keyboard = null, $no_preview = false) {
    $data = [
        'chat_id' => $chat_id,
        'text' => $text,
        'parse_mode' => 'HTML'
    ];
    if ($keyboard) $data['reply_markup'] = json_encode($keyboard, JSON_UNESCAPED_UNICODE);
    if ($no_preview) $data['disable_web_page_preview'] = true;
    file_get_contents(API_URL . 'sendMessage?' . http_build_query($data));
}
?>