"""Build the documentation from a static parse of ``src/evomo/``.

AutoAPI reads the source tree instead of importing it, so a docs build needs no
``evomo``, ``evox``, ``evoxc``, torch, GPU or optional-extra install: the same
environment that renders the guide pages can also render the API reference.
"""

from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
project = "EvoMO"
author = "EvoMO contributors"
copyright = "2026, EvoMO contributors"
# Read the package version without importing it; Python 3.10 has no tomllib.
release = next(
    line.split('"')[1]
    for line in (ROOT / "pyproject.toml").read_text(encoding="utf-8").splitlines()
    if line.startswith("version = ")
)
version = release
language = "en"

extensions = ["myst_parser", "autoapi.extension", "sphinx.ext.mathjax", "sphinx.ext.napoleon"]
source_suffix = {".md": "markdown", ".rst": "restructuredtext"}
root_doc = "index"
templates_path = ["_templates"]
# CONTEXT.md files are agent routing metadata, not documentation pages.
exclude_patterns = ["_build", "README.md", "CONTEXT.md", "**/CONTEXT.md", "papers", "Thumbs.db", ".DS_Store"]
myst_enable_extensions = ["colon_fence", "dollarmath"]
myst_heading_anchors = 3

# AutoAPI generates the per-package and per-module pages under ``docs/autoapi/``.
autoapi_dirs = [str(ROOT / "src" / "evomo")]
autoapi_python_use_implicit_namespaces = True
autoapi_options = ["members", "undoc-members", "show-inheritance", "show-module-summary", "imported-members"]
autoapi_python_class_content = "both"
autoapi_member_order = "bysource"
autoapi_add_toctree_entry = False
autoapi_keep_files = False

html_theme = "shibuya"
html_title = f"EvoMO {release} documentation"
html_static_path = ["_static", "images"]
html_css_files = ["branding.css", "tables.css"]
html_theme_options = {
    "github_url": "https://github.com/EMI-Group/evomo",
    "light_logo": "_static/evox_brand_dark.svg",
    "dark_logo": "_static/evox_brand_light.svg",
}

# The one accepted warning is library-side, not page-side:
# - autoapi.python_import_resolution: pages are generated from source text, never imported.
suppress_warnings = ["autoapi.python_import_resolution"]


def skip_redundant_members(app, what, name, obj, skip, options):
    """Hide empty algorithm attributes and aliases that collide with modules."""
    # Constructor fields already explain these names. Empty instance-attribute
    # entries repeat them and expose internal state without a useful contract.
    # Keep attributes with their own documentation visible.
    if what == "attribute" and name.startswith("evomo.algorithms.") and not obj.docstring.strip():
        return True
    if what == "function" and name in {"evomo.metrics.gd", "evomo.metrics.hv", "evomo.metrics.igd"}:
        return True
    # The optional-import fallback assigns None to the package's module name.
    if what == "data" and name == "evomo.problems.neuroevolution":
        return True
    return None


def setup(app):
    app.connect("autoapi-skip-member", skip_redundant_members)