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

import os
import sys
from datetime import datetime

# -- Path setup --------------------------------------------------------------
# Add the project root to the Python path so autodoc can find the modules
sys.path.insert(0, os.path.abspath('..'))

# -- Project information -----------------------------------------------------
project = 'ENAHOPY'
copyright = f'{datetime.now().year}, Paul Camacho'
author = 'Paul Camacho'
release = '0.1.2'
version = '0.1.2'

# -- General configuration ---------------------------------------------------
extensions = [
    'sphinx.ext.autodoc',        # Auto-generate documentation from docstrings
    'sphinx.ext.napoleon',       # Support for Google and NumPy style docstrings
    'sphinx.ext.viewcode',       # Add links to highlighted source code
    'sphinx.ext.intersphinx',    # Link to other project's documentation
    'sphinx.ext.autosummary',    # Generate autodoc summaries
    'sphinx.ext.doctest',        # Test code snippets in documentation
    'sphinx.ext.coverage',       # Check documentation coverage
    'sphinx.ext.githubpages',    # Create .nojekyll file for GitHub Pages
]

# Napoleon settings for Google-style docstrings
napoleon_google_docstring = True
napoleon_numpy_docstring = False
napoleon_include_init_with_doc = True
napoleon_include_private_with_doc = False
napoleon_include_special_with_doc = True
napoleon_use_admonition_for_examples = True
napoleon_use_admonition_for_notes = True
napoleon_use_admonition_for_references = True
napoleon_use_ivar = False
napoleon_use_param = True
napoleon_use_rtype = True
napoleon_preprocess_types = True
napoleon_type_aliases = None
napoleon_attr_annotations = True

# AutoDoc settings
autodoc_default_options = {
    'members': True,
    'member-order': 'bysource',
    'special-members': '__init__',
    'undoc-members': True,
    'exclude-members': '__weakref__'
}
autodoc_typehints = 'description'
autodoc_typehints_description_target = 'documented'
autodoc_typehints_format = 'short'

# Autosummary settings
autosummary_generate = True
autosummary_imported_members = False

# Doctest settings
doctest_global_setup = '''
import pandas as pd
import numpy as np
from pathlib import Path
'''

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

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']

# The suffix(es) of source filenames.
source_suffix = {
    '.rst': 'restructuredtext',
}

# The master toctree document.
master_doc = 'index'

# The language for content autogenerated by Sphinx
language = 'es'

# -- Options for HTML output -------------------------------------------------
html_theme = 'sphinx_rtd_theme'
html_theme_options = {
    'logo_only': False,
    'display_version': True,
    'prev_next_buttons_location': 'bottom',
    'style_external_links': True,
    'navigation_depth': 4,
    'collapse_navigation': False,
    'sticky_navigation': True,
    'includehidden': True,
    'titles_only': False
}

# Add any paths that contain custom static files (such as style sheets)
html_static_path = ['_static']
html_css_files = ['custom.css']

# Custom sidebar templates
html_sidebars = {
    '**': [
        'globaltoc.html',
        'relations.html',
        'sourcelink.html',
        'searchbox.html',
    ]
}

# -- Options for HTMLHelp output ---------------------------------------------
htmlhelp_basename = 'ENAHOPYdoc'

# -- Options for LaTeX output ------------------------------------------------
latex_elements = {
    'papersize': 'letterpaper',
    'pointsize': '10pt',
}

latex_documents = [
    (master_doc, 'ENAHOPY.tex', 'ENAHOPY Documentation',
     'Paul Camacho', 'manual'),
]

# -- Options for manual page output ------------------------------------------
man_pages = [
    (master_doc, 'enahopy', 'ENAHOPY Documentation',
     [author], 1)
]

# -- Options for Texinfo output ----------------------------------------------
texinfo_documents = [
    (master_doc, 'ENAHOPY', 'ENAHOPY Documentation',
     author, 'ENAHOPY', 'Python library for processing INEI ENAHO microdata.',
     'Miscellaneous'),
]

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

# Intersphinx mapping
intersphinx_mapping = {
    'python': ('https://docs.python.org/3', None),
    'pandas': ('https://pandas.pydata.org/docs/', None),
    'numpy': ('https://numpy.org/doc/stable/', None),
}

# -- Custom CSS setup --------------------------------------------------------
def setup(app):
    """Create _static directory if it doesn't exist."""
    static_dir = os.path.join(os.path.dirname(__file__), '_static')
    if not os.path.exists(static_dir):
        os.makedirs(static_dir)

    # Create custom.css file
    custom_css_path = os.path.join(static_dir, 'custom.css')
    if not os.path.exists(custom_css_path):
        with open(custom_css_path, 'w') as f:
            f.write('''
/* Custom CSS for ENAHOPY documentation */
.wy-side-nav-search {
    background-color: #2c3e50;
}

.rst-content code.literal {
    color: #e74c3c;
}

.rst-content .admonition-title {
    background-color: #3498db;
}
''')
