from pathlib import Path

from unmake import HistoryManager, OperationRegistry, SQLiteStorage


class MoveUndo:
    def __init__(self, payload: dict[str, str]):
        self.src = payload["src"]
        self.dst = payload["dst"]

    def undo(self) -> None:
        Path(self.dst).rename(self.src)


manager = HistoryManager(SQLiteStorage("example.db"))

with manager.start_session(script="basic_usage.py") as session:
    Path("a.txt").write_text("hello", encoding="utf-8")
    Path("a.txt").rename("b.txt")
    session.record_operation("fs.move", {"src": "a.txt", "dst": "b.txt"})

registry = OperationRegistry()
registry.register("fs.move", MoveUndo)
manager.undo_session(manager.list_sessions(limit=1)[0].session_id, registry)
