import logging
from typing import Any, Awaitable, Callable, Dict

from aiogram import BaseMiddleware
from aiogram.types import Message, Update

logger = logging.getLogger(__name__)


class LoggingMiddleware(BaseMiddleware):
    async def __call__(
        self,
        handler: Callable[[Message, Dict[str, Any]], Awaitable[Any]],
        event: Message,
        data: Dict[str, Any]
    ) -> Any:
        logger.info("="*60)
        logger.info(f"[MIDDLEWARE] Incoming message from user {event.from_user.id}")
        logger.info(f"[MIDDLEWARE] Username: @{event.from_user.username}")
        logger.info(f"[MIDDLEWARE] Text: {event.text}")
        logger.info(f"[MIDDLEWARE] Message ID: {event.message_id}")
        logger.info(f"[MIDDLEWARE] Chat ID: {event.chat.id}")
        logger.info("="*60)

        result = await handler(event, data)

        logger.info(f"[MIDDLEWARE] Handler completed for message {event.message_id}")
        return result
