import os

files = {}

# افزودن هندلر دریافت File ID برای ادمین
files["handlers/admin.py"] = """from aiogram import Router, F, Bot
from aiogram.types import Message, CallbackQuery
from aiogram.filters import Command
from config import ADMIN_ID, SUBSCRIPTION_PLANS
from keyboards import get_admin_panel_keyboard, get_payment_review_keyboard, get_withdraw_review_keyboard
from database import get_subscription_stats, get_pending_requests, approve_payment, reject_payment, get_pending_withdraws, process_withdraw

router = Router()

@router.message(Command("admin"))
async def cmd_admin(message: Message):
    if message.from_user.id != ADMIN_ID: return
    stats = await get_subscription_stats()
    text = f"🔧 <b>پنل مدیریت</b>\\n\\n👥 کل کاربران: {stats['total_users']}\\n⭐ اشتراک‌ها: {stats['active_subs']}\\n📋 در انتظار فیش: {stats['pending_requests']}\\n💳 در انتظار تسویه: {stats['pending_withdraws']}"
    await message.answer(text, parse_mode="HTML", reply_markup=get_admin_panel_keyboard())

# --- ابزار جدید ادمین: گرفتن File ID هر ویدیو/گیف/استیکر ---
@router.message(F.from_user.id == ADMIN_ID, F.animation | F.video | F.sticker | F.document)
async def get_file_id_handler(message: Message):
    file_id = None
    file_type = ""
    
    if message.animation:
        file_id = message.animation.file_id
        file_type = "GIF / انیمیشن"
    elif message.video:
        file_id = message.video.file_id
        file_type = "ویدیو"
    elif message.sticker:
        file_id = message.sticker.file_id
        file_type = "استیکر"
    elif message.document:
        file_id = message.document.file_id
        file_type = "فایل"

    if file_id:
        text = f"🎬 <b>شناسه فایل تلگرامی ({file_type}):</b>\\n\\n<code>{file_id}</code>\\n\\n💡 <i>این کد را کپی کرده و در فایل data/exercises.py بگذارید.</i>"
        await message.reply(text, parse_mode="HTML")

@router.callback_query(F.data == "admin_pending")
async def admin_pending(callback: CallbackQuery):
    if callback.from_user.id != ADMIN_ID: return
    reqs = await get_pending_requests()
    if not reqs:
        await callback.message.edit_text("✅ درخواست فیش جدیدی نیست.", reply_markup=get_admin_panel_keyboard())
        return
    r = reqs[0]
    p = SUBSCRIPTION_PLANS.get(r["plan_key"], {})
    await callback.message.edit_text(f"📋 <b>رسید #{r['id']}</b>\\n👤 @{r.get('username')}\\n💰 {p.get('price_display')}", parse_mode="HTML", reply_markup=get_payment_review_keyboard(r['id']))

@router.callback_query(F.data == "admin_withdraws")
async def admin_withdraws(callback: CallbackQuery):
    if callback.from_user.id != ADMIN_ID: return
    reqs = await get_pending_withdraws()
    if not reqs:
        await callback.message.edit_text("✅ درخواست تسویه جدیدی نیست.", reply_markup=get_admin_panel_keyboard())
        return
    r = reqs[0]
    await callback.message.edit_text(f"💳 <b>تسویه #{r['id']}</b>\\n👤 ID: {r['user_id']}\\n💰 مبلغ: {r['amount']:,} تومان\\n💳 کارت: <code>{r['card_info']}</code>", parse_mode="HTML", reply_markup=get_withdraw_review_keyboard(r['id']))

@router.callback_query(F.data.startswith("admin_approve_"))
async def admin_approve(callback: CallbackQuery, bot: Bot):
    if callback.from_user.id != ADMIN_ID: return
    req_id = int(callback.data.replace("admin_approve_", ""))
    res = await approve_payment(req_id, callback.from_user.id)
    if res:
        await callback.message.edit_text(f"✅ درخواست #{req_id} تایید شد.", reply_markup=get_admin_panel_keyboard())
        try: await bot.send_message(res["user_id"], "🎉 <b>اشتراک شما فعال شد!</b>", parse_mode="HTML")
        except: pass

@router.callback_query(F.data.startswith("admin_reject_"))
async def admin_reject(callback: CallbackQuery):
    if callback.from_user.id != ADMIN_ID: return
    req_id = int(callback.data.replace("admin_reject_", ""))
    await reject_payment(req_id)
    await callback.message.edit_text(f"❌ درخواست #{req_id} رد شد.", reply_markup=get_admin_panel_keyboard())

@router.callback_query(F.data.startswith("admin_w_approve_"))
async def w_approve(callback: CallbackQuery, bot: Bot):
    if callback.from_user.id != ADMIN_ID: return
    req_id = int(callback.data.replace("admin_w_approve_", ""))
    res = await process_withdraw(req_id, "approved")
    if res:
        await callback.message.edit_text(f"✅ تسویه #{req_id} انجام شد.", reply_markup=get_admin_panel_keyboard())
        try: await bot.send_message(res["user_id"], f"💳 مبلغ {res['amount']:,} تومان به حساب شما واریز شد!", parse_mode="HTML")
        except: pass

@router.callback_query(F.data.startswith("admin_w_reject_"))
async def w_reject(callback: CallbackQuery, bot: Bot):
    if callback.from_user.id != ADMIN_ID: return
    req_id = int(callback.data.replace("admin_w_reject_", ""))
    res = await process_withdraw(req_id, "rejected")
    if res:
        await callback.message.edit_text(f"❌ تسویه #{req_id} رد شد.", reply_markup=get_admin_panel_keyboard())
        try: await bot.send_message(res["user_id"], f"❌ درخواست تسویه شما رد شد و {res['amount']:,} تومان به کیف پولتان برگشت.", parse_mode="HTML")
        except: pass
"""

for path, content in files.items():
    with open(path, "w", encoding="utf-8") as f: f.write(content)
print("✅ ابزار استخراج File ID برای ادمین فعال شد!")
