#!/bin/bash

# exit when any of these fails
set -e

echo "Creating a virtual study with autogenerated id (hash)."
create_vs_response=$(curl -s 'http://cbioportal-container:8080/api/session/virtual_study' \
    -H 'Content-Type: application/json' \
    --data-raw '{"name":"Test published virtual study","description":"Test published virtual study","studyViewFilter":{"studyIds":["study_es_0_import_export"],"alterationFilter":{"copyNumberAlterationEventTypes":{"AMP":true,"HOMDEL":true},"mutationEventTypes":{"any":true},"structuralVariants":null,"includeDriver":true,"includeVUS":true,"includeUnknownOncogenicity":true,"includeUnknownTier":true,"includeGermline":true,"includeSomatic":true,"includeUnknownStatus":true,"tiersBooleanMap":{}}},"origin":["study_es_0_import_export"],"studies":[{"id":"study_es_0_import_export","samples":["TCGA-A1-A0SB-01"]}],"dynamic":false}')
hash=$(echo "$create_vs_response" | grep -o '"id":"[^"]*"' | cut -d':' -f2 | tr -d '"')
echo "Virtual study created with id: $hash"

echo "Publishing virtual study with hash id: $hash"
publish_status=$(curl -X POST -o /dev/null -s -w "%{http_code}" "http://cbioportal-container:8080/api/public_virtual_studies/$hash" \
    -H 'X-PUBLISHER-API-KEY: TEST')
if [ "$publish_status" -eq 200 ]; then
    echo "Virtual study with id $hash published successfully."
else
    echo "Failed with status $publish_status"
    exit 1
fi

echo "Unpublishing virtual study with hash id: $hash"
unpublish_status=$(curl -X DELETE -o /dev/null -s -w "%{http_code}" "http://cbioportal-container:8080/api/public_virtual_studies/$hash" \
    -H 'X-PUBLISHER-API-KEY: TEST')
if [ "$unpublish_status" -eq 200 ]; then
    echo "Virtual study with id $hash unpublished successfully."
else
    echo "Failed with status $unpublish_status"
    exit 1
fi

vs_custom_id="vs_custom_id"
echo "Creating a virtual study with custom id: $vs_custom_id"
create_custom_id_vs_status=$(curl -X POST -o /dev/null -s -w "%{http_code}" "http://cbioportal-container:8080/api/public_virtual_studies/$vs_custom_id" \
    -H 'X-PUBLISHER-API-KEY: TEST' \
    -H 'Content-Type: application/json' \
    --data-raw '{"name":"Test published virtual study","description":"Test published virtual study","studyViewFilter":{"studyIds":["study_es_0_import_export"],"alterationFilter":{"copyNumberAlterationEventTypes":{"AMP":true,"HOMDEL":true},"mutationEventTypes":{"any":true},"structuralVariants":null,"includeDriver":true,"includeVUS":true,"includeUnknownOncogenicity":true,"includeUnknownTier":true,"includeGermline":true,"includeSomatic":true,"includeUnknownStatus":true,"tiersBooleanMap":{}}},"origin":["study_es_0_import_export"],"studies":[{"id":"study_es_0_import_export","samples":["TCGA-A1-A0SB-01"]}],"dynamic":false}')
if [ "$create_custom_id_vs_status" -eq 200 ]; then
    echo "Virtual study with id $vs_custom_id published successfully."
else
    echo "Failed with status $create_custom_id_vs_status"
    exit 1
fi

echo "Drop virtual study (softDelete=false) with id: $vs_custom_id"
drop_vs_status=$(curl -X DELETE -o /dev/null -s -w "%{http_code}" "http://cbioportal-container:8080/api/public_virtual_studies/$vs_custom_id?softDelete=false" \
    -H 'X-PUBLISHER-API-KEY: TEST')
if [ "$drop_vs_status" -eq 200 ]; then
    echo "Virtual study with id $hash dropped successfully."
else
    echo "Failed with status $drop_vs_status"
    exit 1
fi
exit 0