import logging

from telebot import logger
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton, Message

from config import ADMINS_LIST
LOG_LEVELS = {
    "🔴 ERROR": logging.ERROR,
    "🟠 WARNING": logging.WARNING,
    "🟢 INFO": logging.INFO,
    "🔵 DEBUG": logging.DEBUG
}

def register_handlers(bot,app, state_manager):
    @bot.message_handler(commands=['debug'], func=lambda message: message.chat.id in ADMINS_LIST)
    def debug_handler(message):

        chat_id = message.chat.id
        markup = InlineKeyboardMarkup(row_width=1)
        buttons = [InlineKeyboardButton(text=level, callback_data=f"setlog_{level}") for level in LOG_LEVELS]
        cancel_button = InlineKeyboardButton(text="Отмена", callback_data="cancel_input")
        markup.add(*buttons)
        markup.add(cancel_button)
        bot.send_message(chat_id, "Выберите уровень логирования", reply_markup=markup)

    @bot.callback_query_handler(func=lambda call: call.data.startswith("setlog_"))
    def handle_log_level_callback(call):
        message_id = call.message.message_id
        level_text = call.data.replace("setlog_", "")
        if level_text in LOG_LEVELS:
            level = LOG_LEVELS[level_text]
            logger.setLevel(level)
            for handler in logger.handlers:
                handler.setLevel(level)
            bot.answer_callback_query(call.id, f"✅ Уровень логирования: {level_text}")
            bot.delete_message(call.message.chat.id, message_id)
            bot.send_message(call.message.chat.id, f"📋 Логгер переведён в режим: {level_text}")
        else:
            bot.answer_callback_query(call.id, "❌ Неизвестный уровень логирования")