from __future__ import annotations

from flask import Blueprint

from backends.explorer.connections.db_handler_registry import get_handler_for_snapshot
from backends.explorer.queries.explorer_data import (
    get_snapshot_metadata,
    get_snapshots_metadata_from_folders,
)
from backends.explorer.utils.webapp_config import webapp_config
from backends.utils import return_ok

graph_snapshot = Blueprint("graph_snapshot", __name__, url_prefix="/snapshots")


@graph_snapshot.route("", methods=["GET"])
def get_snapshots():
    folders = webapp_config.db_explorer_folders
    snapshots = get_snapshots_metadata_from_folders(folders)
    return return_ok(data={"snapshots": snapshots})


@graph_snapshot.route("<snapshot_id>", methods=["GET"])
def get_snapshot(snapshot_id: str):
    folders = webapp_config.db_explorer_folders

    db_handler = get_handler_for_snapshot(snapshot_id, folders)
    content = get_snapshot_metadata(snapshot_id, folders, db_handler)
    return return_ok(data=content)
