"""ASV benchmarks for docs build baseline.

Measures the performance of parsing, validating, and extracting
information from documentation files to establish baselines for
the docs build pipeline.
"""

from __future__ import annotations

from pathlib import Path

DOCS_DIR = Path("docs")
PLAYBOOK_PATH = DOCS_DIR / "development" / "review_playbook.md"


class DocsParseSuite:
    """Benchmark parsing documentation markdown files."""

    timeout = 30

    def setup(self) -> None:
        """Read the raw markdown text once for reuse."""
        if PLAYBOOK_PATH.exists():
            self.playbook_text: str = PLAYBOOK_PATH.read_text(encoding="utf-8")
        else:
            self.playbook_text = ""

    def time_read_playbook(self) -> None:
        """Benchmark reading the review playbook file."""
        if PLAYBOOK_PATH.exists():
            PLAYBOOK_PATH.read_text(encoding="utf-8")

    def time_count_sections(self) -> None:
        """Benchmark counting markdown sections in the playbook."""
        if self.playbook_text:
            lines: list[str] = self.playbook_text.splitlines()
            _sections: list[str] = [line for line in lines if line.startswith("#")]

    def time_extract_tables(self) -> None:
        """Benchmark extracting markdown table rows from the playbook."""
        if self.playbook_text:
            lines: list[str] = self.playbook_text.splitlines()
            _table_rows: list[str] = [line for line in lines if line.startswith("|")]


class DocsDiscoverySuite:
    """Benchmark discovering documentation files."""

    timeout = 30

    def setup(self) -> None:
        """Prepare docs directory path."""
        self.docs_dir: Path = DOCS_DIR

    def time_list_all_markdown_files(self) -> None:
        """Benchmark listing all markdown files in the docs directory."""
        if self.docs_dir.exists():
            _files: list[Path] = list(self.docs_dir.rglob("*.md"))

    def time_validate_file_existence(self) -> None:
        """Benchmark validating that key docs files exist."""
        expected_files: list[Path] = [
            DOCS_DIR / "development" / "review_playbook.md",
            DOCS_DIR / "development" / "quality-automation.md",
            DOCS_DIR / "development" / "testing.md",
            DOCS_DIR / "development" / "ci-cd.md",
        ]
        for file_path in expected_files:
            file_path.exists()


class DocsContentValidationSuite:
    """Benchmark content validation operations on docs."""

    timeout = 30

    def setup(self) -> None:
        """Read all markdown files for validation benchmarking."""
        self.docs_dir: Path = DOCS_DIR
        self.all_docs: list[str] = []
        if self.docs_dir.exists():
            for md_file in self.docs_dir.rglob("*.md"):
                self.all_docs.append(md_file.read_text(encoding="utf-8"))

    def time_check_broken_links_pattern(self) -> None:
        """Benchmark scanning docs for potential broken link patterns."""
        for doc_content in self.all_docs:
            lines: list[str] = doc_content.splitlines()
            _links: list[str] = [
                line for line in lines if "]()" in line or "]: " in line
            ]

    def time_count_total_lines(self) -> None:
        """Benchmark counting total lines across all docs."""
        total: int = 0
        for doc_content in self.all_docs:
            total += len(doc_content.splitlines())

    def time_search_nox_references(self) -> None:
        """Benchmark searching for nox session references across all docs."""
        for doc_content in self.all_docs:
            _found: bool = "nox -s" in doc_content
