#!/usr/bin/env bash
# Update gas estimates in documentation files using real-time gas prices
# This script reads gas prices from get-multichain-gas-prices.sh and updates docs

set -euo pipefail

# Source the gas price fetcher
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Run the gas price fetcher to get latest prices
echo "Fetching real-time gas prices..."
"$SCRIPT_DIR/get-multichain-gas-prices.sh" > /dev/null 2>&1

# Check if JSON file exists
if [ ! -f /tmp/multichain_gas_prices.json ]; then
    echo "Error: Gas prices JSON file not found. Run get-multichain-gas-prices.sh first."
    exit 1
fi

# Read from JSON file using jq (more reliable)
if command -v jq >/dev/null 2>&1; then
    GAS_DATA=$(cat /tmp/multichain_gas_prices.json)
    TIMESTAMP=$(echo "$GAS_DATA" | jq -r '.timestamp')

    ETH_GAS_GWEI=$(echo "$GAS_DATA" | jq -r '.gas_prices.ethereum_mainnet.gwei')
    ETH_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.ethereum_mainnet.cost_eth')
    ETH_USD_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.ethereum_mainnet.cost_usd')

    CRONOS_GAS_GWEI=$(echo "$GAS_DATA" | jq -r '.gas_prices.cronos.gwei')
    CRONOS_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.cronos.cost_cro')
    CRONOS_USD_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.cronos.cost_usd')

    BSC_GAS_GWEI=$(echo "$GAS_DATA" | jq -r '.gas_prices.bsc.gwei')
    BSC_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.bsc.cost_bnb')
    BSC_USD_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.bsc.cost_usd')

    POLYGON_GAS_GWEI=$(echo "$GAS_DATA" | jq -r '.gas_prices.polygon.gwei')
    POLYGON_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.polygon.cost_matic')
    POLYGON_USD_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.polygon.cost_usd')

    GNOSIS_GAS_GWEI=$(echo "$GAS_DATA" | jq -r '.gas_prices.gnosis.gwei')
    GNOSIS_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.gnosis.cost_xdai')
    GNOSIS_USD_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.gnosis.cost_usd')

    TOTAL_USD=$(echo "$GAS_DATA" | jq -r '.total_usd')
else
    # Fallback to grep if jq not available
    GAS_DATA=$(cat /tmp/multichain_gas_prices.json)
    TIMESTAMP=$(echo "$GAS_DATA" | grep -o '"timestamp":"[^"]*"' | cut -d'"' -f4)

    ETH_GAS_GWEI=$(echo "$GAS_DATA" | jq -r '.gas_prices.ethereum_mainnet.gwei' 2>/dev/null || echo "$GAS_DATA" | grep -A 5 '"ethereum_mainnet"' | grep '"gwei"' | head -1 | cut -d'"' -f4)
    ETH_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.ethereum_mainnet.cost_eth' 2>/dev/null || echo "$GAS_DATA" | grep -A 5 '"ethereum_mainnet"' | grep '"cost_eth"' | head -1 | cut -d'"' -f4)
    ETH_USD_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.ethereum_mainnet.cost_usd' 2>/dev/null || echo "$GAS_DATA" | grep -A 5 '"ethereum_mainnet"' | grep '"cost_usd"' | head -1 | cut -d'"' -f4)

    CRONOS_GAS_GWEI=$(echo "$GAS_DATA" | jq -r '.gas_prices.cronos.gwei' 2>/dev/null || echo "$GAS_DATA" | grep -A 5 '"cronos"' | grep '"gwei"' | head -1 | cut -d'"' -f4)
    CRONOS_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.cronos.cost_cro' 2>/dev/null || echo "$GAS_DATA" | grep -A 5 '"cronos"' | grep '"cost_cro"' | head -1 | cut -d'"' -f4)
    CRONOS_USD_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.cronos.cost_usd' 2>/dev/null || echo "$GAS_DATA" | grep -A 5 '"cronos"' | grep '"cost_usd"' | head -1 | cut -d'"' -f4)

    BSC_GAS_GWEI=$(echo "$GAS_DATA" | jq -r '.gas_prices.bsc.gwei' 2>/dev/null || echo "$GAS_DATA" | grep -A 5 '"bsc"' | grep '"gwei"' | head -1 | cut -d'"' -f4)
    BSC_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.bsc.cost_bnb' 2>/dev/null || echo "$GAS_DATA" | grep -A 5 '"bsc"' | grep '"cost_bnb"' | head -1 | cut -d'"' -f4)
    BSC_USD_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.bsc.cost_usd' 2>/dev/null || echo "$GAS_DATA" | grep -A 5 '"bsc"' | grep '"cost_usd"' | head -1 | cut -d'"' -f4)

    POLYGON_GAS_GWEI=$(echo "$GAS_DATA" | jq -r '.gas_prices.polygon.gwei' 2>/dev/null || echo "$GAS_DATA" | grep -A 5 '"polygon"' | grep '"gwei"' | head -1 | cut -d'"' -f4)
    POLYGON_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.polygon.cost_matic' 2>/dev/null || echo "$GAS_DATA" | grep -A 5 '"polygon"' | grep '"cost_matic"' | head -1 | cut -d'"' -f4)
    POLYGON_USD_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.polygon.cost_usd' 2>/dev/null || echo "$GAS_DATA" | grep -A 5 '"polygon"' | grep '"cost_usd"' | head -1 | cut -d'"' -f4)

    GNOSIS_GAS_GWEI=$(echo "$GAS_DATA" | jq -r '.gas_prices.gnosis.gwei' 2>/dev/null || echo "$GAS_DATA" | grep -A 5 '"gnosis"' | grep '"gwei"' | head -1 | cut -d'"' -f4)
    GNOSIS_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.gnosis.cost_xdai' 2>/dev/null || echo "$GAS_DATA" | grep -A 5 '"gnosis"' | grep '"cost_xdai"' | head -1 | cut -d'"' -f4)
    GNOSIS_USD_COST=$(echo "$GAS_DATA" | jq -r '.gas_prices.gnosis.cost_usd' 2>/dev/null || echo "$GAS_DATA" | grep -A 5 '"gnosis"' | grep '"cost_usd"' | head -1 | cut -d'"' -f4)

    TOTAL_USD=$(echo "$GAS_DATA" | jq -r '.total_usd' 2>/dev/null || echo "$GAS_DATA" | grep '"total_usd"' | cut -d'"' -f4)
