"""ASV benchmarks for filesystem resource type YAML loading."""

from __future__ import annotations

from pathlib import Path

_EXAMPLES_DIR = Path(__file__).resolve().parent.parent / "examples" / "resource-types"
_FS_MOUNT_YAML = _EXAMPLES_DIR / "fs-mount.yaml"
_FS_FILE_YAML = _EXAMPLES_DIR / "fs-file.yaml"
_FS_DIRECTORY_YAML = _EXAMPLES_DIR / "fs-directory.yaml"


class TimeFsSchemaLoad:
    """Benchmark filesystem resource type YAML schema loading."""

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

        self.schema_cls = ResourceTypeConfigSchema
        self.fs_mount_path = _FS_MOUNT_YAML
        self.fs_file_path = _FS_FILE_YAML
        self.fs_directory_path = _FS_DIRECTORY_YAML

    def time_load_fs_mount(self) -> None:
        self.schema_cls.from_yaml_file(self.fs_mount_path)

    def time_load_fs_file(self) -> None:
        self.schema_cls.from_yaml_file(self.fs_file_path)

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

    def time_load_all_fs_types(self) -> None:
        self.schema_cls.from_yaml_file(self.fs_mount_path)
        self.schema_cls.from_yaml_file(self.fs_file_path)
        self.schema_cls.from_yaml_file(self.fs_directory_path)


class TimeFsDomainModelLoad:
    """Benchmark filesystem 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(_FS_MOUNT_YAML) as f:
            self.fs_mount_config: dict[str, object] = yaml.safe_load(f)
        with open(_FS_FILE_YAML) as f:
            self.fs_file_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_fs_mount_from_config(self) -> None:
        self.spec_cls.from_config(self.fs_mount_config)

    def time_fs_file_from_config(self) -> None:
        self.spec_cls.from_config(self.fs_file_config)

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

    def time_all_fs_from_config(self) -> None:
        self.spec_cls.from_config(self.fs_mount_config)
        self.spec_cls.from_config(self.fs_file_config)
        self.spec_cls.from_config(self.fs_directory_config)


class TimeFsCliDict:
    """Benchmark as_cli_dict rendering for filesystem types."""

    def setup(self) -> None:
        import yaml

        from cleveragents.domain.models.core.resource_type import (
            ResourceTypeSpec,
        )

        with open(_FS_MOUNT_YAML) as f:
            mount_config: dict[str, object] = yaml.safe_load(f)
        with open(_FS_FILE_YAML) as f:
            file_config: dict[str, object] = yaml.safe_load(f)
        with open(_FS_DIRECTORY_YAML) as f:
            dir_config: dict[str, object] = yaml.safe_load(f)
        self.mount_spec = ResourceTypeSpec.from_config(mount_config)
        self.file_spec = ResourceTypeSpec.from_config(file_config)
        self.dir_spec = ResourceTypeSpec.from_config(dir_config)

    def time_fs_mount_cli_dict(self) -> None:
        self.mount_spec.as_cli_dict()

    def time_fs_file_cli_dict(self) -> None:
        self.file_spec.as_cli_dict()

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