"""CRUD y operaciones sobre collections de Metabase."""

from .client import MetabaseClient


def metabase_move_collection(
    client: MetabaseClient,
    collection_id: int,
    parent_id: int | None,
) -> dict:
    """Mueve una collection (sub-arbol completo) a otro padre.

    Endpoint: PUT /api/collection/:id con {parent_id: ...}.
    parent_id=None mueve la collection a la raiz ("Our analytics").

    Metabase reubica la collection y todo su sub-arbol (colecciones hijas,
    cards, dashboards, documents) atomicamente.

    Args:
        client: Cliente autenticado.
        collection_id: ID de la collection a mover.
        parent_id: ID de la collection padre destino. None = raiz.

    Returns:
        Collection actualizada con el nuevo parent_id y location.

    Example:
        >>> col = metabase_move_collection(client, 12, parent_id=3)
        >>> print(col["location"])  # "/3/"

        >>> # Mover a raiz:
        >>> col = metabase_move_collection(client, 12, parent_id=None)
        >>> print(col["location"])  # "/"
    """
    return client.request(
        "PUT",
        f"/api/collection/{collection_id}",
        json={"parent_id": parent_id},
    )