fi

# Validate values
if [ -z "$TIMESTAMP" ] || [ "$TIMESTAMP" = "null" ]; then
    echo "Error: Could not extract timestamp from gas prices data"
    exit 1
fi

# Update GAS_AND_TOKEN_REQUIREMENTS.md
DOCS_DIR="$SCRIPT_DIR/../../docs/deployment"
GAS_DOC="$DOCS_DIR/GAS_AND_TOKEN_REQUIREMENTS.md"

if [ -f "$GAS_DOC" ]; then
    echo "Updating $GAS_DOC..."

    # Update timestamp
    sed -i "s/\*\*Last Updated\*\*:.*/\*\*Last Updated\*\*: $TIMESTAMP/" "$GAS_DOC"

    # Update Ethereum Mainnet costs
    sed -i "s/| \*\*Low\*\* | 20 gwei | 3,000,000 | 0.06 ETH | \$150 |/| \*\*Current\*\* | ${ETH_GAS_GWEI} gwei | 3,000,000 | ${ETH_COST} ETH | \$${ETH_USD_COST} |/" "$GAS_DOC"

    # Update Cronos costs
    sed -i "s/| \*\*Low\*\* | 500 gwei | 8,760,000 | 4.38 CRO | \$0.35 |/| \*\*Current\*\* | ${CRONOS_GAS_GWEI} gwei | 8,760,000 | ${CRONOS_COST} CRO | \$${CRONOS_USD_COST} |/" "$GAS_DOC"

    # Update BSC costs
    sed -i "s/| \*\*Low\*\* | 3 gwei | 8,760,000 | 0.026 BNB | \$7.80 |/| \*\*Current\*\* | ${BSC_GAS_GWEI} gwei | 8,760,000 | ${BSC_COST} BNB | \$${BSC_USD_COST} |/" "$GAS_DOC"

    # Update Polygon costs
    sed -i "s/| \*\*Low\*\* | 30 gwei | 8,760,000 | 0.26 MATIC | \$0.21 |/| \*\*Current\*\* | ${POLYGON_GAS_GWEI} gwei | 8,760,000 | ${POLYGON_COST} MATIC | \$${POLYGON_USD_COST} |/" "$GAS_DOC"

    # Update Gnosis costs
    sed -i "s/| \*\*Low\*\* | 1 gwei | 8,760,000 | 0.00876 xDAI | \$0.01 |/| \*\*Current\*\* | ${GNOSIS_GAS_GWEI} gwei | 8,760,000 | ${GNOSIS_COST} xDAI | \$${GNOSIS_USD_COST} |/" "$GAS_DOC"

    echo "✓ Updated $GAS_DOC"
fi

# Update TOKENS_AND_CHAINS_SUMMARY.md
TOKENS_DOC="$DOCS_DIR/TOKENS_AND_CHAINS_SUMMARY.md"

