#!/bin/bash
# qdrant-api.sh - Qdrant API wrapper using vault cache
# Usage: source this file or call its functions

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/vault-env.sh"

QDRANT_URL=$(vault_qdrant_url)
QDRANT_API_KEY=$(vault_qdrant_api_key)

if [ -z "$QDRANT_API_KEY" ]; then
    echo "Error: Could not retrieve Qdrant API key from vault cache"
    return 1 2>/dev/null || exit 1
fi

# Make Qdrant API calls
qdrant_api() {
    local method="${1:-GET}"
    local endpoint="$2"
    local payload="${3:-}"
    
    local headers=(-H "api-key: ${QDRANT_API_KEY}" -H "Content-Type: application/json")
    
    if [ "$method" = "GET" ]; then
        curl -sk "${headers[@]}" "${QDRANT_URL}${endpoint}"
    else
        curl -sk -X "$method" "${headers[@]}" -d "$payload" "${QDRANT_URL}${endpoint}"
    fi
}

# Common operations
qdrant_list_collections() {
    qdrant_api GET "/collections"
}

qdrant_collection_info() {
    local collection="${1:-}"
    [ -z "$collection" ] && { echo "Usage: qdrant_collection_info <collection_name>"; return 1; }
    qdrant_api GET "/collections/${collection}"
}

# Export
export -f qdrant_api qdrant_list_collections qdrant_collection_info
export QDRANT_URL QDRANT_API_KEY
