from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
import aiohttp
from datetime import datetime

# === Komut logu (davranisi degistirmez, sadece kaydeder) ===
LOG_FILE = "commands.log"

def log_command(update, command_name):
    try:
        user = update.effective_user
        chat = update.effective_chat
        msg = update.effective_message
        with open(LOG_FILE, "a", encoding="utf-8") as f:
            print(
                f"{datetime.now().isoformat(timespec='seconds')} | "
                f"chat_type={getattr(chat, 'type', '')} | chat_id={getattr(chat, 'id', '')} | "
                f"user_id={getattr(user, 'id', '')} | username={getattr(user, 'username', '')} | "
                f"name={getattr(user, 'first_name', '') or ''} | "
                f"command={command_name} | text={(getattr(msg, 'text', '') or '').strip()}",
                file=f,
            )
    except Exception:
        pass


# /btag <kullanıcı> <btag>
async def btag(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    log_command(update, "btag")
    try:
        if len(context.args) < 2:
            await update.effective_message.reply_text("Kullanım: /btag <kullanıcıadı> <btag>")
            return

        username = context.args[0].strip()
        btag_val = context.args[1].strip()
        if not username or not btag_val:
            await update.effective_message.reply_text("Lütfen geçerli bir kullanıcı adı ve btag girin.")
            return

        # bilgilendirme mesajı
        await update.effective_message.reply_text(
            f"Kullanıcı: {username}\nBtag: {btag_val} sorgulanıyor.. 🔍"
        )

        url = "https://fiksturbetbot.com/anasayfa/btagsorgu"
        async with aiohttp.ClientSession() as session:
            await session.get(url, params={"username": username, "btag": btag_val}, timeout=10)

    except:
        pass  # Hata mesajı gösterilmez

# /tc <kullanıcı>
async def tc(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    log_command(update, "tc")
    try:
        if not context.args:
            await update.effective_message.reply_text("Kullanım: /tc <kullanıcı adı>")
            return

        username = context.args[0].strip()
        if not username:
            await update.effective_message.reply_text("Lütfen geçerli bir değer girin.")
            return

        # bilgilendirme mesajı
        await update.effective_message.reply_text(
            f"Kullanıcı için TC Kimlik bilgileri sorgulanıyor.. 🔍 {username}"
        )

        url = "https://fiksturbetbot.com/anasayfa/tcsorgu"
        async with aiohttp.ClientSession() as session:
            await session.get(url, params={"username": username}, timeout=10)

    except:
        pass  # Hata mesajı gösterilmez

# --- BOT ---
app = ApplicationBuilder().token("8905492287:AAHT_pQA583CwhjXA91hIfeh3kdOnwwfBMY").build()
app.add_handler(CommandHandler("btag", btag))
app.add_handler(CommandHandler("tc", tc))
app.run_polling()
