"""ASV benchmarks for virtual core resource type YAML loading."""

from __future__ import annotations

from pathlib import Path
from typing import Any

import yaml

from cleveragents.domain.models.core.resource_type import ResourceTypeSpec
from cleveragents.resource.schema import ResourceTypeConfigSchema

_EXAMPLES_DIR = Path(__file__).resolve().parent.parent / "examples" / "resource-types"

_VIRTUAL_CORE_NAMES = ("file", "directory", "commit", "branch", "tag", "tree")
_VIRTUAL_YAML_PATHS = {
    name: _EXAMPLES_DIR / f"{name}.yaml" for name in _VIRTUAL_CORE_NAMES
}


class TimeVirtualCoreSchemaLoad:
    """Benchmark virtual core resource type YAML schema loading."""

    timeout = 60

    def setup(self) -> None:
        self.schema_cls = ResourceTypeConfigSchema
        self.paths = dict(_VIRTUAL_YAML_PATHS)

    def time_load_file_yaml(self) -> None:
        self.schema_cls.from_yaml_file(self.paths["file"])

    def time_load_directory_yaml(self) -> None:
        self.schema_cls.from_yaml_file(self.paths["directory"])

    def time_load_all_virtual_core(self) -> None:
        for path in self.paths.values():
            self.schema_cls.from_yaml_file(path)


class TimeVirtualCoreDomainModelLoad:
    """Benchmark virtual core resource type domain model creation."""

    timeout = 60

    def setup(self) -> None:
        self.spec_cls = ResourceTypeSpec
        self.configs: dict[str, dict[str, Any]] = {}
        for name, path in _VIRTUAL_YAML_PATHS.items():
            with open(path, encoding="utf-8") as f:
                self.configs[name] = yaml.safe_load(f)

    def time_file_from_config(self) -> None:
        self.spec_cls.from_config(self.configs["file"])

    def time_commit_from_config(self) -> None:
        self.spec_cls.from_config(self.configs["commit"])

    def time_all_virtual_core_from_config(self) -> None:
        for config in self.configs.values():
            self.spec_cls.from_config(config)


class TimeVirtualCoreCliDict:
    """Benchmark as_cli_dict rendering for virtual core types."""

    timeout = 60

    def setup(self) -> None:
        self.specs: dict[str, ResourceTypeSpec] = {}
        for name, path in _VIRTUAL_YAML_PATHS.items():
            with open(path, encoding="utf-8") as f:
                config: dict[str, Any] = yaml.safe_load(f)
            self.specs[name] = ResourceTypeSpec.from_config(config)

    def time_file_cli_dict(self) -> None:
        self.specs["file"].as_cli_dict()

    def time_all_virtual_core_cli_dict(self) -> None:
        for spec in self.specs.values():
            spec.as_cli_dict()
