"""ASV benchmarks for Kubernetes Helm chart YAML parsing.

Structural smoke benchmarks that guard against YAML parsing regressions
in the Helm chart configuration files (Chart.yaml and values.yaml).

These benchmarks intentionally measure PyYAML parsing performance rather
than project-specific application logic because the chart files are
static YAML consumed by Helm — there is no project Python code to
benchmark for this feature.  The benchmarks exist to:

1. Detect accidental chart corruption (malformed YAML).
2. Guard against unexpected parsing slowdowns from chart growth.
3. Satisfy the ASV benchmark requirement for every feature ticket.
"""

from __future__ import annotations

import importlib
import sys
from pathlib import Path
from typing import Any

import yaml

# Ensure the local *source* tree is importable even when ASV has an
# older build of the package installed.
_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)

_K8S_DIR: Path = Path(__file__).resolve().parents[1] / "k8s"


class TimeChartYamlParsing:
    """Benchmark Chart.yaml parsing and validation."""

    timeout: int = 30

    def setup(self) -> None:
        self.chart_path: Path = _K8S_DIR / "Chart.yaml"
        assert self.chart_path.exists(), "Chart.yaml not found"

    def time_parse_chart_yaml(self) -> None:
        """Time parsing Chart.yaml."""
        with open(self.chart_path, encoding="utf-8") as f:
            data: dict[str, Any] = yaml.safe_load(f)
        assert data["apiVersion"] == "v2"
        assert data["name"] == "cleveragents"

    def time_parse_values_yaml(self) -> None:
        """Time parsing values.yaml."""
        values_path: Path = _K8S_DIR / "values.yaml"
        with open(values_path, encoding="utf-8") as f:
            data: dict[str, Any] = yaml.safe_load(f)
        assert "replicaCount" in data
        assert "resources" in data
        assert "ingress" in data
        assert "redis" in data


class TimeValuesValidation:
    """Benchmark values.yaml structure validation."""

    timeout: int = 30

    def setup(self) -> None:
        values_path: Path = _K8S_DIR / "values.yaml"
        with open(values_path, encoding="utf-8") as f:
            self.values: dict[str, Any] = yaml.safe_load(f)

    def time_validate_resource_limits(self) -> None:
        """Time validating resource limits structure."""
        resources: dict[str, Any] = self.values["resources"]
        assert "limits" in resources
        assert "requests" in resources
        assert "cpu" in resources["limits"]
        assert "memory" in resources["limits"]

    def time_validate_ingress_config(self) -> None:
        """Time validating ingress configuration."""
        ingress: dict[str, Any] = self.values["ingress"]
        assert "enabled" in ingress
        assert "tls" in ingress
        assert "hosts" in ingress

    def time_validate_redis_config(self) -> None:
        """Time validating Redis configuration."""
        redis: dict[str, Any] = self.values["redis"]
        assert "enabled" in redis
        assert "auth" in redis
