"""

    AUTHOR:

        Khushal P Soonderji

    DATE:

        Fri, 14th Nov, 2025

    OBJECTIVE:

        To build APIs to serve In/Out Reports from Cosec's Web portal.

    REFERENCES:

        N/A

    DOWNLOADS:

        N/A

    NOTES:

        N/A

"""


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


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

# For system-level activities:
import os

# For using Quart:
from quart import Blueprint, current_app, request

# My utils:
from utils_v2.string import json
from utils_v2.date_time import date_time
from utils_v2.system import files
from utils_v2.api.codes import StatusCodes, HttpCodes
from utils_v2.api.response import ResponseModel
from utils_v2.api.async_quart import (
    set_api_version,
    read_input,
    get_session_info,
    log_request_to_mongo,
    log_chain_to_mongo,
    should_not_be_under_maintenance,
    only_whitelisted_ips,
    limit_rate,
    validate_input,
    handle_cancelled_request
)

# Models:
from backend.models.api.common import SimpleCosecCredentialsHeaders
from backend.models.api.reports.in_out_report import CosecInOutReportRequestData

# Cosec-related:
from cosec_web.cosec_web import CosecWeb

# Common:
from backend.shared import constants

# To work with dat and time:
import time
import datetime

# For asynchronous activities:
import asyncio


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


# Related to Quart:
in_out_report_bp = Blueprint("in_out_report", __name__)


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


# --- Nothing Yet


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


@in_out_report_bp.record_once
def init(blueprint_setup_state):

    # This gets called when the blueprints is registered.
    # Consider this to be a one-time setup for the whole blueprints:
    pass


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


@in_out_report_bp.route("/generate", methods = ["GET"])
@set_api_version(api_version = "1.0.0")
@read_input(sanitize_headers = False, sanitize_data = False)
# @get_session_info(key = "X-User-Id", session_coro = "get_session")
# @log_request_to_mongo(
#     attr_name = "logs_mongo",
#     project = constants.PROJECT_NAME,
#     log_type = constants.MODULE_NAME,
#     operation = "ytLnkAddApi",
#     log_input = True,
#     log_output = True,
#     sensitive_keys = None
# )
# @log_chain_to_mongo(attr_name = "logs_mongo")
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
@validate_input(
    # header_validator = lambda x: SimpleCosecCredentialsHeaders(**x),
    # data_validator = lambda x: CosecInOutReportRequestData(**x)
)
@handle_cancelled_request()
async def in_out_report_generate(
        inbound_headers: SimpleCosecCredentialsHeaders | dict = None,
        inbound_data: CosecInOutReportRequestData | dict = None,
        inbound_files: dict = None,
        **kwargs
):

    """
    To automate the web browser interaction with Cosec's web portal and return a JSON of the actual report content.
    :param inbound_headers: auto-extracted by the decorators.
    :param inbound_data: auto-extracted by the decorators.
    :param inbound_files: auto-extracted by the decorators.
    :param kwargs: Any number of extra inputs supplied by the decorators.
    :return: A standard response structure.
    """

    # Start by assuming failure:
    success = False
    report_data = None

    # Figure out the paths:
    proj_dir = files.get_parent_directory(
        files.get_file_directory(include_filename = False),
        depth = 4
    )
    cache_file = os.path.join(proj_dir, "local", "cache", "in_out_summary_cache.json")

    # Read the report:
    try: report_data = json.from_file(cache_file)
    except Exception as e: pass

    # Note down the status of success or failure:
    success = True if report_data else False

    # ┳┓
    # ┣┫┏┓┏┏┓┏┓┏┓┏┏┓
    # ┛┗┗ ┛┣┛┗┛┛┗┛┗
    #      ┛

    if success: return ResponseModel(
        status_code = StatusCodes.OK,
        http_code = HttpCodes.SUCCESS,
        data = report_data,
        message = f"Successfully fetched {len(report_data)} records."
    )
    else: return ResponseModel(
        status_code = StatusCodes.FAILED,
        http_code = HttpCodes.INTERNAL_SERVER_ERROR,
        message = f"Failed to fetch records. Please try again."
    )


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


if __name__ == "__main__":

    pass
