"""Local configuration for the SPG Multi-Manager Support Bot.

IMPORTANT: Keep this file private. Never post your bot token in a public chat or GitHub.
"""

# ONLY THREE VALUES YOU NEED TO EDIT BEFORE FIRST LAUNCH:
# 1) BOT_TOKEN from @BotFather
# 2) OWNER_IDS: your numeric Telegram ID
# 3) HEAD_MANAGER_IDS: numeric ID(s) of head manager(s)
BOT_TOKEN = "8943984122:AAHU1TQkLaiEB0u0VEd27QUpl2SkWUPqbXU"
OWNER_IDS = [7338889499]
HEAD_MANAGER_IDS = []

TIMEZONE = "Asia/Kolkata"
DATABASE_PATH = "spg_support.sqlite3"
WELCOME_IMAGE_PATH = "spg_welcome_banner.jpg"

# Optional channel links shown on the welcome page. Joining is NEVER required to contact support.
# The chat_id fields remain for backwards compatibility and are not used to block users.
REQUIRED_CHANNELS = [
    {
        "name": "Official Prediction Channel",
        "url": "https://t.me/+18M0AUdGoho5OWE1",
        "chat_id": None,
    },
    {
        "name": "Daily Updates Channel",
        "url": "https://t.me/+1YYmqL_sFc01MjJl",
        "chat_id": None,
    },
    {
        "name": "Gift Codes Channel",
        "url": "https://t.me/+eJ_z--r8caRiZmNl",
        "chat_id": None,
    },
]

# Text/caption keyword detection. This is rule-based triage, not an AI model.
EMERGENCY_KEYWORDS = [
    "emergency", "urgent", "scam", "fraud", "hacked", "hacked account",
    "account hacked", "stolen", "cheated", "cheating", "thagi", "dhokha",
    "fraud hua", "account chori", "unauthorized", "unauthorised",
    "emergency help", "help emergency", "ठगी", "धोखा", "हैक", "खाता हैक",
]

DEPOSIT_KEYWORDS = ["deposit", "payin", "recharge", "add money", "fund", "payment", "जमा", "रिचार्ज"]
WITHDRAWAL_KEYWORDS = ["withdraw", "withdrawal", "payout", "cashout", "withdrawl", "निकासी", "पैसा नहीं आया"]
ACCOUNT_KEYWORDS = ["login", "log in", "register", "registration", "password", "otp", "account", "लॉगिन"]

# Bot welcome text. Keep below Telegram's photo-caption limit.
WELCOME_TEXT = (
    "<b>WELCOME TO SEIKH PRIME GAME (SPG) SUPPORT</b>\n\n"
    "<b>ENGLISH</b>\n"
    "Welcome to the official SPG Support Center. Choose a category above or send your issue directly. Our team handles deposit/finance, withdrawals, pending payments, account/login, scam/fraud reports, and other support matters. Text, voice notes, audio, video, photos, documents, and supported files are accepted.\n\n"
    "<b>हिंदी सहायता</b>\n"
    "नमस्कार! SPG सहायता केंद्र में आपका स्वागत है। ऊपर दिए गए विकल्पों में से कोई विषय चुनें या अपनी समस्या सीधे भेजें। हमारी टीम जमा/फाइनेंस, निकासी, लंबित भुगतान, अकाउंट/लॉगिन, धोखाधड़ी की रिपोर्ट और अन्य समस्याओं में सहायता करेगी। टेक्स्ट, वॉइस नोट, ऑडियो, वीडियो, फोटो और दस्तावेज़ भेज सकते हैं।\n\n"
    "<b>SECURITY / सुरक्षा</b>\n"
    "Never share passwords, OTPs, PINs, bank passwords, full card details, or recovery phrases. Never pay any manager or staff member personally; use only the official website/payment page.\n"
    "अपना पासवर्ड, OTP, PIN, बैंक पासवर्ड, कार्ड की पूरी जानकारी या रिकवरी फ्रेज साझा न करें। किसी भी मैनेजर या स्टाफ सदस्य के निजी UPI/बैंक खाते में भुगतान न करें; केवल आधिकारिक वेबसाइट/पेमेंट पेज का उपयोग करें।\n\n"
    "<b>URGENT HELP / तत्काल सहायता</b>\n"
    "For urgent or suspicious activity, write words such as EMERGENCY, URGENT, FRAUD, SCAM, HACKED, UNAUTHORIZED PAYMENT, ठगी, धोखा, or खाता हैक in your message. The system will mark it high priority automatically.\n"
    "तत्काल या संदिग्ध समस्या के लिए संदेश में EMERGENCY, URGENT, FRAUD, SCAM, HACKED, UNAUTHORIZED PAYMENT, ठगी, धोखा या खाता हैक लिखें। सिस्टम उसे अपने-आप प्राथमिकता देगा।\n\n"
    "<b>OPTIONAL CHANNELS / वैकल्पिक चैनल</b>\n"
    "Prediction, Daily Updates, and Gift Codes channels are optional. Joining any channel is not required to contact support.\n"
    "Prediction, Daily Updates और Gift Codes चैनल वैकल्पिक हैं। सहायता लेने के लिए किसी भी चैनल से जुड़ना ज़रूरी नहीं है।"
)

# cPanel WSGI webhook deployment. You do NOT need to edit these values.
# Run: python setup_webhook.py https://YOUR-SUBDOMAIN.example.com/webhook
# The URL is passed on the setup command, and a strong secret is generated/persisted
# privately in .spg_webhook_secret automatically. Keep that generated file private.
from pathlib import Path as _Path
import secrets as _secrets
import os as _os

def get_or_create_webhook_secret() -> str:
    override = _os.environ.get("SPG_WEBHOOK_SECRET", "").strip()
    if override:
        return override
    path = _Path(__file__).resolve().parent / ".spg_webhook_secret"

    def read_valid() -> str:
        try:
            value = path.read_text(encoding="utf-8").strip()
        except OSError:
            return ""
        if len(value) >= 32 and all(c.isalnum() or c in "_-" for c in value):
            return value
        return ""

    existing = read_valid()
    if existing:
        return existing

    value = _secrets.token_urlsafe(48)
    # O_EXCL avoids two cPanel workers overwriting each other's first-run secret.
    try:
        fd = _os.open(str(path), _os.O_WRONLY | _os.O_CREAT | _os.O_EXCL, 0o600)
    except FileExistsError:
        # Another worker may be writing the file; briefly wait for its completed write.
        import time as _time
        for _ in range(25):
            _time.sleep(0.04)
            existing = read_valid()
            if existing:
                return existing
        raise RuntimeError("Private webhook-secret file exists but is invalid/empty; remove it and restart once.")
    with _os.fdopen(fd, "w", encoding="utf-8") as stream:
        stream.write(value + "\n")
        stream.flush()
        _os.fsync(stream.fileno())
    try:
        _os.chmod(path, 0o600)
    except OSError:
        pass
    return value
