# Copyright NTESS. See COPYRIGHT file for details.
#
# SPDX-License-Identifier: MIT

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

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import glob
import os
import re
import subprocess
import sys

import canary

docs_source_dir = os.path.abspath(".")
tests_dir = os.path.abspath(os.path.join(docs_source_dir, "../../tests"))
assert os.path.exists(tests_dir)

os.environ["CANARY_MAKE_DOCS"] = "1"


# -- Project information -----------------------------------------------------

project = "canary"
copyright = "2024, National Technology & Engineering Solutions of Sandia, LLC (NTESS)"
author = "National Technology & Engineering Solutions of Sandia, LLC (NTESS)"

# The short X.Y version
version = re.sub(r"\+.*$", r"", canary.version)

# The full version, including alpha/beta/rc tags
release = canary.version


# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    "sphinx.ext.todo",
    "sphinx.ext.autodoc",
    "sphinx.ext.mathjax",
    "sphinx.ext.napoleon",
    "sphinx_design",
    "_canary.third_party.programoutput",
    "_canary.third_party.imagesvg",
]
autodoc_member_order = "bysource"
autodoc_default_options = {"ignore-module-all": True}

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]
add_module_names = False
napoleon_numpy_docstring = False

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = "python"

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = [
    "tutorial/intro.how.rst",
    "tutorial/intro.pyt.rst",
    "tutorial/intro.testcase.rst",
    "tutorial/intro.testfile.rst",
    "tutorial/intro.why.rst",
    "tutorial/resource.spec.rst",
    "presentation",
]


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = "pydata_sphinx_theme"
html_logo = "icon/Canary_icon3_color_800.svg"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = []

html_sidebars = {"index": ["search-button-field"], "**": ["search-button-field", "sidebar-nav-bs"]}

html_theme_options = {
    "navigation_depth": 4,
    "show_toc_level": 1,
    "github_url": "https://github.com/sandialabs/canary",
    "navbar_start": ["navbar-logo"],
    "navbar_end": ["theme-switcher", "navbar-icon-links"],
    "logo": {"text": f"canary {version}"},
    "navbar_persistent": [],
    "secondary_sidebar_items": ["page-toc"],
}

# -- Extension configuration -------------------------------------------------

# -- Options for todo extension ----------------------------------------------

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = True

# Set an environment variable so that colify will print output like it would to
# a terminal.
os.environ["COLIFY_SIZE"] = "25x120"
os.environ["COLUMNS"] = "120"

# Generate full package list if needed
user_dir = os.path.join(docs_source_dir, "user")
for section in ("directives", "commands"):
    pat = os.path.join(user_dir, f"{section}.*rst")
    files = glob.glob(pat)
    for file in files:
        os.remove(file)
args = [sys.executable, "-m", "canary", "autodoc", "-d", user_dir]
proc = subprocess.run(args)
assert proc.returncode == 0

if os.getenv("PROGRAM_OUTPUT_RESET_CACHE") is not None:
    print("Resetting program output cache")
    cache_dir = os.path.join(docs_source_dir, ".cache")
    for file in os.listdir(cache_dir):
        os.remove(os.path.join(cache_dir, file))
