"""

    AUTHOR:

        Khushal P Soonderji

    DATE:

        Thursday, 19th Dec., 2024

    OBJECTIVE:

        To handle all messages from one place.

    REFERENCES:

        N/A

    DOWNLOADS:

        N/A

"""

#   *****************************************************************************************************************
#  *****                                                                                                          ****
# ***                                                   IMPORT                                                      ***
#  *****                                                                                                          ****
#   *****************************************************************************************************************


# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")

# My async utils:
from utils_v2.database.async_mongo_v2 import AsyncMongo
from utils_v2.cache.async_redis_cache_v2 import AsyncRedisCache

# Controllers:
from controllers_v2.core.base import CoreBaseModel
from controllers_v2.core.auth_token import CoreAuthTokenController

# Models:
from models.core.message import CoreMessageModel

# To work with MongoDB:
from bson import ObjectId

# To work with datatypes:
from typing import List

# To make HTTP requests:
import httpx


#   *****************************************************************************************************************
#  *****                                                                                                          ****
# ***                                           MACROS / ONE-TIME INIT                                              ***
#  *****                                                                                                          ****
#   *****************************************************************************************************************


# --- Nothing Yet


#   *****************************************************************************************************************
#  *****                                                                                                          ****
# ***                                                VARIABLES                                                      ***
#  *****                                                                                                          ****
#   *****************************************************************************************************************


# --- Nothing Yet


#   *****************************************************************************************************************
#  *****                                                                                                          ****
# ***                                                  FUNCTIONS                                                    ***
#  *****                                                                                                          ****
#   *****************************************************************************************************************


# --- Nothing Yet


#   *****************************************************************************************************************
#  *****                                                                                                          ****
# ***                                                   CLASSES                                                     ***
#  *****                                                                                                          ****
#   *****************************************************************************************************************


