
NODE=https://rjkiyd98hwsn5fubp-1.a1.typesense.net
APIKEY=

curl --location --request DELETE "$NODE/collections/schemachange_test" \
--header "X-TYPESENSE-API-KEY: $APIKEY" \


# Create collection 
curl --location --request POST "$NODE/collections" \
--header "X-TYPESENSE-API-KEY: $APIKEY" \
--header 'Content-Type: application/json' \
--data-raw '{
  "name": "schemachange_test",
  "fields": [
    {
      "name": "id",
      "type": "string"
    },
    {
      "name": "sku",
      "type": "string"
    },
    {
      "name": "price",
      "type": "object"
    },
    {
        "name": "price.default.ch.compareAt",
        "type": "float",
        "facet": true
    },
    {
        "name": "price.default.ch.price",
        "type": "float",
        "facet": true
    },
    {
        "name": "price.default.de.compareAt",
        "type": "float",
        "facet": true
    },
    {
        "name": "price.default.de.price",
        "type": "float",
        "facet": true
    },
    {
        "name": "price.default.at.compareAt",
        "type": "float",
        "facet": true
    },
    {
        "name": "price.default.at.price",
        "type": "float",
        "facet": true
    },
    {
      "name": "price.marketing.ch.compareAt",
      "type": "float",
      "facet": true
    },
    {
        "name": "price.marketing.ch.price",
        "type": "float",
        "facet": true
    },
    {
      "name": "price.marketing.ch.prescriptionCost",
      "type": "float",
      "optional": true
    },
    {
        "name": "price.marketing.de.compareAt",
        "type": "float",
        "facet": true
    },
    {
        "name": "price.marketing.de.price",
        "type": "float",
        "facet": true
    },
    {
        "name": "price.marketing.de.prescriptionCost",
        "type": "float",
        "optional": true
    },
    {
        "name": "price.marketing.at.compareAt",
        "type": "float",
        "facet": true
    },
    {
        "name": "price.marketing.at.price",
        "type": "float",
        "facet": true
    },
    {
      "name": "price.marketing.at.prescriptionCost",
      "type": "float",
      "optional": true
    }
    ],
    "enable_nested_fields": true
}'

echo ""
echo ""


# Add a document to the index
curl --location --request POST "$NODE/collections/schemachange_test/documents" \
--header "X-TYPESENSE-API-KEY: $APIKEY" \
--header 'Content-Type: application/json' \
--data-raw '{
   "id":"sku1",
   "sku":"test",
   "price":{
      "default": {
        "at": {
          "compareAt": 130,
          "price": 130
        },
        "ch": {
          "compareAt": 120,
          "price": 120
        },
        "de": {
          "compareAt": 130,
          "price": 130
        }
      },
      "marketing": {
        "at": {
          "compareAt": 130,
          "price": 130
        },
        "ch": {
          "compareAt": 120,
          "price": 120
        },
        "de": {
          "compareAt": 130,
          "price": null
        }
      }
    }
}'

echo ""
echo ""

#This import fails correctly
curl --location --request POST "$NODE/collections/schemachange_test/documents/import?action=create" \
        -H "X-TYPESENSE-API-KEY: ${APIKEY}" \
        -H "Content-Type: text/plain" \
        -X POST \
        --data-raw '{"id":"sku1", "sku":"test", "price":{ "default": { "at": {"compareAt": 130,"price": 130},"ch": {"compareAt": 120,"price": 120},"de": {"compareAt": 130,"price": 130}},"marketing": {"at": {"compareAt": 130,"price": 130},"ch": {"compareAt": 120,"price": 120},"de": {"compareAt": 130,"price": null}}}}'

#This import throws an error message, but works - which invalidates the data in the schema
curl --location --request POST "$NODE/collections/schemachange_test/documents/import?action=emplace" \
        -H "X-TYPESENSE-API-KEY: ${APIKEY}" \
        -H "Content-Type: text/plain" \
        -X POST \
        --data-raw '{"id":"sku1", "sku":"test", "price":{ "default": { "at": {"compareAt": 130,"price": 130},"ch": {"compareAt": 120,"price": 120},"de": {"compareAt": 130,"price": 130}},"marketing": {"at": {"compareAt": 130,"price": 130},"ch": {"compareAt": 120,"price": 120},"de": {"compareAt": 130,"price": null}}}}'

echo ""
echo ""

# Add field --> works
curl --location --request PATCH "$NODE/collections/schemachange_test" \
--header "X-TYPESENSE-API-KEY: $APIKEY" \
--header 'Content-Type: text/plain' \
--data-raw '{
         "fields": [
           {"name": "test", "type": "string", "facet": true,"optional":true }   
         ]
       }'

