# Configuration file for the Sphinx documentation builder.
# For a full list of options, see:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

import datetime
import os
import pathlib
import sys
import unittest.mock
import mars

# ==== PATH SETUP ======================================================================================================

project_path = pathlib.Path(mars.__file__).parent.parent
mars_module_path = pathlib.Path(mars.__file__).parent

# Add project paths to sys.path
sys.path.insert(0, str(project_path))
sys.path.insert(0, str(project_path / "completion" / "vscode"))

exclude_patterns = [
    "build",
    "autoapi_templates",
    "venv",
    ".*",
    "designer",
]  # Patterns to exclude from the build

# ==== MOCK MODULES ====================================================================================================

# Mock Maya-specific modules to prevent errors during documentation build
mock_modules_list = [
    "maya",
    "maya.cmds",
    "pymel",
    "pymel.core",
    "maya.api.OpenMaya",
    "maya.api.OpenMayaAnim",
    "maya.OpenMaya",
    "maya.OpenMayaAnim",
]

for mod in mock_modules_list:
    if mod in sys.modules:
        sys.modules[mod] = unittest.mock.Mock()

# ==== PROJECT INFORMATION ============================================================================================

project = project_path.name
copyright = (
    f"{datetime.datetime.now().year}, Bernat Mas Pastor"
)
author = "Bernat Mas Pastor"

# Import version from the package
from versionControl import package

release = package.VERSION

# ==== SPHINX EXTENSIONS ===============================================================================================

extensions = [
    "sphinx.ext.duration",  # Build duration
    "sphinx.ext.napoleon",  # Google-style docstrings
    "sphinx.ext.autodoc",  # Automatic documentation generation from docstrings. Using autoapi instead
    "autoapi.extension",  # AutoAPI extension for generating API documentation
    "sphinx.ext.graphviz",
    # "sphinx.ext.viewcode",
    "sphinx_copybutton",
    "sphinxcontrib.blockdiag",
    # "sphinx.ext.todo",
    # "sphinx.ext.doctest",
    "sphinx.ext.inheritance_diagram",
]

# ==== AUTODOC CONFIGURATION ===========================================================================================

autodoc_typehints = "both"  # Show type hints in the signature of functions and methods
# autodoc_class_signature = "separated"


# ==== Jinja2 tests ====================================================================================================


def contains(seq, item):
    return item in seq


def prepare_jinja_env(jinja_env) -> None:
    jinja_env.tests["contains"] = contains


autoapi_prepare_jinja_env = prepare_jinja_env

# ==== AUTOAPI CONFIGURATION ===========================================================================================

# surpress_warnings = ["autoapi.python_import_resolution"]  # Suppress warnings for not found modules
autoapi_template_dir = "autoapi_templates"  # Custom template directory for AutoAPI
autoapi_dirs = [
    str(mars_module_path),
]
autoapi_keep_files = True  # Keep the generated files after the build
autoapi_add_toctree_entry = False  # Do not add the generated files to the toctree
autoapi_type = "python"  # Generate documentation for Python files
autoapi_options = [
    "members",  # Include members (functions, classes, etc.) in the documentation
    "undoc-members",  # Include undocumented members
    "show-inheritance",  # Show inheritance hierarchy for classes
    # "show-inheritance-diagram",  # Show inheritance hierarchy for classes
    "special-members",  # Include special members (like __init__)
    "show-module-summary",  # Show module summary at the top of the documentation
    "imported-members",  # Include imported members from other modules
]
autoapi_python_use_implicit_namespaces = True
autoapi_output_dir = "autoapi"  # Explicitly set output directory for AutoAPI


def autoapi_skip_members(app, what, name, obj, skip, options):
    """Skip members based on their name or type."""

    if what == "attribute":
        if not obj.docstring or obj.docstring == "":
            return True

        # Skip attributes without docstrings
        return True

    return skip


def setup(sphinx):
    sphinx.connect("autoapi-skip-member", autoapi_skip_members)
    ...


# ==== HTML OUTPUT CONFIGURATION =======================================================================================

html_theme = "furo"
html_css_files = [
    "css/custom.css",  # Custom CSS file
]

# ==== CUSTOM LABELS ====================================================================================================

rst_prolog = """
.. role:: summarylabel
"""


if __name__ == "__main__":
    print(f"[AutoAPI] Using autoapi_dirs: {autoapi_dirs}")
    print(mars_module_path)
