"""ASV benchmarks for definition-of-done evaluation throughput.

Measures the performance of:
- DoD text parsing into criteria
- Template rendering with argument substitution
- TextMatchEvaluator evaluation
"""

from __future__ import annotations

import importlib
import sys
from pathlib import Path

# Ensure the local *source* tree is importable.
_SRC = str(Path(__file__).resolve().parents[1] / "src")
if _SRC not in sys.path:
    sys.path.insert(0, _SRC)

import cleveragents  # noqa: E402

importlib.reload(cleveragents)

from cleveragents.domain.models.core.definition_of_done import (  # noqa: E402
    TextMatchEvaluator,
    parse_dod_criteria,
    render_dod_template,
)

# ---------------------------------------------------------------------------
# Benchmark: parse_dod_criteria
# ---------------------------------------------------------------------------

_SIMPLE_DOD = "All tests pass\nNo lint errors\nCoverage above 90%"
_BULLET_DOD = "- All tests pass\n- No lint errors\n- Coverage above 90%\n- No warnings"
_NUMBERED_DOD = (
    "1. Tests pass\n2. Lint clean\n3. Coverage >= 97%\n4. Types check\n5. Docs updated"
)


class ParseDoDCriteria:
    """Benchmark suite for ``parse_dod_criteria``."""

    def time_parse_simple(self) -> None:
        """Parse plain-text DoD criteria."""
        parse_dod_criteria(_SIMPLE_DOD)

    def time_parse_bullets(self) -> None:
        """Parse bullet-point DoD criteria."""
        parse_dod_criteria(_BULLET_DOD)

    def time_parse_numbered(self) -> None:
        """Parse numbered DoD criteria."""
        parse_dod_criteria(_NUMBERED_DOD)

    def time_parse_empty(self) -> None:
        """Parse empty DoD text."""
        parse_dod_criteria("")


# ---------------------------------------------------------------------------
# Benchmark: render_dod_template
# ---------------------------------------------------------------------------

_TEMPLATE = "Tests for {module} in {env} must pass with {threshold}% coverage"
_ARGS = {"module": "auth", "env": "production", "threshold": "95"}


class RenderDoDTemplate:
    """Benchmark suite for ``render_dod_template``."""

    def time_render_with_args(self) -> None:
        """Render template with all placeholders matched."""
        render_dod_template(_TEMPLATE, _ARGS)

    def time_render_partial_args(self) -> None:
        """Render template with only some placeholders matched."""
        render_dod_template(_TEMPLATE, {"module": "auth"})

    def time_render_no_args(self) -> None:
        """Render template with empty arguments dict."""
        render_dod_template(_TEMPLATE, {})


# ---------------------------------------------------------------------------
# Benchmark: TextMatchEvaluator
# ---------------------------------------------------------------------------


class TextMatchEvaluation:
    """Benchmark suite for ``TextMatchEvaluator.evaluate``."""

    def setup(self) -> None:
        """Prepare evaluator and criteria."""
        self.evaluator = TextMatchEvaluator()
        self.criteria = parse_dod_criteria(_NUMBERED_DOD)
        self.context_all_pass = {
            "tests": "tests pass",
            "lint": "lint clean",
            "coverage": "coverage >= 97%",
            "types": "types check",
            "docs": "docs updated",
        }
        self.context_mixed = {
            "tests": "tests pass",
            "lint": "3 lint errors",
        }

    def time_evaluate_all_pass(self) -> None:
        """Evaluate criteria where all pass."""
        self.evaluator.evaluate(self.criteria, self.context_all_pass)

    def time_evaluate_mixed_results(self) -> None:
        """Evaluate criteria with mixed pass/fail."""
        self.evaluator.evaluate(self.criteria, self.context_mixed)

    def time_evaluate_empty_context(self) -> None:
        """Evaluate criteria with empty context (all skip)."""
        self.evaluator.evaluate(self.criteria, {})
