#  Copyright 2020 Google LLC
#  Copyright 2020 EPAM Systems
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

pip install --upgrade google-api-python-client
pip install dataclasses
pip install jsonschema
pip install google
pip install google-cloud-storage
pip install deepdiff
export ACL='{"viewers": ["foo"],"owners": ["foo"]}'
export LEGAL='{"legaltags": ["foo"], "otherRelevantDataCountries": ["FR", "US", "CA"],"status": "compliant"}'
export WORKFLOW_URL="http://127.0.0.1:5000"
export UPDATE_STATUS_URL="http://127.0.0.1:5000/wf/us"
export STORAGE_URL="http://127.0.0.1:5000/st"
export SEARCH_URL="http://127.0.0.1:5000/sr/qr"
export LOCALHOST="http://127.0.0.1:5000"
export SEARCH_CONN_ID="http://127.0.0.1:5000"
export WORKFLOW_CONN_ID="http://127.0.0.1:5000"
export DATALOAD_CONFIG_PATH="/usr/local/airflow/dags/configs/dataload.ini"
export SA_FILE_PATH="test"

airflow initdb > /dev/null 2>&1

# exclude testing DAGS
sed -i 's/load_examples = True/load_examples = False/'  /usr/local/airflow/airflow.cfg
# turn on all dags
sed -i 's/dags_are_paused_at_creation = True/dags_are_paused_at_creation = False/'  /usr/local/airflow/airflow.cfg

airflow variables -s storage_url $STORAGE_URL
airflow variables -s id_token 	http://127.0.0.1:5000/storage
airflow variables -s acl "$ACL"
airflow variables -s data_partition_id 	odes
airflow variables -s entitlements_module_name entitlements_client
airflow variables -s legal "$LEGAL"
airflow variables -s provider gcp
airflow variables -s record_kind "odes:osdu:file:0.2.0"
airflow variables -s schema_version "0.2.0"
airflow variables -s workflow_url $WORKFLOW_URL
airflow variables -s file_service_url $LOCALHOST
airflow variables -s update_status_url $UPDATE_STATUS_URL
airflow variables -s search_url $SEARCH_URL
airflow variables -s schema_service_url  $LOCALHOST
airflow variables -s dataload_config_path $DATALOAD_CONFIG_PATH
airflow variables -s search_query_ep sr/qr
airflow variables -s access_token test
airflow variables -s "sa-file-osdu" "test"

airflow connections -a --conn_id workflow --conn_uri $WORKFLOW_CONN_ID
airflow connections -a --conn_id google_cloud_storage --conn_uri $WORKFLOW_CONN_ID

mkdir -p /usr/local/airflow/dags/
cp -rf src/* /usr/local/airflow/
cp -r tests/end-to-end-tests/mock-external-apis /mock-server
cp -r tests/end-to-end-tests/mock-data /mock-server/mock-data

cp tests/end-to-end-tests/{test-osdu-ingest-r2-success.sh,test-osdu-ingest-r2-fail.sh,test-default-ingest-{success,fail}.sh} /mock-server/
cp tests/end-to-end-tests/osdu_api_config.yaml /mock-server/
cp tests/*.py /mock-server/

chmod +x /mock-server/{test-osdu-ingest-r2-success.sh,test-osdu-ingest-r2-fail.sh,test-default-ingest-{success,fail}.sh}