from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
import requests


async def saatlik_deposit(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    await update.message.reply_text(
        f'Raporunuzu hazırlıyorum, lütfen bekleyin {update.effective_user.first_name} Bey'
    )
    url = "https://telegram.bonusavva.pro/anasayfa/saatlik"

    try:
        r = requests.get(url, timeout=30)

        if r.status_code == 200:
            await update.message.reply_text("✅ Rapor isteğiniz başarıyla iletildi.")
        else:
            body = r.text or ""
            if len(body) > 3000:
                body = body[:3000] + "\n\n...[kısaltıldı]"

            await update.message.reply_text(
                f"⚠️ Rapor isteği iletildi ancak beklenmeyen bir cevap alındı. (HTTP {r.status_code})\n"
                f"\n--- Hata İçeriği Başlangıcı ---\n"
                f"{body}\n"
                f"--- Hata İçeriği Sonu ---"
            )

    except requests.RequestException as e:
        # Buraya düşerse istek çoğu zaman backend'e gitmiş OLUP
        # sadece zamanında yanıt dönmemiş olabilir (timeout, connection reset vs.)
        print("RAPOR İSTEĞİ HATASI:", repr(e))

        await update.message.reply_text(
            "⚠️ Rapor isteğiniz muhtemelen iletildi ancak sunucudan zamanında yanıt alınamadı "
            "veya bağlantı sırasında bir hata oluştu.\n"
            f"Hata detayı (debug için): {type(e).__name__}: {e}"
        )


async  def gunsonu(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    await update.message.reply_text(f'Raporunuzu hazırlıyorum, lütfen bekleyin {update.effective_user.first_name} Bey')
    url = "https://telegram.bonusavva/anasayfa/gunluksorgu"
    r = requests.get(url)

    
async  def tc(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    print("hello")
    """Add a job to the queue."""
    chat_id = update.effective_message.chat_id
    try:
        # args[0] should contain the time for the timer in seconds
        username = len(context.args[0])
        if username <= 0:
            await update.effective_message.reply_text("Lütfen geçerli bir değer girin.")
            return
        await update.message.reply_text(f'Kullanıcı için TC Kimlik bilgileri sorgulanıyor.. 🔍'+str(context.args[0]))
        url = "https://showbahis.website/anasayfa/tcsorgu?username="+str(context.args[0])
        r = requests.get(url)
        

    except (IndexError, ValueError):
        await update.effective_message.reply_text("Kullanım: /tc <kullanıcı adı>")


async def btag(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    print("hello")
    chat_id = update.effective_message.chat_id
    try:
        # en az iki argüman: kullanıcı adı ve callcenter
        if len(context.args) < 2 or not context.args[0].strip():
            await update.effective_message.reply_text(
                "Kullanım: /btag <kullanıcı adı> <callcenter ismi>"
            )
            return

        username   = context.args[0].strip()
        callcenter = context.args[1].strip()

        # tek bir mesaj atıp referansını alalım
        msg = await update.effective_message.reply_text(
            f'Kullanıcı için işlem yapılıyor.. 🔍 {username}'
        )

        # URL'i doğru şekilde birleştir
        url = (
            "https://telegram.bonusavva.pro/anasayfa/btag_guncelle"
            f"?username={username}&btag={callcenter}"
        )
        r = requests.get(url)

        # cevabı kontrol edip aynı mesajı güncelle
        if r.status_code == 200 and "İşlem Başarılı." in r.text:
            await msg.edit_text(
                f'✅ {username} kullanıcısının {callcenter} btag güncellemesi sağlandı.'
            )

    except (IndexError, ValueError):
        await update.effective_message.reply_text(
            "Kullanım: /btag <kullanıcı adı> <callcenter ismi>"
        )



async  def ip(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    print("hello")
    """Add a job to the queue."""
    chat_id = update.effective_message.chat_id
    try:
        # args[0] should contain the time for the timer in seconds
        username = len(context.args[0])
        if username <= 0:
            await update.effective_message.reply_text("Lütfen geçerli bir değer girin.")
            return
        await update.message.reply_text(f'Kullanıcı için IP bilgileri sorgulanıyor.. 🔍'+str(context.args[0]))
        url = "https://telegram.avvabonus.pro/anasayfa/ipsorgu?username="+str(context.args[0])
        r = requests.get(url)
        

    except (IndexError, ValueError):
        await update.effective_message.reply_text("Kullanım: /ip <kullanıcı adı>")
        

app = ApplicationBuilder().token("8245303437:AAEFvy2VPdHy-b45V9ehZuRmAndKGT8tbPA").build()

app.add_handler(CommandHandler("rapor", saatlik_deposit))
app.add_handler(CommandHandler("gunsonu", gunsonu))
app.add_handler(CommandHandler("ip", ip))
app.add_handler(CommandHandler("tc", tc))
app.add_handler(CommandHandler("btag", btag))

app.run_polling()
