"""ASV benchmarks for exception handling overhead.

Measures the cost of error classification, redaction,
and wrapping operations.
"""

from __future__ import annotations

from cleveragents.core.error_handling import (
    classify_error,
    format_error_for_cli,
    redact_error_details,
    redact_value,
    wrap_unexpected,
)
from cleveragents.core.exceptions import (
    DatabaseError,
    ValidationError,
)


class ClassifyBench:
    """Benchmark error classification throughput."""

    timeout = 60

    def setup(self) -> None:
        self.validation_err = ValidationError("field invalid")
        self.db_err = DatabaseError("connection lost")
        self.bare_err = Exception("unknown crash")

    def time_classify_validation(self) -> None:
        """Benchmark classifying a ValidationError."""
        classify_error(self.validation_err)

    def time_classify_database(self) -> None:
        """Benchmark classifying a DatabaseError."""
        classify_error(self.db_err)

    def time_classify_bare(self) -> None:
        """Benchmark classifying a bare Exception."""
        classify_error(self.bare_err)

    def time_format_cli(self) -> None:
        """Benchmark formatting an error for CLI."""
        info = classify_error(self.validation_err)
        format_error_for_cli(info)


class RedactBench:
    """Benchmark secret redaction throughput."""

    timeout = 60

    def setup(self) -> None:
        self.details_secret = {"api_key": "sk-abc123", "plan_id": "01PLAN"}
        self.details_clean = {"plan_id": "01PLAN", "status": "ok"}
        self.value_with_secret = "token sk-abcdefghijklmnopqrstuvwxyz end"

    def time_redact_with_secret(self) -> None:
        """Benchmark redacting details with a secret key."""
        redact_error_details(self.details_secret)

    def time_redact_clean(self) -> None:
        """Benchmark redacting details without secrets."""
        redact_error_details(self.details_clean)

    def time_redact_value(self) -> None:
        """Benchmark redacting a string value."""
        redact_value(self.value_with_secret)


class WrapBench:
    """Benchmark exception wrapping throughput."""

    timeout = 60

    def setup(self) -> None:
        self.bare_err = Exception("crash")

    def time_wrap_bare(self) -> None:
        """Benchmark wrapping a bare Exception."""
        wrap_unexpected(self.bare_err)

    def time_wrap_with_context(self) -> None:
        """Benchmark wrapping with context."""
        wrap_unexpected(self.bare_err, context={"plan_id": "01PLAN"})
