"""ASV benchmarks for AutomationProfileService resolution latency.

Measures the performance of:
- Profile resolution with various precedence levels
- Profile listing
- Built-in profile lookup via the service
"""

from __future__ import annotations

import os
import sys
from pathlib import Path

try:
    from cleveragents.application.services.automation_profile_service import (
        AutomationProfileService,
    )
except ModuleNotFoundError:
    sys.path.insert(
        0,
        str(Path(__file__).resolve().parents[1] / "src"),
    )
    from cleveragents.application.services.automation_profile_service import (
        AutomationProfileService,
    )


class ProfileResolutionSuite:
    """Benchmark profile resolution precedence."""

    def setup(self) -> None:
        """Create a service instance for benchmarks."""
        os.environ.pop("CLEVERAGENTS_AUTOMATION_PROFILE", None)
        self.svc = AutomationProfileService(repo=None, global_default="manual")

    def time_resolve_plan_level(self) -> None:
        """Benchmark plan-level resolution."""
        self.svc.resolve_profile(plan_profile="full-auto")

    def time_resolve_action_level(self) -> None:
        """Benchmark action-level resolution."""
        self.svc.resolve_profile(action_profile="auto")

    def time_resolve_project_level(self) -> None:
        """Benchmark project-level resolution."""
        self.svc.resolve_profile(project_profile="ci")

    def time_resolve_global_fallback(self) -> None:
        """Benchmark global fallback resolution."""
        self.svc.resolve_profile()

    def time_resolve_full_precedence(self) -> None:
        """Benchmark with all levels set."""
        self.svc.resolve_profile(
            plan_profile="full-auto",
            action_profile="auto",
            project_profile="supervised",
        )


class ProfileListingSuite:
    """Benchmark profile listing."""

    def setup(self) -> None:
        """Create service instance."""
        self.svc = AutomationProfileService(repo=None)

    def time_list_all_profiles(self) -> None:
        """Benchmark listing all profiles."""
        self.svc.list_profiles()

    def time_get_builtin_profile(self) -> None:
        """Benchmark getting a built-in profile."""
        self.svc.get_profile("cautious")
