import logging

from pyrogram import filters
from pyrogram.types import Message

from classes.pyroclient import PyroClient
from modules.utils import is_permitted

logger = logging.getLogger(__name__)


@PyroClient.on_message(
    ~filters.scheduled
    & filters.group
    & filters.command(["ban_failed"], prefixes=["/"])  # type: ignore
)
async def command_ban_failed(app: PyroClient, message: Message):
    group = await app.find_group(message.chat.id)
    locale = group.select_locale(app, message.from_user)

    if not (await is_permitted(app, group, message=message)):
        await message.reply_text(
            app._("permission_denied", "messages", locale=locale), quote=True
        )
        return

    if group.ban_failed:
        await message.reply_text(
            app._("ban_failed_disabled", "messages", locale=locale)
        )
    else:
        await message.reply_text(app._("ban_failed_enabled", "messages", locale=locale))

    logger.info(
        "Ban on fail in group %s has been set to %s",
        group.id,
        not group.ban_failed,
    )

    await group.set_ban_failed(not group.ban_failed)
