#!/usr/bin/env bash
set -euo pipefail

QUERY="${1:?Usage: coral_delete_matching <query>}"
LIMIT=1
API_URL="${CORAL_API_URL:-https://search-api.coralbricks.ai}"

if [ -z "${CORAL_API_KEY:-}" ]; then
  echo '{"error": "CORAL_API_KEY is not set. Get one at https://coralbricks.ai"}' >&2
  exit 1
fi

# Memory API — delete by query (no ids exposed)
curl -s -X POST "${API_URL}/api/v1/memories/delete_matching" \
  -H "Authorization: Bearer ${CORAL_API_KEY}" \
  -H "Content-Type: application/json" \
  -d "{\"query\": $(printf '%s' "$QUERY" | python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))'), \"limit\": ${LIMIT}}"
