#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset

# Ensure canonical mysql environment variables are set.
export MYSQL_HOST="${MYSQL_HOST:=materialize-mysql-mysql-1.flow-test}"
export MYSQL_PORT="${MYSQL_PORT:=3306}"
export MYSQL_DATABASE="${MYSQL_DATABASE:=flow}"
export MYSQL_PASSWORD="${MYSQL_PASSWORD:=flow}"
export MYSQL_USER="${MYSQL_USER:=flow}"

docker compose -f materialize-mysql/docker-compose.yaml up --wait

function query() {
  echo "$1" | docker exec -i \
    materialize-mysql-mysql-1 mysqlsh --host=127.0.0.1 --port=$MYSQL_PORT --user=root --password=$MYSQL_PASSWORD --database $MYSQL_DATABASE --sql --json=raw
}

query "GRANT ALL PRIVILEGES ON *.* TO 'flow'@'%' WITH GRANT OPTION"
query "SET @@GLOBAL.time_zone='UTC'"
query "SET GLOBAL innodb_large_prefix = ON;"
query "SET GLOBAL innodb_file_format = Barracuda;"

config_json_template='{
   "address":  "$MYSQL_HOST:$MYSQL_PORT",
   "database": "$MYSQL_DATABASE",
   "password": "$MYSQL_PASSWORD",
   "user":     "$MYSQL_USER",
   "hardDelete": true,
   "advanced": {
    "no_flow_document": true
   }
}'

resources_json_template='[
  {
    "resource": {
      "table": "Simple"
    },
    "source": "${TEST_COLLECTION_SIMPLE}"
  },
  {
    "resource": {
      "table": "duplicate_keys_standard"
    },
    "source": "${TEST_COLLECTION_DUPLICATED_KEYS}"
  },
  {
    "resource": {
      "table": "duplicate_keys_delta",
      "delta_updates": true
    },
    "source": "${TEST_COLLECTION_DUPLICATED_KEYS}"
  },
  {
    "resource": {
      "table": "duplicate_keys_delta_exclude_flow_doc",
      "delta_updates": true
    },
    "source": "${TEST_COLLECTION_DUPLICATED_KEYS}",
    "fields": {
      "recommended": true,
      "exclude": [
        "flow_document" 
      ]
    }
  },
  {
    "resource": {
      "table": "Multiple Types"
    },
    "source": "${TEST_COLLECTION_MULTIPLE_DATATYPES}",
    "fields": {
      "recommended": true,
      "exclude": ["nested/id"],
      "include": {
        "nested": {},
        "array_int": {}
      }
    }
  },
  {
    "resource": {
      "table": "Formatted Strings"
    },
    "source": "${TEST_COLLECTION_FORMATTED_STRINGS}",
    "fields": {
      "recommended": true
    }
  },
  {
    "resource": {
      "table": "long-string"
    },
    "source": "${TEST_COLLECTION_LONG_STRING}"
  },
  {
    "resource": {
      "table": "unsigned_bigint"
    },
    "source": "${TEST_COLLECTION_UNSIGNED_BIGINT}"
  },
  {
    "resource": {
      "table": "deletions"
    },
    "source": "${TEST_COLLECTION_DELETIONS}"
  },
  {
    "resource": {
      "table": "all_key_types_part_one"
    },
    "source": "${TEST_COLLECTION_ALL_KEY_TYPES_PART_ONE}"
  },
  {
    "resource": {
      "table": "all_key_types_part_two"
    },
    "source": "${TEST_COLLECTION_ALL_KEY_TYPES_PART_TWO}"
  },
  {
    "resource": {
      "table": "all_key_types_part_three"
    },
    "source": "${TEST_COLLECTION_ALL_KEY_TYPES_PART_THREE}"
  },
  {
    "resource": {
      "table": "fields_with_projections"
    },
    "source": "${TEST_COLLECTION_FIELDS_WITH_PROJECTIONS}",
    "fields": {
      "recommended": true,
      "exclude": ["original_field"],
      "include": {
        "another_field": {},
        "projected_another": {}
      }
    }
  },
  {
    "resource": {
      "table": "many_columns"
    },
    "source": "${TEST_COLLECTION_MANY_COLUMNS}",
    "fields": {
      "recommended": true
    }
  }
]'

export CONNECTOR_CONFIG="$(echo "$config_json_template" | envsubst | jq -c)"
export RESOURCES_CONFIG="$(echo "$resources_json_template" | envsubst | jq -c)"
