# app/src/telegram/handlers/home.py

from __future__ import annotations

from aiogram import F, Router
from aiogram.fsm.context import FSMContext
from aiogram.types import Message

from src.telegram.live.active_screen import ActiveScreenManager
from src.telegram.menus import HOME_TEXT


router = Router(name="home")


async def _prepare_home_from_message(
    message: Message,
) -> bool:
    bot = message.bot

    if bot is None:
        return False

    await ActiveScreenManager.prepare_new_screen(
        screen="home",
        bot=bot,
        chat_id=message.chat.id,
    )

    return True


@router.message(F.text == "🏠 Главная")
async def open_home(
    message: Message,
    state: FSMContext,
) -> None:
    await state.clear()

    if not await _prepare_home_from_message(message):
        return

    sent_message = await message.answer(HOME_TEXT)

    ActiveScreenManager.register(
        screen="home",
        message=sent_message,
    )