"""ASV benchmarks for ResourceRegistryService operations."""

from __future__ import annotations

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from cleveragents.application.services.resource_registry_service import (
    ResourceRegistryService,
)
from cleveragents.infrastructure.database.models import Base


class BootstrapBenchmark:
    """Benchmark built-in type bootstrap."""

    timeout = 30.0

    def setup(self) -> None:
        self.engine = create_engine("sqlite:///:memory:")
        Base.metadata.create_all(self.engine)
        self.factory = sessionmaker(bind=self.engine)

    def time_bootstrap_builtin_types(self) -> None:
        svc = ResourceRegistryService(session_factory=self.factory)
        svc.bootstrap_builtin_types()

    def time_bootstrap_idempotent(self) -> None:
        svc = ResourceRegistryService(session_factory=self.factory)
        svc.bootstrap_builtin_types()
        svc.bootstrap_builtin_types()


class ListTypesBenchmark:
    """Benchmark type listing operations."""

    timeout = 30.0

    def setup(self) -> None:
        self.engine = create_engine("sqlite:///:memory:")
        Base.metadata.create_all(self.engine)
        self.factory = sessionmaker(bind=self.engine)
        svc = ResourceRegistryService(session_factory=self.factory)
        svc.bootstrap_builtin_types()
        self.svc = svc

    def time_list_all_types(self) -> None:
        self.svc.list_types()

    def time_show_type(self) -> None:
        self.svc.show_type("git-checkout")


class RegisterResourceBenchmark:
    """Benchmark resource registration."""

    timeout = 30.0

    def setup(self) -> None:
        self.engine = create_engine("sqlite:///:memory:")
        Base.metadata.create_all(self.engine)
        self.factory = sessionmaker(bind=self.engine)
        svc = ResourceRegistryService(session_factory=self.factory)
        svc.bootstrap_builtin_types()
        self.svc = svc
        self.counter = 0

    def time_register_resource(self) -> None:
        self.counter += 1
        self.svc.register_resource(
            type_name="git-checkout",
            name=f"local/bench-repo-{self.counter}",
            location="/tmp/bench",
        )
