"""ASV benchmarks for built-in resource type YAML loading."""

from __future__ import annotations

from pathlib import Path

_EXAMPLES_DIR = Path(__file__).resolve().parent.parent / "examples" / "resource-types"
_GIT_CHECKOUT_YAML = _EXAMPLES_DIR / "git-checkout.yaml"
_FS_DIRECTORY_YAML = _EXAMPLES_DIR / "fs-directory.yaml"


class TimeBuiltinSchemaLoad:
    """Benchmark built-in resource type YAML schema loading."""

    def setup(self) -> None:
        from cleveragents.resource.schema import ResourceTypeConfigSchema

        self.schema_cls = ResourceTypeConfigSchema
        self.git_checkout_path = _GIT_CHECKOUT_YAML
        self.fs_directory_path = _FS_DIRECTORY_YAML

    def time_load_git_checkout(self) -> None:
        self.schema_cls.from_yaml_file(self.git_checkout_path)

    def time_load_fs_directory(self) -> None:
        self.schema_cls.from_yaml_file(self.fs_directory_path)

    def time_load_both_builtin_types(self) -> None:
        self.schema_cls.from_yaml_file(self.git_checkout_path)
        self.schema_cls.from_yaml_file(self.fs_directory_path)


class TimeBuiltinDomainModelLoad:
    """Benchmark built-in resource type domain model creation."""

    def setup(self) -> None:
        import yaml

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

        self.spec_cls = ResourceTypeSpec
        with open(_GIT_CHECKOUT_YAML) as f:
            self.git_checkout_config: dict[str, object] = yaml.safe_load(f)
        with open(_FS_DIRECTORY_YAML) as f:
            self.fs_directory_config: dict[str, object] = yaml.safe_load(f)

    def time_git_checkout_from_config(self) -> None:
        self.spec_cls.from_config(self.git_checkout_config)

    def time_fs_directory_from_config(self) -> None:
        self.spec_cls.from_config(self.fs_directory_config)

    def time_both_from_config(self) -> None:
        self.spec_cls.from_config(self.git_checkout_config)
        self.spec_cls.from_config(self.fs_directory_config)


class TimeBuiltinCliDict:
    """Benchmark as_cli_dict rendering for built-in types."""

    def setup(self) -> None:
        import yaml

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

        with open(_GIT_CHECKOUT_YAML) as f:
            git_config: dict[str, object] = yaml.safe_load(f)
        with open(_FS_DIRECTORY_YAML) as f:
            fs_config: dict[str, object] = yaml.safe_load(f)
        self.git_spec = ResourceTypeSpec.from_config(git_config)
        self.fs_spec = ResourceTypeSpec.from_config(fs_config)

    def time_git_checkout_cli_dict(self) -> None:
        self.git_spec.as_cli_dict()

    def time_fs_directory_cli_dict(self) -> None:
        self.fs_spec.as_cli_dict()
