"""ASV benchmarks for deferred virtual resource type loading (#331).

Benchmarks cover YAML schema loading, domain model creation via
``ResourceTypeSpec.from_config``, and ``as_cli_dict`` rendering for the
three deferred virtual types: remote, submodule, symlink.
"""

from __future__ import annotations

from pathlib import Path
from typing import Any

_EXAMPLES_DIR = Path(__file__).resolve().parent.parent / "examples" / "resource-types"
_REMOTE_YAML = _EXAMPLES_DIR / "remote.yaml"
_SUBMODULE_YAML = _EXAMPLES_DIR / "submodule.yaml"
_SYMLINK_YAML = _EXAMPLES_DIR / "symlink.yaml"


class TimeDeferredVirtualSchemaLoad:
    """Benchmark deferred virtual resource type YAML schema loading."""

    def setup(self) -> None:
        """Set up schema class and file paths."""
        from cleveragents.resource.schema import ResourceTypeConfigSchema

        self.schema_cls = ResourceTypeConfigSchema
        self.remote_path = _REMOTE_YAML
        self.submodule_path = _SUBMODULE_YAML
        self.symlink_path = _SYMLINK_YAML

    def time_load_remote_schema(self) -> None:
        """Benchmark loading remote.yaml via schema loader."""
        self.schema_cls.from_yaml_file(self.remote_path)

    def time_load_submodule_schema(self) -> None:
        """Benchmark loading submodule.yaml via schema loader."""
        self.schema_cls.from_yaml_file(self.submodule_path)

    def time_load_symlink_schema(self) -> None:
        """Benchmark loading symlink.yaml via schema loader."""
        self.schema_cls.from_yaml_file(self.symlink_path)

    def time_load_all_deferred_virtual_schemas(self) -> None:
        """Benchmark loading all three deferred virtual type schemas."""
        self.schema_cls.from_yaml_file(self.remote_path)
        self.schema_cls.from_yaml_file(self.submodule_path)
        self.schema_cls.from_yaml_file(self.symlink_path)


class TimeDeferredVirtualDomainModelLoad:
    """Benchmark deferred virtual resource type domain model creation."""

    def setup(self) -> None:
        """Set up ResourceTypeSpec and parsed configs."""
        import yaml

        from cleveragents.domain.models.core.resource_type import ResourceTypeSpec

        self.spec_cls = ResourceTypeSpec
        with open(_REMOTE_YAML) as f:
            self.remote_config: dict[str, Any] = yaml.safe_load(f)
        with open(_SUBMODULE_YAML) as f:
            self.submodule_config: dict[str, Any] = yaml.safe_load(f)
        with open(_SYMLINK_YAML) as f:
            self.symlink_config: dict[str, Any] = yaml.safe_load(f)

    def time_remote_from_config(self) -> None:
        """Benchmark creating remote ResourceTypeSpec from config."""
        self.spec_cls.from_config(self.remote_config)

    def time_submodule_from_config(self) -> None:
        """Benchmark creating submodule ResourceTypeSpec from config."""
        self.spec_cls.from_config(self.submodule_config)

    def time_symlink_from_config(self) -> None:
        """Benchmark creating symlink ResourceTypeSpec from config."""
        self.spec_cls.from_config(self.symlink_config)

    def time_all_deferred_virtual_from_config(self) -> None:
        """Benchmark creating all three deferred virtual specs."""
        self.spec_cls.from_config(self.remote_config)
        self.spec_cls.from_config(self.submodule_config)
        self.spec_cls.from_config(self.symlink_config)


class TimeDeferredVirtualCliDict:
    """Benchmark as_cli_dict rendering for deferred virtual types."""

    def setup(self) -> None:
        """Set up pre-built ResourceTypeSpec instances."""
        import yaml

        from cleveragents.domain.models.core.resource_type import ResourceTypeSpec

        with open(_REMOTE_YAML) as f:
            remote_cfg: dict[str, Any] = yaml.safe_load(f)
        with open(_SUBMODULE_YAML) as f:
            submodule_cfg: dict[str, Any] = yaml.safe_load(f)
        with open(_SYMLINK_YAML) as f:
            symlink_cfg: dict[str, Any] = yaml.safe_load(f)

        self.remote_spec = ResourceTypeSpec.from_config(remote_cfg)
        self.submodule_spec = ResourceTypeSpec.from_config(submodule_cfg)
        self.symlink_spec = ResourceTypeSpec.from_config(symlink_cfg)

    def time_remote_cli_dict(self) -> None:
        """Benchmark remote as_cli_dict rendering."""
        self.remote_spec.as_cli_dict()

    def time_submodule_cli_dict(self) -> None:
        """Benchmark submodule as_cli_dict rendering."""
        self.submodule_spec.as_cli_dict()

    def time_symlink_cli_dict(self) -> None:
        """Benchmark symlink as_cli_dict rendering."""
        self.symlink_spec.as_cli_dict()
