import telebot
from flask import Flask
from telebot import TeleBot
from telebot.types import Message
from telebot import logger
from app.bot.constants import UserStates
from app.bot.states import UserStateManager
from app.bot.processors.my_subscriptions_processor import handle_my_subscriptions
from app.bot.utils.auth import auth


def register_handlers(bot: TeleBot,app: Flask, state_manager: UserStateManager):
    @bot.message_handler(commands=['subscribes'])
    @bot.message_handler(func=lambda msg: msg.text == "Мои подписки")
    def handle_my_subscriptions_button(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
        handle_my_subscriptions(message, bot,app, state_manager)