class CoreMessageController(CoreAuthTokenController):

    # ┏┓┓      ┓┏
    # ┃ ┃┏┓┏┏  ┃┃┏┓┏┓┏
    # ┗┛┗┗┻┛┛  ┗┛┗┻┛ ┛

    # For MongoDB:
    MESSAGES_COLLECTION = "_messages"

    # ┏┓
    # ┃ ┏┓┏┓┏╋┏┓┓┏┏╋┏┓┏┓
    # ┗┛┗┛┛┗┛┗┛ ┗┻┗┗┗┛┛

    def __init__(
            self,
            cache: AsyncRedisCache = None,
            http_client: httpx.AsyncClient = None,
            alert_url: str = None,
            base_filter: dict = None,
            debug: bool = True,
            debug_prefix: str = "Message (C) | ",
            debug_only_errors: bool = True
    ):

        """
        This is the foundational controller of all message controllers. You must structure individual message
        controllers through this structure. Individual message controllers would be for things like mails, SMS messages,
        chat app messages, etc.
        :param cache: The object to use for caching results from database calls.
        :param http_client: The HTTP client
        :param base_filter: The basic filter that will be applied to all fetching/updating queries. WARNING: THE BASE
            FILTER WILL ALWAYS BE APPLIED AUTOMATICALLY. SET THIS UP WISELY.
        :param debug: Whether, or not, you would like to print debugging messages:
        :param debug_prefix: The prefix to print with the debugging messages.
        :param debug_only_errors: Whether you would like to print only error messages or all messages.
        :return: None.
        """

        # Accept the base filter:
        self._base_filter = base_filter or {}

        # Invoke the parent's constructor:
        CoreAuthTokenController.__init__(
            self,
            cache = cache,
            http_client = http_client,
            alert_url = alert_url,
            base_filter = base_filter,
            debug = debug,
            debug_prefix = debug_prefix,
            debug_only_errors = debug_only_errors
        )

    # ┏┓┳┓┳┳┳┓      ┏┓
    # ┃ ┣┫┃┃┃┃  ━━  ┃ ┏┓┏┓┏┓╋┏┓
    # ┗┛┛┗┗┛┻┛      ┗┛┛ ┗ ┗┻┗┗

    async def save_one_message(
            self,
            mongo_data_conn: AsyncMongo,
            message: CoreMessageModel,
            session = None
    ) -> ObjectId:

        """
        Simply insert one message document into the database.
        :param mongo_data_conn: The instance of the database connector to use for the operation.
        :param message: The message to save into the database.
        :param session: In case you need to perform this operation as a transaction, pass a session here.
        :return: The object id of the inserted document.
        """

        # Simply insert the document:
        return await mongo_data_conn.insert_one(
            collection = self.MESSAGES_COLLECTION,
            document = message.model_dump(),
            raise_exception = True,
            session = session
        )

    async def bulk_operate_messages(
            self,
            mongo_data_conn: AsyncMongo,
            mongo_operations: list
    ) -> int:

        """
        Needed in cases like forcing re-sync of mails where you need to perform actions like bulk replacements of
        existing documents. Not recommended to use. Please use very carefully to ensure document integrity.
        :param mongo_data_conn: The instance of the database connector to use for the operation.
        :param mongo_operations: The list operations that are supported by MongoDB's Bulk Write system.
        :return: The no. of documents affected.
        """

        return await mongo_data_conn.bulk_write(
            collection = self.MESSAGES_COLLECTION,
            requests = mongo_operations,
            raise_exception = True
        )

    # ┏┓┳┓┳┳┳┓      ┳┓     •
    # ┃ ┣┫┃┃┃┃  ━━  ┣┫┏┓╋┏┓┓┏┓┓┏┏┓
    # ┗┛┛┗┗┛┻┛      ┛┗┗ ┗┛ ┗┗ ┗┛┗

    async def count_messages(
            self,
            mongo_data_conn: AsyncMongo,
            token_ids: List[ObjectId | str] = None,
            additional_filter: dict = None
    ) -> int:

        """
        Just counts the no. of messages that match a given set of conditions.
        :param mongo_data_conn: The instance of the database connector to use for the operation.
        :param token_ids: The token ids of the accounts from which these messages must be fetched.
        :param additional_filter: Any addition filters to use.
        :return: The no. of messages that match the given conditions.
        """

        # We cannot allow counting without any filter whatsoever:
        if token_ids is None and not additional_filter:
            raise ValueError("Cannot operate without some filter.")

        # Prepare the filter:
        filter_json = {}
        if token_ids is not None:
            if not isinstance(token_ids, list): token_ids = [token_ids]
            filter_json["tokenId"] = {"$in": token_ids}
        if self._base_filter:
            for k, v in self._base_filter.items(): filter_json[k] = v
        if additional_filter:
            for k, v in additional_filter.items(): filter_json[k] = v

        # Get the count of the documents that match the criteria:
        count = await mongo_data_conn.count(
            collection = self.MESSAGES_COLLECTION,
            filter = filter_json,
            raise_exception = True
        )

        # Done here:
        return count

    async def get_message_previews(
            self,
            mongo_data_conn: AsyncMongo,
            token_ids: List[ObjectId | str] = None,
            limit: int = 100,
            skip: int = 0,
            additional_filter: dict = None,
            projection: dict = None
    ) -> List[CoreMessageModel] | None:

        """
        Fetches many messages in one call, but leaves out the full payloads.
        :param mongo_data_conn: The instance of the database connector to use for the operation.
        :param token_ids: The token ids of the accounts from which these messages must be fetched.
        :param limit: The max. no. of messages to retrieve in this call.
        :param skip: The no. of initial messages to skip. Useful for pagination.
        :param additional_filter: Any addition filters to use.
        :param projection: To decide what is picked from each document. WARNING: THIS MAY BREAK THE BEHAVIOUR OF THE
            CORE MESSAGE MODEL. USE CAREFULLY.
        :return: The list of messages (as the message model). This list can be empty.
        """

        # We cannot allow counting without any filter whatsoever:
        if token_ids is None and not additional_filter:
            raise ValueError("Cannot operate without some filter.")

        # Prepare the filter:
        filter_json = {}
        if token_ids is not None:
            if not isinstance(token_ids, list): token_ids = [token_ids]
            filter_json["tokenId"] = {"$in": token_ids}
        if self._base_filter:
            for k, v in self._base_filter.items(): filter_json[k] = v
        if additional_filter:
            for k, v in additional_filter.items(): filter_json[k] = v

        # We fetch the messages that are identified by a specific token id,
        # with the specified fetching limits, while enforcing the sorting condition:
        records = await mongo_data_conn.find_many(
            collection = self.MESSAGES_COLLECTION,
            filter = filter_json,
            limit = limit,
            skip = skip,
            sort = {"ts": -1},
            projection = projection or {
                "_id": True,
                "ts": True,
                "syncTs": True,
                "tokenId": True,
                "serviceType": True,
                "client": True,
                "clientMessageId": True,
                "clientThreadId": True,
                "isSent": True,
                "isBroadcast": True,
                "sentSuccessfully": True,
                "sender": True,
                "chat": True,
                "snippet": True,
                "aiSnippet": True,
                "tags": True
            },
            raise_exception = True
        )

        # Convert the fetched records to instances of the data model and return:
        for record in records: record["message"] = {}
        return [CoreMessageModel(**record) for record in records]

    async def get_messages(
            self,
            mongo_data_conn: AsyncMongo,
            token_ids: List[ObjectId | str] = None,
            limit: int = 100,
            skip: int = 0,
            additional_filter: dict = None,
            projection: dict = None
    ) -> List[CoreMessageModel] | None:

        """
        Fetches many full messages in one call.
        :param mongo_data_conn: The instance of the database connector to use for the operation.
        :param token_ids: The token ids of the accounts from which these messages must be fetched.
        :param limit: The max. no. of messages to retrieve in this call.
        :param skip: The no. of initial messages to skip. Useful for pagination.
        :param additional_filter: Any addition filters to use.
        :param projection: To decide what is picked from each document. WARNING: THIS MAY BREAK THE BEHAVIOUR OF THE
            CORE MESSAGE MODEL. USE CAREFULLY.
        :return: The list of messages (as the message model). This list can be empty.
        """

        # We cannot allow counting without any filter whatsoever:
        if token_ids is None and not additional_filter:
            raise ValueError("Cannot operate without some filter.")

        # Prepare the filter:
        filter_json = {}
        if token_ids is not None:
            if not isinstance(token_ids, list): token_ids = [token_ids]
            filter_json["tokenId"] = {"$in": token_ids}
        if self._base_filter:
            for k, v in self._base_filter.items(): filter_json[k] = v
        if additional_filter:
            for k, v in additional_filter.items(): filter_json[k] = v

        # We fetch the messages that are identified by a specific token id,
        # with the specified fetching limits, while enforcing the sorting condition:
        records = await mongo_data_conn.find_many(
            collection = self.MESSAGES_COLLECTION,
            filter = filter_json,
            limit = limit,
            skip = skip,
            sort = {"ts": -1},
            projection = projection,
            raise_exception = True
        )

        # Convert the fetched records to instances of the data model and return:
        return [CoreMessageModel(**record) for record in records]

    async def get_message(
            self,
            mongo_data_conn: AsyncMongo,
            message_id: ObjectId | str,
            additional_filter: dict = None,
            projection: dict = None
    ) -> CoreMessageModel | None:

        """
        Gets one message if you know its message id.
        :param mongo_data_conn: The instance of the database connector to use for the operation.
        :param message_id: The id of the message that needs to be read.
        :param additional_filter: Any addition filters to use.
        :param projection: To decide what is picked from each document. WARNING: THIS MAY BREAK THE BEHAVIOUR OF THE
            CORE MESSAGE MODEL. USE CAREFULLY.
        :return: The contents of that one message in a structured format.
        """

        # Start by preparing the filter:
        filter_json = {"_id": ObjectId(message_id)}
        if self._base_filter:
            for k, v in self._base_filter.items(): filter_json[k] = v
        if additional_filter:
            for k, v in additional_filter.items(): filter_json[k] = v

        # We fetch the whole payload of that one message:
        record = await mongo_data_conn.find_one(
            collection = self.MESSAGES_COLLECTION,
            filter = filter_json,
            projection = projection,
            raise_exception = True
        )

        # If no such message was found:
        if record is None: return None

        # If a record was found,
        # we return it as our data model:
        return CoreMessageModel(**record)

    # ┏┓┳┓┳┳┳┓      ┳┳   ┓
    # ┃ ┣┫┃┃┃┃  ━━  ┃┃┏┓┏┫┏┓╋┏┓
    # ┗┛┛┗┗┛┻┛      ┗┛┣┛┗┻┗┻┗┗
    #                 ┛

    # We don't support updating messages themselves,
    # but we will allow updating fields like tags, marking as read or unread, etc.

    async def update_tags(
            self,
            mongo_data_conn: AsyncMongo,
            message_id: ObjectId | str,
            unset_tags: List[str] = None,
            set_tags: List[str] = None,
            additional_filter: dict = None
    ) -> bool:

        """
        Updates the tags on one message. The tags to remove are processed first, the ones to add are processed later.
        :param mongo_data_conn: The instance of the database connector to use for the operation.
        :param message_id: The id of the message that needs to be read.
        :param unset_tags: The tags to remove from the message.
        :param set_tags: The tags to add to the message.
        :param additional_filter: Any addition filters to use.
        :return: True if the update was successful, else False.
        """

        # Start by preparing the filter:
        filter_json = {"_id": ObjectId(message_id)}
        if self._base_filter:
            for k, v in self._base_filter.items(): filter_json[k] = v
        if additional_filter:
            for k, v in additional_filter.items(): filter_json[k] = v

        # Update the tags:
        return await mongo_data_conn.update_one(
            collection = self.MESSAGES_COLLECTION,
            filter = filter_json,
            update = [{
                "$set": {
                    "tags": {
                        "$let": {
                            "vars": {
                                "removed_tags": {
                                    "$setDifference": [
                                        "$tags",
                                        unset_tags
                                    ]
                                }
                            },
                            "in": {
                                "$setUnion": [
                                    "$$removed_tags",
                                    set_tags
                                ]
                            }
                        }
                    }
                }
            }],
            raise_exception = True
        )

    # ┏┓┳┓┳┳┳┓      ┳┓  ┓
    # ┃ ┣┫┃┃┃┃  ━━  ┃┃┏┓┃┏┓╋┏┓
    # ┗┛┛┗┗┛┻┛      ┻┛┗ ┗┗ ┗┗

    # No support whatsoever for deleting messages.


#   *****************************************************************************************************************
#  *****                                                                                                          ****
# ***                                               MAIN PROGRAM                                                    ***
#  *****                                                                                                          ****
#   *****************************************************************************************************************


if __name__ == "__main__":

    pass
