# app/bot/handlers/main_menu.py
from telebot.types import Message
from telebot import logger
from app.bot.constants import UserStates
from app.bot.keyboards.main_menu import get_main_menu
from app.bot.keyboards.settings_menu import get_settings_menu
from app.bot.states import UserStateManager
from app.bot.utils.auth import auth


def register_handlers(bot,app, state_manager: UserStateManager):
    @bot.message_handler(commands=['settings'])
    @bot.message_handler(func=lambda msg: msg.text == "Настройки")
    def handle_settings_menu(message: Message):
        chat_id = message.chat.id
        username = f"{message.from_user.username}" if message.from_user.username else "N/A"
        with app.app_context():
            if not auth(chat_id, app):
                bot.send_message(chat_id, "Вы не авторизованы для использования этого бота.")
                logger.warning(f"Неавторизованный пользователь {chat_id} @{username}")
                state_manager.set_state(chat_id, UserStates.REGISTRATION)
                return
            else:
                state_manager.set_state(chat_id, UserStates.SETTINGS_MENU)
                bot.send_message(message.chat.id,"Меню настроек:",reply_markup=get_settings_menu())

    @bot.message_handler(func=lambda msg: msg.text == "Назад")
    def handle_back_button(message: Message):
        bot.send_message(message.chat.id,"Главное меню", reply_markup=get_main_menu())