"""ASV benchmarks for ProjectContextPolicy validation overhead.

Measures the performance of:
- ContextView construction (Pydantic validation)
- ProjectContextPolicy construction
- resolve_view() for each phase
- JSON serialization round-trip
"""

from __future__ import annotations

import sys
from pathlib import Path

try:
    from cleveragents.domain.models.core.context_policy import (
        ContextView,
        ProjectContextPolicy,
    )
except ModuleNotFoundError:
    sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
    from cleveragents.domain.models.core.context_policy import (
        ContextView,
        ProjectContextPolicy,
    )


def _make_view() -> ContextView:
    """Create a fully-populated ContextView."""
    return ContextView(
        include_resources=["db-*", "cache-*"],
        exclude_resources=["db-test"],
        include_paths=["src/**/*.py", "lib/**"],
        exclude_paths=["*.pyc", "__pycache__/**"],
        max_file_size=1048576,
        max_total_size=10485760,
    )


def _make_policy() -> ProjectContextPolicy:
    """Create a fully-populated ProjectContextPolicy."""
    return ProjectContextPolicy(
        default_view=ContextView(
            include_resources=["db-*"],
            exclude_resources=["db-test"],
            include_paths=["src/**"],
            exclude_paths=["*.pyc"],
            max_file_size=1048576,
            max_total_size=10485760,
        ),
        strategize_view=ContextView(
            include_resources=["db-*", "cache-*"],
        ),
        execute_view=ContextView(
            include_paths=["src/**", "lib/**"],
        ),
        apply_view=ContextView(
            exclude_paths=["tests/**"],
        ),
    )


class ContextViewValidationSuite:
    """Benchmark ContextView model construction."""

    def time_view_construction(self) -> None:
        """Benchmark creating a fully-populated ContextView."""
        _make_view()

    def time_view_minimal_construction(self) -> None:
        """Benchmark creating a minimal ContextView."""
        ContextView()

    def time_view_with_size_limits(self) -> None:
        """Benchmark ContextView with size limit validation."""
        ContextView(
            max_file_size=1048576,
            max_total_size=10485760,
        )


class PolicyValidationSuite:
    """Benchmark ProjectContextPolicy model construction."""

    def time_policy_construction(self) -> None:
        """Benchmark creating a fully-populated policy."""
        _make_policy()

    def time_policy_empty_construction(self) -> None:
        """Benchmark creating an empty policy."""
        ProjectContextPolicy()

    def time_policy_default_only(self) -> None:
        """Benchmark policy with only default view."""
        ProjectContextPolicy(
            default_view=_make_view(),
        )


class PolicyResolveSuite:
    """Benchmark resolve_view() for each phase."""

    def setup(self) -> None:
        """Create policy for resolve benchmarks."""
        self.policy = _make_policy()

    def time_resolve_default(self) -> None:
        """Benchmark resolve_view('default')."""
        self.policy.resolve_view("default")

    def time_resolve_strategize(self) -> None:
        """Benchmark resolve_view('strategize')."""
        self.policy.resolve_view("strategize")

    def time_resolve_execute(self) -> None:
        """Benchmark resolve_view('execute')."""
        self.policy.resolve_view("execute")

    def time_resolve_apply(self) -> None:
        """Benchmark resolve_view('apply')."""
        self.policy.resolve_view("apply")


class PolicySerializationSuite:
    """Benchmark policy serialization."""

    def setup(self) -> None:
        """Create policy for serialization benchmarks."""
        self.policy = _make_policy()

    def time_model_dump(self) -> None:
        """Benchmark model_dump() dict serialization."""
        self.policy.model_dump()

    def time_model_dump_json(self) -> None:
        """Benchmark model_dump_json() JSON serialization."""
        self.policy.model_dump_json()

    def time_json_roundtrip(self) -> None:
        """Benchmark JSON serialize + deserialize."""
        json_str = self.policy.model_dump_json()
        ProjectContextPolicy.model_validate_json(json_str)
