from __future__ import annotations

from flask import Blueprint, Response

from backends.explorer.discovery.info_registry import ConnectionInfoRegistry
from backends.explorer.queries.explorer_data import (
    get_snapshot_metadata,
    get_snapshots_metadata_from_folders,
)
from backends.explorer.utils.connections import get_webapp_connection_registry, with_db_handler
from backends.utils import return_ok
from graph.connections.db_handler import DbHandler

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


@graph_snapshot.route("", methods=["GET"])
def get_snapshots() -> Response:
    """
    Retrieves metadata for all available graph snapshots.

    Returns:
        dict: OK response with a list of snapshot metadata objects.
    """
    conn_info_registry = get_webapp_connection_registry()
    snapshots = get_snapshots_metadata_from_folders(conn_info_registry)
    return return_ok(data={"snapshots": snapshots})


@graph_snapshot.route("<snapshot_id>", methods=["GET"])
@with_db_handler
def get_snapshot(db_handler: DbHandler, conn_info_registry: ConnectionInfoRegistry, snapshot_id: str) -> Response:
    """
    Retrieves metadata for a specific graph snapshot.

    Args:
        db_handler (DbHandler): Injected database handler.
        conn_info_registry (ConnectionInfoRegistry): Injected connection registry.
        snapshot_id (str): The unique identifier of the snapshot.

    Returns:
        dict: OK response with the snapshot metadata.
    """
    content = get_snapshot_metadata(snapshot_id, db_handler, conn_info_registry)
    return return_ok(data=content)
