import typing
import importlib.resources

import chitragupt.core.logger as logger

uninitialized_logger = logger.GuptLogger(
    config=logger.LoggerConfig(
        name="Uninitialized System", level=logger.LogLevel.DEBUG
    )
)


def read_text(
    path: str,
    package: typing.Optional[str] = None,
) -> typing.Optional[str]:
    """
    Read the contents of a text file.

    Parameters
    ----------
    path : str
        The path to the file. If `package` is provided, this is the filename inside the package.
    package : Optional[str], optional
        The package name if the file is a part of the package data, by default None.

    Returns
    -------
    Optional[str]
        The contents of the file as a string, or None if an error occurs.
    """
    try:
        if package:
            # Read from package data (e.g., assets inside the package)
            with importlib.resources.open_text(package, path) as f:
                return f.read() or None
        else:
            # Read from filesystem
            with open(path, "r", encoding="utf-8") as f:
                return f.read() or None
    except FileNotFoundError:
        uninitialized_logger.log(
            f"File not found: {path}", logger.LogType.ERROR
        )
        return None
    except Exception as e:
        uninitialized_logger.log(
            f"Exception while reading file '{path}': {str(e)}",
            logger.LogType.WARNING,
        )
        return None


__version__: typing.Final = "0.3"
__developer__: typing.Final = "xsyncio"
__readme__: typing.Final = read_text("README.md")
__banner__: typing.Final[str] = read_text("banner.txt", "chitragupt.assets")
__announcement_url__: typing.Final = "https://raw.githubusercontent.com/xsyncio/ChitraGupt-Plugin-Garden/refs/heads/main/announcements.json"
__plugin_garden_url__: typing.Final = (
    "https://github.com/xsyncio/ChitraGupt-Plugin-Garden"
)
__license__: typing.Final = read_text("LICENSE.md")
__copyright__: typing.Final = f"Copyright (c) 2025-present {__developer__}"
__plugin_garden_url_slug__: typing.Final = "xsyncio/ChitraGupt-Plugin-Garden"
__repo_slug__: typing.Final = "xsyncio/chitragupt"
