from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
import asyncio
import requests
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



async  def saatlik_deposit(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    log_command(update, "saatlik_deposit")
    await update.message.reply_text(f'Raporunuzu hazırlıyorum, lütfen bekleyin {update.effective_user.first_name} Bey')
    url = "https://telegram.grandoperabonus.com/anasayfa/saatlik_deposit"
    r = await asyncio.to_thread(requests.get, url, timeout=120)


async  def gunsonu(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    log_command(update, "gunsonu")
    await update.message.reply_text(f'Raporunuzu hazırlıyorum, lütfen bekleyin {update.effective_user.first_name} Bey')
    url = "https://telegram.grandoperabonus.com/anasayfa/gunluksorgu"
    r = await asyncio.to_thread(requests.get, url, timeout=120)

    
async  def tc(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    log_command(update, "tc")
    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://telegram.netotobonus.com/anasayfa/tcsorgu?username="+str(context.args[0])
        r = await asyncio.to_thread(requests.get, url, timeout=120)
        

    except (IndexError, ValueError):
        await update.effective_message.reply_text("Kullanım: /tc <kullanıcı adı>")

app = ApplicationBuilder().token("7878235478:AAHWYDrvmxHM8_jUwW5L0GTaJJeMNSRPTnw").build()

app.add_handler(CommandHandler("rapor", saatlik_deposit))

app.add_handler(CommandHandler("gunsonu", gunsonu))

app.add_handler(CommandHandler("tc", tc))

app.run_polling()
