### Run Typesense via Docker ########################################
set -x

export TYPESENSE_API_KEY=xyz
export TYPESENSE_HOST=http://localhost:8108

docker stop typesense-repro 2>/dev/null
docker rm typesense-repro 2>/dev/null
rm -rf "$(pwd)"/typesense-data-dir-repro
mkdir "$(pwd)"/typesense-data-dir-repro

# Wait for Typesense to be ready
docker run -d -p 8108:8108 --name typesense-repro \
            -v"$(pwd)"/typesense-data-dir-repro:/data \
            typesense/typesense:29.0.rc31 \
            --data-dir /data \
            --api-key=$TYPESENSE_API_KEY \
            --enable-cors

# Wait till typesense is ready.
until curl -s -o /dev/null -w "%{http_code}" "$TYPESENSE_HOST/health" -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" | grep -q "200"; do
  sleep 2
done

curl -s "$TYPESENSE_HOST/debug" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" | jq


curl -s "$TYPESENSE_HOST/collections" \
       -X POST \
       -H "Content-Type: application/json" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
       -d '
          {
    "default_sorting_field": "item_priority",
    "enable_nested_fields": false,
    "fields": [
        {
            "facet": false,
            "index": true,
            "infix": true,
            "locale": "",
            "name": "code",
            "optional": true,
            "sort": false,
            "stem": false,
            "stem_dictionary": "",
            "store": true,
            "type": "string"
        },
        {
            "facet": false,
            "index": true,
            "infix": false,
            "locale": "",
            "name": "family_name",
            "optional": true,
            "sort": false,
            "stem": false,
            "stem_dictionary": "",
            "store": true,
            "type": "string"
        },
        {
            "facet": true,
            "index": true,
            "infix": false,
            "locale": "",
            "name": "url_without_anchor",
            "optional": false,
            "sort": false,
            "stem": false,
            "stem_dictionary": "",
            "store": true,
            "type": "string"
        },
        {
            "facet": true,
            "index": true,
            "infix": false,
            "locale": "",
            "name": "language",
            "optional": true,
            "sort": false,
            "stem": false,
            "stem_dictionary": "",
            "store": true,
            "type": "string"
        },
        {
          "facet": false,
          "index": true,
          "infix": false,
          "locale": "",
          "name": "item_priority",
          "optional": false,
          "sort": true,
          "stem": false,
          "stem_dictionary": "",
          "store": true,
          "type": "int64"
        },
        {
          "facet": true,
          "index": true,
          "infix": false,
          "locale": "",
          "name": "template",
          "optional": true,
          "sort": false,
          "stem": false,
          "stem_dictionary": "",
          "store": true,
          "type": "string"
        }
    ],
    "name": "hbk_content",
    "symbols_to_index": [
        ":",
        "/",
        "-",
        "."
    ],
    "token_separators": []
}' | jq

curl -s "$TYPESENSE_HOST/collections/hbk_content/documents/import?action=create" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -H "Content-Type: text/plain" \
        -X POST \
        -d '{"code":"e01-24-56","family_name":"IMU-3DM-CV5","url_without_anchor":"https://www.hbkworld.com/en/products/transducers/inertial-sensors/inertial-measurement-units--imu-/3dm-cv5-imu","language":"en","template":"product-page", "item_priority": 10}
            {"code":"34523-23423","family_name":"Inertial Sensors","url_without_anchor":"https://www.hbkworld.com/en/products/transducers/inertial-sensors/inertial-measurement-units--imu-/3dm-cv5-imu/p-34523-23423","language":"en","template":"productoption-page", "item_priority": 10}
            {"code":"e01-02-14","family_name":"C10","url_without_anchor":"https://www.hbkworld.com/en/products/transducers/force/c10","language":"en","template":"product-page", "item_priority": 10}
            {"code":"1-C10/100KN","family_name":"Compression Force Transducer C10/100kN","url_without_anchor":"https://www.hbkworld.com/en/products/transducers/force/c10/p-1-C10/100KN","language":"en","template":"productoption-page", "item_priority": 10}' | jq


### Expect it to return document with url_without_anchor = https://www.hbkworld.com/en/products/transducers/inertial-sensors/inertial-measurement-units--imu-/3dm-cv5-imu
### It also return document with url_without_anchor = https://www.hbkworld.com/en/products/transducers/inertial-sensors/inertial-measurement-units--imu-/3dm-cv5-imu/p-34523-23423
curl -s "$TYPESENSE_HOST/collections/hbk_content/documents/search?q=*&filter_by=url_without_anchor:=\`https://www.hbkworld.com/en/products/transducers/inertial-sensors/inertial-measurement-units--imu-/3dm-cv5-imu\`" \
        -X GET \
        -H "Content-Type: application/json" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
          | jq

### Working as expected,  returns only document with url_without_anchor = https://www.hbkworld.com/en/products/transducers/force/c10
curl -s "$TYPESENSE_HOST/collections/hbk_content/documents/search?q=*&filter_by=url_without_anchor:=\`https://www.hbkworld.com/en/products/transducers/force/c10\`" \
        -X GET \
        -H "Content-Type: application/json" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
          | jq

docker stop typesense-repro
docker rm typesense-repro

### Documentation ######################################################################################
# Visit the API reference section: https://typesense.org/docs/28.0/api/collections.html
# Click on the "Shell" tab under each API resource's docs, to get shell commands for other API endpoints