#!/bin/bash

# Function to show help
show_help() {
    echo "Usage: $0 [OPTIONS]"
    echo ""
    echo "Set environment variables for Canton contracts configuration."
    echo ""
    echo "OPTIONS:"
    echo "  --show-details    Show detailed output with all environment variables"
    echo "  --help           Show this help message"
    echo ""
    echo "Examples:"
    echo "  $0                 # Set configuration with minimal output"
    echo "  $0 --show-details # Set configuration with detailed output"
    exit 0
}

# Check for command line arguments
SHOW_DETAILS=false
for arg in "$@"; do
    case $arg in
        --show-details)
            SHOW_DETAILS=true
            ;;
        --help)
            show_help
            ;;
        *)
            echo "Unknown option: $arg"
            echo "Use --help for usage information."
            exit 1
            ;;
    esac
done

source ./.env

echo "🔁 Setting up configuration..."

TOKEN=$(cd ../../scripts-config && ./get_token.sh)

# Export canton token, only show summary
export CANTON_TOKEN="$TOKEN"

# Pull Oracle specific template IDs
export REQUEST_FACTORY_TEMPLATE_ID="${PACKAGE_ID}:PrepaidOracle.RequestFactory:RequestFactory"
export REQUEST_TEMPLATE_ID="${PACKAGE_ID}:PrepaidOracle.Request:Request"
export FILLED_REQUEST_TEMPLATE_ID="${PACKAGE_ID}:PrepaidOracle.Request:FilledRequest"
export AMULET_RULES_TEMPLATE_ID="${SPLICE_PACKAGE_ID}:Splice.AmuletRules:AmuletRules"
export OPEN_MINING_ROUND_TEMPLATE_ID="${SPLICE_PACKAGE_ID}:Splice.Round:OpenMiningRound"

if [[ "$SHOW_DETAILS" == true ]]; then
    echo "  ✅ CANTON_TOKEN set:                    ${CANTON_TOKEN:0:10}...${CANTON_TOKEN: -10}"
    echo "  ✅ REQUEST_FACTORY_TEMPLATE_ID:         $REQUEST_FACTORY_TEMPLATE_ID"
    echo "  ✅ REQUEST_TEMPLATE_ID:                 $REQUEST_TEMPLATE_ID"
    echo "  ✅ AMULET_RULES_TEMPLATE_ID:            $AMULET_RULES_TEMPLATE_ID"
    echo "  ✅ OPEN_MINING_ROUND_TEMPLATE_ID:       $OPEN_MINING_ROUND_TEMPLATE_ID"
fi

echo "✅ Configuration has been set"