from __future__ import annotations

from dataclasses import dataclass, field


@dataclass(slots=True)
class MetricsCollector:
    """Collect lightweight runtime metrics for a HistoryManager instance."""

    metrics: dict[str, float | int] = field(
        default_factory=lambda: {
            "session_created": 0,
            "operations_recorded": 0,
            "sessions_undone": 0,
            "total_undo_time": 0.0,
        }
    )

    def record_session_created(self) -> None:
        self.metrics["session_created"] = int(self.metrics["session_created"]) + 1

    def record_operation(self) -> None:
        self.metrics["operations_recorded"] = int(self.metrics["operations_recorded"]) + 1

    def record_undo(self, duration: float) -> None:
        self.metrics["sessions_undone"] = int(self.metrics["sessions_undone"]) + 1
        self.metrics["total_undo_time"] = float(self.metrics["total_undo_time"]) + duration

    def get_stats(self) -> dict[str, float | int]:
        return dict(self.metrics)
