# -*- coding: utf-8 -*-

import logging

from libbot import json_read
from pyrogram import filters
from pyrogram.client import Client
from pyrogram.enums.chat_action import ChatAction
from pyrogram.types import Message

from modules.utils import string, userGet

logger = logging.getLogger(__name__)


@Client.on_message(
    ~filters.scheduled
    & filters.command(["topup", "refill", "поповнити"], prefixes=["/", ""])  # type: ignore
)
async def command_topup(app: Client, msg: Message):
    try:
        if "card" in (await json_read("data/database.json"))[str(msg.from_user.id)]:
            await app.send_chat_action(chat_id=msg.chat.id, action=ChatAction.TYPING)
            await msg.reply_text(
                (await string("top_up")).format(str(userGet(msg.from_user.id, "card")))
            )
            logger.info(f"User {str(msg.from_user.id)} requested top up")
        else:
            await msg.reply_text(
                (await string("card_not_linked")).format(await string("get_number"))
            )
            logger.info(
                f"User {str(msg.from_user.id)} tried to request top up without card set"
            )
    except Exception as exp:
        await msg.reply_text(str(exp))
