#!/usr/bin/env bash
set -euo pipefail
# Creates synthetic demo data. Use a disposable database.
# Configure OC_BASE_URL, OC_TENANT and OC_TOKEN first.
: "${OC_BASE_URL:?Set your database HTTPS origin}"
: "${OC_TENANT:?Set your tenant}"
: "${OC_TOKEN:?Set your database token}"

# Register a schema
curl --fail-with-body --include --request POST "${OC_BASE_URL}/v1/tenants/${OC_TENANT}/schemas" \
  --header "Authorization: Bearer ${OC_TOKEN}" \
  --header "Content-Type: text/plain; charset=utf-8" \
  --data-binary 'namespace = "demo"
table = "people"
primary_key = ["id"]

[[columns]]
name = "id"
ty = "str"
required = true

[[columns]]
name = "follows_id"
ty = "str"

[[columns]]
name = "score"
ty = "i64"

[[relations]]
name = "follows"
from_col = "follows_id"
bidirectional = true

[relations.target]
namespace = "demo"
table = "people"
pk = "id"
'

# Write a batch
curl --fail-with-body --include --request POST "${OC_BASE_URL}/v1/tenants/${OC_TENANT}/rows/demo.people/_batch" \
  --header "Authorization: Bearer ${OC_TOKEN}" \
  --header "Content-Type: application/json" \
  --data-binary '[
  {
    "id": "u1",
    "follows_id": "u2",
    "score": 10
  },
  {
    "id": "u2",
    "follows_id": "u3",
    "score": 20
  },
  {
    "id": "u3",
    "follows_id": "u1",
    "score": 30
  }
]'

# Execute SQL
curl --fail-with-body --include --request POST "${OC_BASE_URL}/v1/tenants/${OC_TENANT}/sql" \
  --header "Authorization: Bearer ${OC_TOKEN}" \
  --header "Content-Type: application/json" \
  --data-binary '{
  "sql": "SELECT id, score FROM demo.people WHERE score >= $1 ORDER BY id LIMIT 10",
  "params": [
    10
  ]
}'

# Breadth-first traversal
curl --fail-with-body --include --request GET "${OC_BASE_URL}/v1/tenants/${OC_TENANT}/graph/demo.people/bfs?rel=follows&pk=u1&max_depth=2" \
  --header "Authorization: Bearer ${OC_TOKEN}"

# Index a document
curl --fail-with-body --include --request POST "${OC_BASE_URL}/v1/tenants/${OC_TENANT}/fts/demo_docs/body" \
  --header "Authorization: Bearer ${OC_TOKEN}" \
  --header "Content-Type: application/json" \
  --data-binary '{
  "doc_id": "p1",
  "text": "Trail mug with insulated steel"
}'

# Search a text field
curl --fail-with-body --include --request GET "${OC_BASE_URL}/v1/tenants/${OC_TENANT}/fts/demo_docs/body?q=trail&mode=bm25&k=5" \
  --header "Authorization: Bearer ${OC_TOKEN}"

# Store a vector
curl --fail-with-body --include --request POST "${OC_BASE_URL}/v1/tenants/${OC_TENANT}/vector/demo_vectors/put" \
  --header "Authorization: Bearer ${OC_TOKEN}" \
  --header "Content-Type: application/json" \
  --data-binary '{
  "id": "v1",
  "embedding": [
    0.1,
    0.2,
    0.3
  ],
  "dim": 3,
  "metric": "cosine",
  "metadata": {
    "category": "outdoor"
  }
}'

# Find nearest vectors
curl --fail-with-body --include --request POST "${OC_BASE_URL}/v1/tenants/${OC_TENANT}/vector/demo_vectors/topk" \
  --header "Authorization: Bearer ${OC_TOKEN}" \
  --header "Content-Type: application/json" \
  --data-binary '{
  "query": [
    0.1,
    0.2,
    0.3
  ],
  "dim": 3,
  "metric": "cosine",
  "k": 5
}'
