"""

    AUTHOR:

        Khushal P Soonderji

    DATE:

        Saturday, 4th Jan., 2025.

    OBJECTIVE:

        To provide a data model for describing the tokens to be used for ICICI Breeze.

    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, model_validator, AwareDatetime
from typing import Optional, Literal, Union, Dict, List, Any

# Related to Google:
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials

# 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
import dateparser

# To make API calls:
import httpx


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


# --- Nothing Yet


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


# --- Nothing Yet


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


class ICICIBreezeAuthTokens(BaseModel):

    userId: str = Field(
        description = "The id of the user as assigned by ICICI Breeze.",
        alias = "idirect_userid"
    )

    name: str = Field(
        description = "The name of the user.",
        alias = "idirect_user_name"
    )

    displayName: str | None = Field(
        description = "The short display name of the user.",
        default = None,
        alias = "idirect_user_name"
    )

    displayPictureUrl: str | None = Field(
        description = "The display picture of the user.",
        default = None,
        alias = "avatar_url"
    )

    loginTs: AwareDatetime = Field(
        description = "The time (utc) at which this user logged in.",
        alias = "idirect_lastlogin_time"
    )

    sessionToken: str | None = Field(
        description = "The main access token to be used in actual requests.",
        default = None
    )

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

    class Config:
        extra = "ignore"
        populate_by_name = True

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

    @field_validator("loginTs", mode = "before")
    def parse_dates(cls, value):
        if not isinstance(value, datetime.datetime):
            parsed = date_time.parse_date_time(
                value,
                date_formats = ["%d-%b-%Y %H:%M:%S", "%Y-%m-%d %H:%M:%S"],
                timezone = date_time.TIMEZONE_IST
            )
            value = parsed if isinstance(parsed, datetime.datetime) else dateparser.parse(value)
        if isinstance(value, datetime.datetime): value = date_time.to_timezone(value, date_time.TIMEZONE_UTC)
        return value

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

    pass

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

    pass


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


if __name__ == "__main__":

    print("BHOPLI & MUCHHI")

    sample_token = {
        "exg_trade_date": {
            "NSE": "06-Jan-2025",
            "BSE": "06-Jan-2025",
            "FNO": "06-Jan-2025",
            "NDX": "06-Jan-2025"
        },
        "exg_status": {
            "NSE": "C",
            "BSE": "C",
            "FNO": "Y",
            "NDX": "C"
        },
        "segments_allowed": {
            "Trading": "Y",
            "Equity": "Y",
            "Derivatives": "Y",
            "Currency": "Y"
        },
        "idirect_userid": "BHUSHaI4",
        "idirect_user_name": "BHUSHAN C THAKKAR",
        "idirect_ORD_TYP": "N",
        "idirect_lastlogin_time": "04-Jan-2025 15:41:53",
        "mf_holding_mode_popup_flg": "N",
        "commodity_exchange_status": "Y",
        "commodity_trade_date": "06-Jan-2025",
        "commodity_allowed": "C"
    }

    model = ICICIBreezeAuthTokens(**sample_token)
    print(json.to_string(model.model_dump(), default = str))
