"""

    AUTHOR:

        Khushal P Soonderji

    DATE:

        Thursday, 19th Dec., 2024.

    OBJECTIVE:

        To provide a structure to send mails.

    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, EmailStr
from typing import Optional, Literal, List

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

# To work with date and time:
import datetime

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


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


# RegEx Patterns:
REGEX_SESSION_TOKEN = r"^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$"


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


# --- Nothing Yet


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


class MailSendRequestHeaders(BaseModel):

    sessionToken: str = Field(
        description = "the session token of the user who is requesting the service",
        pattern = REGEX_SESSION_TOKEN,
        frozen = True,
        alias = "X-Session-Token"
    )

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

    class Config:
        extra = "allow"

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


# ---------------------------------------------------------------------------------------------------------------------


class MailSendInlineFiles(BaseModel):

    key: str = Field(
        description = "the key in the form data under which the file has been sent",
        frozen = True
    )

    cid: str = Field(
        description = "the content id to assign to the file",
        frozen = True
    )

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

    class Config:
        extra = "forbid"


# ---------------------------------------------------------------------------------------------------------------------


class MailSendRequestData(BaseModel):

    tokenKey: ObjectId = Field(
        description = "the account identifier (Mongo ObjectId)",
        frozen = True
    )

    html: str = Field(
        description = "a valid html string that will become the mail's body",
        frozen = True
    )

    to: List[EmailStr] = Field(
        description = "the recipient of your mail",
        # default = None,
        # validate_default = True,
        frozen = True
    )

    cc: List[EmailStr] = Field(
        description = "the list of ids to add as cc",
        default = None,
        validate_default = True,
        frozen = True
    )

    bcc: List[EmailStr] = Field(
        description = "the list of ids to add as bcc",
        default = None,
        validate_default = True,
        frozen = True
    )

    inlineFiles: List[MailSendInlineFiles] = Field(
        description = (
            "when sending files, this will be a list of keys whose "
            "associated files will be treated as inline files"
        ),
        default = None,
        validate_default = True,
        frozen = True
    )

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

    class Config:
        extra = "forbid"
        arbitrary_types_allowed = True

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

    @field_validator("tokenKey", mode = "before")
    def parse_oid(cls, value):
        try: value = ObjectId(value)
        except: pass
        return value

    @field_validator("inlineFiles", mode = "before")
    def parse_json(cls, value):

        # If we get a null value or an empty string:
        if value is None: return []
        if isinstance(value, str):
            if not value.strip(): return []

        # If we get a properly populated string:
        try: value = json.from_string(value)
        except: pass

        # Done here:
        return value

    @field_validator("to", "cc", "bcc", mode = "before")
    def parse_recipients(cls, value):

        # If we get a null value or an empty string:
        if value is None: return []
        if isinstance(value, str):
            if not value.strip(): return []

        # If we get a properly populated string:
        try: value = json.from_string(value)
        except: value = [value.strip()]

        # Done here:
        return value


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


if __name__ == "__main__":

    pass
