import logging
from aiogram.types import CallbackQuery
from aiogram_dialog import DialogManager
from sqlalchemy import select
from sqlalchemy.exc import SQLAlchemyError
from shopbot.bot.database.db import async_session
from shopbot.bot.database.models import Account
from shopbot.bot.states import PurchaseHistorySG
ACCOUNT_INFO_TEMPLATE = (
"Подробная информация об аккаунте:\n\n"
"💰 Цена: {price:.2f}₽\n\n"
"🔑 Логин: {login}\n"
"🔒 Пароль: {password}\n\n"
"🌍 Регион: {region}\n"
"🎮 Ранг: {current_rank}\n"
"🔪 Количество ножей: {knife_count}\n"
"🛡️ Количество агентов: {agent_count}\n"
"🖌️ Скины: {skin_count}\n"
"🗡️ Стоимость инвентаря: {inventory_value}VP\n"
"📈 Последний ранг: {last_rank}\n"
"🏅 Уровень: {level}\n"
)
async def _format_account_info(account: Account) -> str:
"""Формирует строку с информацией об аккаунте."""
return ACCOUNT_INFO_TEMPLATE.format(
price=account.price,
login=account.login,
password=account.password,
region=account.region,
current_rank=account.current_rank,
knife_count=account.knife_count,
agent_count=account.agent_count,
skin_count=account.skin_count,
inventory_value=account.inventory_value,
last_rank=account.last_rank,
level=account.level,
)
async def on_history_account_selected(
callback: CallbackQuery, selecte, manager: DialogManager, item_id: int
):
try:
if not isinstance(item_id, int):
item_id = int(item_id)
manager.current_context().dialog_data["selected_account_id"] = item_id
async with async_session() as session:
async with session.begin():
account = await session.scalar(
select(Account).where(Account.id == item_id)
)
if account:
manager.current_context().dialog_data[
"account_info"
] = await _format_account_info(account)
else:
logging.warning(f"Account with id {item_id} not found")
manager.current_context().dialog_data["account_info"] = (
"Аккаунт не найден!"
)
await manager.switch_to(PurchaseHistorySG.details)
except SQLAlchemyError as e:
logging.error(f"Database error: {e}", exc_info=True)
except Exception as e:
logging.error(f"An unexpected error occurred: {e}", exc_info=True)