#!/usr/bin/env python
#
# Copyright (c) 2025 Authors and contributors
# (see the AUTHORS.rst file for the full list of names)
#
# Released under the GNU Public Licence, v3 or any higher version
# SPDX-License-Identifier: GPL-3.0-or-later

"""Configuration file for the Sphinx documentation builder.

This file does only contain a selection of the most common options. For a full list see
the documentation: http://www.sphinx-doc.org/en/master/config
"""

from datetime import datetime
from pathlib import Path

# -- Path setup --------------------------------------------------------------
import tomli

import maicos

ROOT = Path("../../")

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

with Path(ROOT / "pyproject.toml").open(mode="rb") as fp:
    project_dict = tomli.load(fp)["project"]

project = project_dict["name"]
author = maicos.__authors__
version = maicos.__version__
copyright = f"{datetime.now().date().year}, {author}"

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

# Add any Sphinx extension module names here, as strings.
extensions = [
    "sphinx.builders.linkcheck",  # Check that external links are working
    "sphinx.ext.autodoc",  # import the modules you are documenting
    "sphinx_gallery.gen_gallery",  # Generate examples from python files
    "sphinx.ext.viewcode",  # link to source files from docs
    "sphinx.ext.intersphinx",  # generate links to in external projects
    "sphinx.ext.mathjax",  # Render math via JavaScript
    "sphinx.ext.napoleon",  # Support for NumPy and Google style docstrings
    "sphinxcontrib.bibtex",  # Cite references using a bibtex file
    "sphinx_toggleprompt",  # button to examples to toggle prompts
]

# The path to the bibtex file
bibtex_bibfiles = ["../static/refs.bib"]
bibtex_default_style = "unsrt"
bibtex_reference_style = "author_year"

sphinx_gallery_conf = {
    "filename_pattern": r"/*\.py",
    "copyfile_regex": r".*\.(tpr|trr|xtc)",
    "default_thumb_file": (ROOT / "docs/static/logo.svg").absolute(),
    "example_extensions": {".py", ".sh"},
    "examples_dirs": ["../../examples"],
    "gallery_dirs": ["examples"],
    "min_reported_time": 60,
    "reference_url": {"maicos": None},
    "remove_config_comments": True,
    "prefer_full_module": ["maicos"],
}

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

# The suffix(es) of source filenames. You can specify multiple suffix as a list of
# string: note: do not add .ipynb when nbspinx is enabled, otherwise you get the
# "missing title" error
source_suffix = [".rst"]

# The master toctree document.
master_doc = "index"

# 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 = "en"

# 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: list[str] = []

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = "tango"

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

html_theme = "furo"
html_title = "MAICoS"
html_favicon = "../static/logo.ico"
html_static_path = ["../static"]

html_theme_options = {
    "navigation_with_keys": True,
    "footer_icons": [
        {
            "name": "GitHub",
            "url": project_dict["urls"]["repository"],
            "html": "",
            "class": "fa-brands fa-github fa-2x",
        },
    ],
    "top_of_page_buttons": [],
    "light_logo": "logo_MAICOS_light.svg",
    "dark_logo": "logo_MAICOS_dark.svg",
    "sidebar_hide_name": True,
}

# font-awesome logos
html_css_files = [
    "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/fontawesome.min.css",  # noqa: E501
    "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/solid.min.css",  # noqa: E501
    "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/brands.min.css",  # noqa: E501
]

# -- Options for HTMLHelp output ---------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = "MAICoSdoc"


# -- Options for LaTeX output ------------------------------------------------

latex_elements: dict[str, str] = {
    # The paper size ('letterpaper' or 'a4paper').
    #
    # 'papersize': 'letterpaper',
    # The font size ('10pt', '11pt' or '12pt').
    #
    # 'pointsize': '10pt',
    # Additional stuff for the LaTeX preamble.
    #
    # 'preamble': '',
    # Latex figure (float) alignment
    #
    # 'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
    (
        master_doc,
        "MAICoS.tex",
        "MAICoS Documentation",
        "see the file AUTHORS for the full list of names",
        "manual",
    ),
]


# -- Options for manual page output ------------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [(master_doc, "maicos", "MAICoS Documentation", [author], 1)]


# -- Options for Texinfo output ----------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
    (
        master_doc,
        "MAICoS",
        "MAICoS Documentation",
        author,
        "MAICoS",
        "One line description of project.",
        "Miscellaneous",
    ),
]


# -- Options for Epub output -------------------------------------------------

# Bibliographic Dublin Core info.
epub_title = project

# The unique identifier of the text. This can be a ISBN number
# or the project homepage.
#
# epub_identifier = ''

# A unique identification for the text.
#
# epub_uid = ''

# A list of files that should not be packed into the epub file.
epub_exclude_files = ["search.html"]


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

# Configuration for intersphinx: refer to the Python standard library
# and other packages used by MAICoS
intersphinx_mapping = {
    "scipy": ("https://docs.scipy.org/doc/scipy/", None),
    "matplotlib": ("https://matplotlib.org/stable/", None),
    "MDAnalysis": ("https://docs.mdanalysis.org/stable/", None),
    "numpy": ("https://numpy.org/doc/stable/", None),
    "python": ("https://docs.python.org/3/", None),
}
