#!/bin/zsh

# Define an associative array of subgraph names to URLs.
# Change values to the URLs of the subgraphs you want to run locally so the
# composed supergraph will include the schema changes you are working on locally.
typeset -A subgraphs
subgraphs=(
    [abacus]="https://qa-graphql-abacus.theorchard.io/graphql"
    [account]="https://qa-graphql-account.theorchard.io/graphql"
    [analytics]="https://qa-graphql-analytics.theorchard.io/graphql"
    [audience]="https://qa-graphql-audience.theorchard.io/graphql"
    [content-review]="https://qa-graphql-content-review.theorchard.io/graphql"
    [distribution]="https://qa-graphql-distribution.theorchard.io/graphql"
    [collaborator]="https://qa-graphql-collaborator.theorchard.io/graphql"
    [knowledge]="https://qa-graphql-knowledge.theorchard.io/graphql"
    [knowledge-search]="https://qa-graphql-knowledge-search.theorchard.io/graphql"
    [neighbouring-rights]="https://qa-graphql-neighbouring-rights.theorchard.io/graphql"
    [participant]="https://qa-graphql-participant.theorchard.io/graphql"
    [product]="https://qa-graphql-product.theorchard.io/graphql"
    [publishing]="https://qa-graphql-publishing.theorchard.io/graphql"
    [sr-delivery]="https://qa-graphql-sr-delivery.theorchard.io/graphql"
    [tax-payment]="https://qa-graphql-tax-payment.theorchard.io/graphql"
    [user]="https://qa-graphql-user.theorchard.io/graphql"
)

# Define headers as a normal array.
headers=(
    "orchard-user-id: oa:123"
    "orchard-profile-id: some-profile-id"
    "orchard-profile-type: profileType"
    "apollographql-client-name: some-client"
    "orchard-profile-uuid: uuid1234"
    "orchard-identity-id: some-identity-i"
)

# Join headers into a single string with each header prefixed by --header.
header_string=""
for header in "${headers[@]}"; do
    header_string+="--header \"$header\" "
done

# Cleanup any pre-existing files or directories.
[ -f ./supergraph.yaml ] && rm ./supergraph.yaml
# Note: Deleting supergraph.graphql while the router is running may crash the router.
[ -d ./subgraphs ] && rm -rf ./subgraphs

# Create a directory to store the subgraph schemas.
mkdir -p ./subgraphs

# Initialize the supergraph.yaml file content.
yaml_content="federation_version: =2.0.3\nsubgraphs:\n"

# Loop through the associative array to create URLs and file names.
for subgraph in ${(k)subgraphs}; do
    introspection_url=${subgraphs[$subgraph]}
    file="graphql-${subgraph}.gql"

    echo "Introspecting $subgraph from $introspection_url"
    # Build the command as an array.
    cmd=(rover subgraph introspect "$introspection_url")
    for header in "${headers[@]}"; do
        cmd+=(--header "$header")
    done

    # Execute the command and redirect output to the appropriate file.
    "${cmd[@]}" > "./subgraphs/$file"
    if [ $? -ne 0 ]; then
        echo "Failed to introspect $subgraph from $introspection_url"
        exit 1
    fi

    # Add subgraph URL and headers to the YAML content.
    yaml_content+="  graphql-${subgraph}:\n"
    yaml_content+="    schema:\n"
    yaml_content+="      subgraph_url: ${introspection_url}\n"
    yaml_content+="      introspection_headers:\n"

    # List out headers in the YAML.
    for header in "${headers[@]}"; do
        # Split the header into key-value pairs.
        key=$(echo "$header" | cut -d':' -f 1)
        value=$(echo "$header" | cut -d':' -f 2-)
        yaml_content+="        ${key}:${value}\n"
    done
done

# Write the YAML content to the supergraph.yaml file.
echo "Writing yaml config"
echo -e "$yaml_content" > ./scripts/supergraph.yaml

# Compose the supergraph.
echo "Composing supergraph"
eval "yes | rover supergraph compose --config ./scripts/supergraph.yaml > ./supergraph.graphql"

if [ $? -ne 0 ]; then
    echo "Failed to compose supergraph"
fi