if [ -f "$TOKENS_DOC" ]; then
    echo "Updating $TOKENS_DOC..."

    # Update timestamp
    sed -i "s/\*\*Last Updated\*\*:.*/\*\*Last Updated\*\*: $TIMESTAMP/" "$TOKENS_DOC"

    # Update recommended balances based on current costs (add 50% buffer)
    ETH_RECOMMENDED=$(echo "scale=4; $ETH_COST * 1.5" | bc)
    CRONOS_RECOMMENDED=$(echo "scale=2; $CRONOS_COST * 1.5" | bc)
    BSC_RECOMMENDED=$(echo "scale=4; $BSC_COST * 1.5" | bc)
    POLYGON_RECOMMENDED=$(echo "scale=2; $POLYGON_COST * 1.5" | bc)
    GNOSIS_RECOMMENDED=$(echo "scale=4; $GNOSIS_COST * 1.5" | bc)

    sed -i "s/| \*\*Ethereum Mainnet\*\* | ETH | \*\*0.20 ETH\*\* | ~\$500 |/| \*\*Ethereum Mainnet\*\* | ETH | \*\*${ETH_RECOMMENDED} ETH\*\* | ~\$${ETH_USD_COST} |/" "$TOKENS_DOC"
    sed -i "s/| \*\*Cronos\*\* | CRO | \*\*15 CRO\*\* | ~\$1.20 |/| \*\*Cronos\*\* | CRO | \*\*${CRONOS_RECOMMENDED} CRO\*\* | ~\$${CRONOS_USD_COST} |/" "$TOKENS_DOC"
    sed -i "s/| \*\*BSC\*\* | BNB | \*\*0.06 BNB\*\* | ~\$18 |/| \*\*BSC\*\* | BNB | \*\*${BSC_RECOMMENDED} BNB\*\* | ~\$${BSC_USD_COST} |/" "$TOKENS_DOC"
    sed -i "s/| \*\*Polygon\*\* | MATIC | \*\*1.0 MATIC\*\* | ~\$0.80 |/| \*\*Polygon\*\* | MATIC | \*\*${POLYGON_RECOMMENDED} MATIC\*\* | ~\$${POLYGON_USD_COST} |/" "$TOKENS_DOC"
    sed -i "s/| \*\*Gnosis\*\* | xDAI | \*\*0.05 xDAI\*\* | ~\$0.05 |/| \*\*Gnosis\*\* | xDAI | \*\*${GNOSIS_RECOMMENDED} xDAI\*\* | ~\$${GNOSIS_USD_COST} |/" "$TOKENS_DOC"

    echo "✓ Updated $TOKENS_DOC"
fi

# Update DEPLOYMENT_QUICK_REFERENCE.md
QUICK_REF_DOC="$DOCS_DIR/DEPLOYMENT_QUICK_REFERENCE.md"

if [ -f "$QUICK_REF_DOC" ]; then
    echo "Updating $QUICK_REF_DOC..."

    sed -i "s/| \*\*Mainnet\*\* | ETH | 0.15 ETH | \*\*0.20 ETH\*\* |/| \*\*Mainnet\*\* | ETH | ${ETH_COST} ETH | \*\*${ETH_RECOMMENDED} ETH\*\* |/" "$QUICK_REF_DOC"
    sed -i "s/| \*\*Cronos\*\* | CRO | 8.76 CRO | \*\*15 CRO\*\* |/| \*\*Cronos\*\* | CRO | ${CRONOS_COST} CRO | \*\*${CRONOS_RECOMMENDED} CRO\*\* |/" "$QUICK_REF_DOC"
    sed -i "s/| \*\*BSC\*\* | BNB | 0.044 BNB | \*\*0.06 BNB\*\* |/| \*\*BSC\*\* | BNB | ${BSC_COST} BNB | \*\*${BSC_RECOMMENDED} BNB\*\* |/" "$QUICK_REF_DOC"
    sed -i "s/| \*\*Polygon\*\* | MATIC | 0.44 MATIC | \*\*1.0 MATIC\*\* |/| \*\*Polygon\*\* | MATIC | ${POLYGON_COST} MATIC | \*\*${POLYGON_RECOMMENDED} MATIC\*\* |/" "$QUICK_REF_DOC"
    sed -i "s/| \*\*Gnosis\*\* | xDAI | 0.0175 xDAI | \*\*0.05 xDAI\*\* |/| \*\*Gnosis\*\* | xDAI | ${GNOSIS_COST} xDAI | \*\*${GNOSIS_RECOMMENDED} xDAI\*\* |/" "$QUICK_REF_DOC"

    echo "✓ Updated $QUICK_REF_DOC"
fi

echo ""
echo "✓ All documentation updated with real-time gas prices"
echo "  Timestamp: $TIMESTAMP"
echo "  Total Estimated Cost: ~\$${TOTAL_USD} USD"

