# -*- coding: utf-8 -*-
"""
ارسال اعلان به ادمین‌ها.
- در بات اصلی: به گروه ادمین یا PV همه ADMIN_IDS
- در بات نماینده: به ادمین نماینده (برای رسیدها/پشتیبانی) یا به ADMIN_IDS اصلی (برای سفارشات)
"""

from config import ADMIN_IDS
import database as db


def _main_targets():
    group_id = db.get_setting("admin_group_id")
    if group_id:
        return [int(group_id)] if group_id.lstrip("-").isdigit() else [group_id]
    return list(ADMIN_IDS)


async def notify_admins(context, text=None, reply_markup=None,
                        photo_file_id=None, caption=None, order_notification=False):
    """
    order_notification=True  → همیشه به ADMIN_IDS اصلی (برای تکمیل سفارش)
    order_notification=False → به ادمین نماینده اگه در بات نماینده‌ایم، وگرنه به اصلی
    """
    bot_data = getattr(context, "application", context).bot_data if hasattr(context, "application") else {}
    reseller_admin = bot_data.get("reseller_admin_id") if bot_data else None

    if order_notification or not reseller_admin:
        targets = _main_targets()
    else:
        # رسید/پشتیبانی در بات نماینده → فقط به ادمین نماینده
        targets = [reseller_admin]

    for target in targets:
        try:
            if photo_file_id:
                await context.bot.send_photo(
                    target, photo_file_id,
                    caption=caption or text, reply_markup=reply_markup
                )
            else:
                await context.bot.send_message(target, text, reply_markup=reply_markup)
        except Exception:
            pass
