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"))
registry = OperationRegistry()
registry.register("fs.move", MoveUndo)

with manager.start_session(script="batch_processing.py") as session:
    for index in range(3):
        src = Path(f"file_{index}.txt")
        dst = Path(f"file_{index}.bak")
        src.write_text(f"file {index}", encoding="utf-8")
        src.rename(dst)
        session.record_operation("fs.move", {"src": str(src), "dst": str(dst)})

session_id = manager.list_sessions(limit=1)[0].session_id
manager.undo_session(session_id, registry)
