#  RubigramClient - Rubika API library for python
#  Copyright (C) 2025-present Javad <https://github.com/DevJavad>
#  Github - https://github.com/DevJavad/rubigram


from typing import Optional, Callable
from rubigram.filters import Filter
import rubigram


class OnInlineMessage:
    def on_inline_message(
        self: "rubigram.Client",
        filters: Optional["Filter"] = None
    ):
        """
        **Decorator for handling inline messages.**
            `@client.on_inline_message(filters.button("my_button"))`

        This decorator registers a function to handle inline message events
        that match the specified filters. If no filters are provided,
        all inline messages will be handled.

        Args:
            filters (`Optional[Filter]`):
                Filter to determine which inline messages should be handled.
                If None, all inline messages will be processed.

        Returns:
            Callable: The decorator function.

        Example:
        .. code-block:: python

            # Handle specific button clicks
            @client.on_inline_message(filters.button("start_btn"))
            async def handle_start_button(client, update):
                await client.send_message(
                    update.chat_id, 
                    "Start button was clicked!"
                )

            # Handle inline messages with text
            @client.on_inline_message(filters.text)
            async def handle_inline_text(client, update):
                print(f"Inline message text: {update.text}")

            # Handle all inline messages (no filter)
            @client.on_inline_message()
            async def handle_all_inline(client, update):
                print(f"Inline message received from: {update.sender_id}")

        Note:
            Inline messages are typically generated by button clicks in
            inline keyboards or other interactive elements. The button ID
            can be accessed via `update.aux_data.button_id`.
        """
        def decorator(func: Callable) -> Callable:
            async def wrapper(
                client: "rubigram.Client",
                update: "rubigram.types.InlineMessage"
            ):
                if filters is None or await filters(client, update):
                    await func(client, update)
                    return True
                return False

            self.inline_message_handlers.append(wrapper)
            return func
        return decorator