"""

    AUTHOR:

        Khushal P Soonderji

    DATE:

        Saturday, 7th Dec., 2024.

    OBJECTIVE:

        To define how messages will be stored in the database.

    REFERENCES:

        N/A

    DOWNLOADS:

        N/A

"""


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


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

# For making data behaviour_models:
from pydantic import BaseModel, Field, field_validator, PastDatetime, AwareDatetime, constr
from typing import Optional, Literal, Union, List, Any

# My utils:
from utils_v2.string import regex
from utils_v2.date_time import date_time

# To work with MongoDB:
from bson.objectid import ObjectId

# Data models:
from models.core.ai.llm import LLMOutput

# To work with date and time:
import datetime


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


# --- Nothing Yet


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


# --- Nothing Yet


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


class CoreMessageModel(BaseModel):

    messageId: ObjectId = Field(
        description = "the id of the document in mongodb that holds this information",
        frozen = True,
        default = None,
        alias = "_id"
    )

    ts: AwareDatetime = Field(
        description = "the time (utc) at which this message was sent by the sender",
        frozen = True
    )

    syncTs: AwareDatetime = Field(
        description = "the time (utc) at which this message was pulled and stored in your server",
        frozen = True,
        default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
    )

    tokenId: ObjectId = Field(
        description = "the id of the auth token that is associated with this message",
        frozen = True
    )

    serviceType: Literal["email", "sms", "chat"] = Field(
        description = "the kind of service this message was sent/received from",
        frozen = True
    )

    client: Literal[
        "gmail", "outlook",  # ...................... Mail Clients
        "telegram", "whatsapp",  # .................. Chat Clients
        "nimbusSmsIndia", "savvyBulkSmsKenya",  # ... SMS Clients
    ] = Field(
        description = "the third-part client that was used",
        frozen = True
    )

    clientMessageId: str | int | None = Field(
        description = "how the client identifies this message",
        frozen = True
    )

    clientThreadId: str | int | None = Field(
        description = "how the client identifies the chat/thread in which this message was sent/received",
        frozen = True,
        default = None
    )

    isSent: bool = Field(
        description = "to understand whether this message was an incoming message or outgoing message",
        frozen = False,
        default = False
    )

    isBroadcast: bool = Field(
        description = "to understand if this message was broadcasted or sent one-to-one",
        frozen = True,
        default = False
    )

    sentSuccessfully: bool | None = Field(
        description = "when a message is an outgoing message, this indicates if the message was send successfully",
        frozen = False,
        default = False
    )

    sender: str | None = Field(
        description = "the name of the sender; null if you are the sender",
        frozen = True
    )

    recipient: str | None = Field(
        description = "the name of the recipient; null if you are the recipient",
        frozen = True,
        default = None
    )

    chat: str | None = Field(
        description = "the name of the chat where the message was exchanged; relevant in chat apps like telegram",
        frozen = True
    )

    message: dict = Field(
        description = "the actual content(s) of the message; will differ for each service/client",
        frozen = True
    )

    snippet: str = Field(
        description = "a truncated version of the actual textual content of the message",
        frozen = False
    )

    aiSnippet: LLMOutput | dict | None = Field(
        description = "holds a short summary generated by an llm",
        frozen = False,
        default = None
    )

    tags: List[Any] = Field(
        description = "a list of keywords to apply to this file/dir to filter it later",
        frozen = False,
        default = [],
        examples = ["urgent", "otp", "GST"]
    )

    # ┏┓    ┏•
    # ┃ ┏┓┏┓╋┓┏┓
    # ┗┛┗┛┛┗┛┗┗┫
    #          ┛

    class Config:
        extra = "forbid"
        arbitrary_types_allowed = True

    def model_dump(self, *args, **kwargs):
        return super().model_dump(*args, by_alias = True, **kwargs)

    # ┏┓           •
    # ┃┃┏┓┏┓┏┓┏┓┏┓╋┓┏┓┏
    # ┣┛┛ ┗┛┣┛┗ ┛ ┗┗┗ ┛
    #       ┛

    @property
    def full(self):
        return {
            "messageId": str(self.messageId),
            "ts": self.ts.isoformat(),
            "serviceType": self.serviceType,
            "client": self.client,
            "clientMessageId": self.clientMessageId,
            "clientThreadId": self.clientThreadId,
            "isSent": self.isSent,
            "isBroadcast": self.isBroadcast,
            "sentSuccessfully": self.sentSuccessfully,
            "sender": self.sender,
            "chat": self.chat,
            "message": self.message,
            "snippet": self.snippet,
            "aiSnippet": self.aiSnippet,
            "tags": self.tags
        }

    @property
    def preview(self):
        return {
            "messageId": str(self.messageId),
            "ts": self.ts.isoformat(),
            "serviceType": self.serviceType,
            "client": self.client,
            "clientMessageId": self.clientMessageId,
            "clientThreadId": self.clientThreadId,
            "isSent": self.isSent,
            "isBroadcast": self.isBroadcast,
            "sentSuccessfully": self.sentSuccessfully,
            "sender": self.sender,
            "chat": self.chat,
            "snippet": self.snippet,
            "aiSnippet": self.aiSnippet,
            "tags": self.tags
        }

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

    @field_validator("ts", "syncTs", mode = "before")
    def parse_date_time(cls, value):
        return date_time.parse_date_time(input_value = value, timezone = date_time.TIMEZONE_UTC)

    @field_validator("tokenId", mode = "before")
    def parse_oid(cls, value):
        try:
            if isinstance(value, str):
                value = ObjectId(value)
        except: pass
        return value

    @field_validator("tags", mode = "before")
    def validate_tags(cls, value):
        if value is None: value = []
        return value


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


if __name__ == "__main__":

    from utils_v2.string import json

    message = CoreMessageModel(
        ts = date_time.get_current_utc_date_time(as_string = False),
        tokenId = "67519cf3a7804fcbc6f12452",
        serviceType = "email",
        client = "gmail",
        clientMessageId = 123,
        clientThreadId = 456,
        isSent = False,
        message = {
            "from": "bhopli@gmil.com",
            "to": "hello@thecaoffice.com",
            "message": "Hello, World!"
        },
        sender = "Polki",
        chat = "T6 Cats",
        preview = "Hi, there! How do you do?"
    )

    print("MESSAGE MODEL:", json.to_string(message.model_dump(), default = str))
