"""

    AUTHOR:

        Khushal P Soonderji

    DATE:

        Monday, 16th Dec., 2024.

    OBJECTIVE:

        To provide a structure to receive payment request details.

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

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

# Models:
from models.core.payment import CorePaymentModel

# To work with date and time:
import datetime

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

# To work with currencies:
import pycountry


#   *****************************************************************************************************************
#  *****                                                                                                          ****
# ***                                           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 PGPaymentRequestHeaders(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 PaymentRequestMetadata(BaseModel):

    idClient: str | int = Field(
        description = "account master id for the user's customer",
        frozen = True
    )

    inAccount: str | int = Field(
        description = "account master id for the user's bank account",
        frozen = True
    )

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

    class Config:
        extra = "allow"


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


class PGPaymentRequestData(BaseModel):

    tokenKey: ObjectId = Field(
        description = "the auth token to use to send this message",
        frozen = True,
    )

    customerName: str | None = Field(
        description = "the name of the registered customer who must make the payment",
        frozen = True,
        default = None
    )

    customerNo: str = Field(
        description = "the contact no. of the registered customer who must make the payment",
        frozen = True
    )

    payerNo: str | None = Field(
        description = (
            "the phone no. to which the payment request will go;"
            "if not specified, the value of 'customerNo' should be used"
        ),
        frozen = True,
        default = None
    )

    email: str | None = Field(
        description = "the e-mail id of the registered customer",
        pattern = regex.REGEX_EMAIL_ID,
        frozen = True,
        default = None
    )

    amount: float | int = Field(
        description = "the amount of money to be requested",
        frozen = True
    )

    currencyCode: str = Field(
        description = "the three-letter iso 4217 code to identify the currency",
        frozen = True,
        examples = ["INR", "USD", "KES"]
    )

    reference: str = Field(
        description = "some reference against which the payment was made",
        frozen = True
    )

    description: str = Field(
        description = "a short, human-readable description of the payment",
        frozen = True
    )

    metadata: PaymentRequestMetadata = Field(
        description = "any extra information about this payment",
        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("currencyCode", mode = "before")
    def validate_currency(cls, value):
        currency = pycountry.currencies.get(alpha_3 = value)
        if currency is None: raise ValueError("invalid currency code, please use iso 4217 standard")
        return value


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


class PaymentRequestOneResult(BaseModel):

    success: bool = Field(
        description = "whether, or not, the sms was successfully sent",
        default = False
    )

    message: str | None = Field(
        description = "a brief message to summarize the result of the process",
        default = None
    )

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

    class Config:
        extra = "forbid"


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


if __name__ == "__main__":

    pass
