"""ASV benchmarks for deferred physical resource type loading (#330)."""

from __future__ import annotations

from pathlib import Path
from typing import Any

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

_GIT_TAXONOMY_TYPES = (
    "git",
    "git-remote",
    "git-branch",
    "git-tag",
    "git-commit",
    "git-tree",
    "git-tree-entry",
    "git-stash",
    "git-submodule",
)

_FS_LINK_TYPES = (
    "fs-symlink",
    "fs-hardlink",
)

_ALL_DEFERRED_PHYSICAL = _GIT_TAXONOMY_TYPES + _FS_LINK_TYPES


class TimeDeferredPhysicalYAMLLoad:
    """Benchmark deferred physical resource type YAML loading."""

    def setup(self) -> None:
        import yaml

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

        self.spec_cls = ResourceTypeSpec
        self.configs: dict[str, dict[str, Any]] = {}
        for name in _ALL_DEFERRED_PHYSICAL:
            path = _EXAMPLES_DIR / f"{name}.yaml"
            with open(path) as f:
                self.configs[name] = yaml.safe_load(f)

    def time_load_git_type(self) -> None:
        self.spec_cls.from_config(self.configs["git"])

    def time_load_git_branch(self) -> None:
        self.spec_cls.from_config(self.configs["git-branch"])

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

    def time_load_git_tree(self) -> None:
        self.spec_cls.from_config(self.configs["git-tree"])

    def time_load_git_tree_entry(self) -> None:
        self.spec_cls.from_config(self.configs["git-tree-entry"])

    def time_load_fs_symlink(self) -> None:
        self.spec_cls.from_config(self.configs["fs-symlink"])

    def time_load_fs_hardlink(self) -> None:
        self.spec_cls.from_config(self.configs["fs-hardlink"])

    def time_load_all_git_taxonomy(self) -> None:
        for name in _GIT_TAXONOMY_TYPES:
            self.spec_cls.from_config(self.configs[name])

    def time_load_all_fs_links(self) -> None:
        for name in _FS_LINK_TYPES:
            self.spec_cls.from_config(self.configs[name])

    def time_load_all_deferred_physical(self) -> None:
        for name in _ALL_DEFERRED_PHYSICAL:
            self.spec_cls.from_config(self.configs[name])


class TimeDeferredPhysicalCliDict:
    """Benchmark as_cli_dict rendering for deferred physical types."""

    def setup(self) -> None:
        import yaml

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

        self.specs: dict[str, Any] = {}
        for name in _ALL_DEFERRED_PHYSICAL:
            path = _EXAMPLES_DIR / f"{name}.yaml"
            with open(path) as f:
                config: dict[str, Any] = yaml.safe_load(f)
            self.specs[name] = ResourceTypeSpec.from_config(config)

    def time_git_cli_dict(self) -> None:
        self.specs["git"].as_cli_dict()

    def time_all_deferred_physical_cli_dict(self) -> None:
        for name in _ALL_DEFERRED_PHYSICAL:
            self.specs[name].as_cli_dict()


class TimeBootstrapBuiltinTypes:
    """Benchmark bootstrap_builtin_types() including deferred physical types."""

    timeout = 120

    def setup(self) -> None:
        from sqlalchemy import create_engine
        from sqlalchemy.orm import sessionmaker

        from cleveragents.infrastructure.database.models import Base

        self.engine = create_engine("sqlite:///:memory:")
        Base.metadata.create_all(self.engine)
        self.factory = sessionmaker(bind=self.engine)

    def time_bootstrap(self) -> None:
        from cleveragents.application.services.resource_registry_service import (
            ResourceRegistryService,
        )

        service = ResourceRegistryService(session_factory=self.factory)
        service.bootstrap_builtin_types()
