"""ASV benchmarks for Resource CLI command parsing overhead.

Measures the import time and basic construction of resource CLI objects
to track startup overhead.
"""

from __future__ import annotations


class ResourceCliImportBench:
    """Benchmark resource CLI module import time."""

    timeout = 30.0

    def setup(self) -> None:
        """Pre-warm Python import caches."""
        pass

    def time_import_resource_module(self) -> None:
        """Measure import time of the resource CLI module."""
        import importlib

        import cleveragents.cli.commands.resource as mod

        importlib.reload(mod)

    def time_import_formatting_module(self) -> None:
        """Measure import time of the formatting module."""
        import importlib

        import cleveragents.cli.formatting as mod

        importlib.reload(mod)


class ResourceDictConversionBench:
    """Benchmark resource dict conversion helpers."""

    timeout = 30.0

    def setup(self) -> None:
        """Set up test data."""
        from datetime import UTC, datetime

        self.mock_resource_data = {
            "resource_id": "01HXYZ1234567890ABCDEFGHIJ",
            "name": "local/bench-resource",
            "resource_type_name": "git-checkout",
            "classification": "physical",
            "description": "Benchmark test resource",
            "location": "/tmp/bench",
            "properties": {"path": "/tmp/bench", "branch": "main"},
            "created_at": datetime.now(tz=UTC),
            "updated_at": datetime.now(tz=UTC),
        }

    def time_resource_dict_construction(self) -> None:
        """Measure dict construction for resource output."""
        data = self.mock_resource_data
        result = {
            "resource_id": data["resource_id"],
            "name": data["name"],
            "type": data["resource_type_name"],
            "classification": str(data["classification"]),
            "description": data["description"],
            "location": data["location"],
            "properties": data["properties"],
            "created_at": data["created_at"].isoformat(),
            "updated_at": data["updated_at"].isoformat(),
        }
        assert result["name"] == "local/bench-resource"


class ResourceTypeListBench:
    """Benchmark resource type list operations."""

    timeout = 60.0

    def setup(self) -> None:
        """Create an in-memory registry with bootstrapped types."""
        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

        engine = create_engine("sqlite:///:memory:", echo=False)
        Base.metadata.create_all(engine)
        factory = sessionmaker(bind=engine, expire_on_commit=False)
        self.service = ResourceRegistryService(session_factory=factory)
        self.service.bootstrap_builtin_types()

    def time_list_types(self) -> None:
        """Measure listing all resource types."""
        types = self.service.list_types()
        assert len(types) >= 2

    def time_show_type(self) -> None:
        """Measure showing a single resource type."""
        spec = self.service.show_type("git-checkout")
        assert spec.name == "git-checkout"
