<?php
define('BOT_TOKEN', '8034753588:AAH1cIikUpb72eTsADuIKiy8EvWL6l29mjg');
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' => '💳 BETZULA 250TL',       'url' => 'https://t2m.io/bot10'],
        ['text' => '⚡ BETPLAY 500TL',       'url' => 'https://adresplay.com/bot10'],
        ['text' => '🔥 TAMBET 555TL',        'url' => 'https://t2m.io/tbot10'],
        ['text' => '💰 BETCIO 2025TL',       'url' => 'https://t2m.io/ciobot10'],
        ['text' => '💸 CASHBACK 500TL',      'url' => 'https://t2m.io/cashbackbot10'],
        ['text' => '🎰 NESINECASINO 250TL',  'url' => 'https://t2m.io/nsnbot10'],
        ['text' => '💶 CASINOMILYON 250TL',  'url' => 'https://t2m.co/cmbot10'],
        ['text' => '🎯 BAHISFAIR 777TL',     'url' => 'https://t2m.io/bfbot10'],
        ['text' => '🍀 MEYBET 150TL',        'url' => 'https://t2m.io/meybot10'],
        ['text' => '💲 SONBAHIS 555TL',      'url' => 'https://t2m.io/Sonbot10'],
        ['text' => '👑 VIPSLOT 777TL',       'url' => 'https://r2m.io/vipbot10'],
        ['text' => '🚀 ENBET 250TL',         'url' => 'https://t2m.io/enbot10'],
        ['text' => '✅ BETASUS 100TL',       'url' => 'https://t2m.io/betasusbot10'],
        ['text' => '🎲 CASINOBONANZA 100TL', 'url' => 'https://t2m.io/cbbot10com'],
        ['text' => '🪙 ATLASBET 250TL',      'url' => 'https://t2m.io/Bot10'],
        ['text' => '🎡 CASINOPOP 1500TL',    'url' => 'https://t2m.io/popbot10'],
        ['text' => '💸 ALOBET 500TL',        'url' => 'https://t2m.io/Alobot10'],
        ['text' => '🏆 MILLIBAHIS 500TL',    'url' => 'https://r2m.io/millibot10'],
        ['text' => '💎 CASINOROYAL 300TL',   'url' => 'https://t2m.io/royalbot10']
    ];
    shuffle($buttons);
    $rows = [];
    while ($chunk = array_splice($buttons, 0, 2)) {
        $rows[] = $chunk;
    }
    return ['inline_keyboard' => $rows];
}

function sendMainMenu($chat_id) {
    $text = "🎯 Bahis Tutkunlarına Merhaba!\n"
          . "En popüler ve güvenilir 20 bahis sitesine tek tıkla ulaş!\n\n"
          . "💥 Şans seninle olsun!\n"
          . "⚡️ Anlık kampanyalar\n"
          . "🎁 Bonuslar\n"
          . "💸 Kayıp iadeleri\n"
          . "🏆 Yüksek oranlar\n\n"
          . "👉 <a href=\"https://t2m.io/guncelgirisi\">GÜNCEL GİRİŞ</a>\n"
          . "📣 Resmi Kanal: https://t.me/+Ps_Ao5Kv77g3OWJi";
    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));
}