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(["timeout_verify"], prefixes=["/"])  # type: ignore
)
async def command_timeout_verify(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 len(message.command) < 2 or not message.command[1].isdigit():
        await message.reply_text(
            app._("timeout_verify_invalid", "messages", locale=locale), quote=True
        )
        return

    timeout = int(message.command[1])

    if timeout < 10 or timeout > 600:
        await message.reply_text(
            app._("timeout_invalid_number", "messages", locale=locale), quote=True
        )
        return

    await message.reply_text(
        app._("timeout_verify_set", "messages", locale=locale).format(timeout=timeout)
    )

    logger.info(
        "Verification timeout in group %s has been set to %s",
        group.id,
        timeout,
    )

    await group.set_timeout_verify(timeout)
