import time

from app.modules.shared.checkpointer import get_checkpointer


def bootstrap_database(rag_repository, chat_repository, agent_repository, story_context_repository) -> None:
    last_error: Exception | None = None
    for attempt in range(1, 16):
        try:
            rag_repository.ensure_tables()
            chat_repository.ensure_tables()
            agent_repository.ensure_tables()
            story_context_repository.ensure_tables()
            get_checkpointer()
            return
        except Exception as exc:  # noqa: BLE001
            last_error = exc
            if attempt == 15:
                break
            time.sleep(1)
    assert last_error is not None
    raise last_error
