# uv_add_packages
# -----------------
# Instala paquetes Python en un proyecto con uv add.
# Inicializa pyproject.toml si no existe.
# Fallback a pip install si uv no esta disponible.
#
# USO (sourced):
#   source uv_add_packages.sh
#   uv_add_packages /path/to/project jupyter jupyterlab pandas

uv_add_packages() {
    local project_dir="$1"
    shift
    local packages=("$@")

    if [ ${#packages[@]} -eq 0 ]; then
        echo "uv_add_packages: se requiere al menos un paquete" >&2
        return 1
    fi

    if [ ! -d "$project_dir/.venv" ]; then
        echo "uv_add_packages: no existe .venv en $project_dir — ejecuta init_uv_venv primero" >&2
        return 1
    fi

    if command -v uv &>/dev/null; then
        # Inicializar pyproject.toml si no existe
        if [ ! -f "$project_dir/pyproject.toml" ]; then
            (cd "$project_dir" && uv init 2>/dev/null) || true
        fi
        (cd "$project_dir" && uv add "${packages[@]}" 2>&1)
    else
        "$project_dir/.venv/bin/pip" install "${packages[@]}" 2>&1
    fi
}
