# ======================================================================
# CLOSE / CANCEL
# ======================================================================
from __future__ import annotations

from typing import Optional, Tuple, Dict, Any, List

from aiogram import Router, F, Bot
from aiogram.types import Message, CallbackQuery
from aiogram.filters import Command
from aiogram.utils.keyboard import InlineKeyboardBuilder
from aiogram.utils.markdown import hide_link
from aiogram.exceptions import TelegramBadRequest, TelegramForbiddenError

from aiogram.fsm.context import FSMContext
from aiogram.fsm.state import State, StatesGroup

from configs import settings, COMMANDS
from database import get_manager, AutoComment
from middleware.loggers import logger
from bot.filters.admin import IsAdmin
from bot.utils import log_action, tg_emoji

__all__ = ("router",)
CMD: str = "cancel"
router: Router = Router(name="channel_comments_router")

@router.callback_query(F.data == "menu:close")
async def close_menu_callback(callback: CallbackQuery, state: FSMContext) -> None:
    await state.clear()
    try:
        if callback.message:
            await callback.message.delete()
    except TelegramBadRequest:
        pass
    await callback.answer("❌ Меню закрыто")

@router.callback_query(F.data.casefold() == CMD)
@router.message(Command(*COMMANDS[CMD], prefix=settings.PREFIX, ignore_case=True), IsAdmin())
@log_action(action_name="START_COMMAND", log_args=True)
async def cancel_handler(message: Message, state: FSMContext) -> None:
    current_state = await state.get_state()
    if current_state is None:
        await message.answer("❌ Нечего отменять")
        return

    await state.clear()
    await message.answer("✅ Действие отменено")
