#!/usr/bin/env bash
# Execute approved multisig proposal
# Usage: ./execute-proposal.sh <multisig_address> <transaction_id>

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../../../.." && pwd)"

source "$PROJECT_ROOT/.env" 2>/dev/null || true

MULTISIG_ADDRESS="${1:-}"
TX_ID="${2:-}"

if [ -z "$MULTISIG_ADDRESS" ] || [ -z "$TX_ID" ]; then
    echo "Usage: $0 <multisig_address> <transaction_id>"
    echo ""
    echo "Example:"
    echo "  $0 0x1234... 42"
    exit 1
fi

RPC_URL="${ETHEREUM_MAINNET_RPC:-${RPC_URL:-}}"
if [ -z "$RPC_URL" ]; then
    echo "Error: RPC_URL or ETHEREUM_MAINNET_RPC must be set"
    exit 1
fi

PRIVATE_KEY="${PRIVATE_KEY:-}"
if [ -z "$PRIVATE_KEY" ]; then
    echo "Error: PRIVATE_KEY must be set"
    exit 1
fi

echo "Executing multisig proposal..."
echo "Multisig: $MULTISIG_ADDRESS"
echo "Transaction ID: $TX_ID"
echo ""

# Check if transaction is approved
echo "Checking transaction status..."
TX_APPROVED=$(cast call "$MULTISIG_ADDRESS" "getTransactionCount(bool,bool)" "true" "true" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")

# Note: Gnosis Safe uses different function signatures
# This is a placeholder - adjust based on your multisig implementation
echo "Transaction approved: $TX_APPROVED"
echo ""

# Execute transaction
# Note: This uses Gnosis Safe's executeTransaction function
# Adjust based on your multisig implementation
EXECUTE_CALLDATA=$(cast calldata "executeTransaction(uint256)" "$TX_ID")

echo "Executing transaction..."
echo "Transaction data: $EXECUTE_CALLDATA"
echo ""

# For safety, this script doesn't automatically execute
# Uncomment the following lines to actually execute:
# cast send "$MULTISIG_ADDRESS" "$EXECUTE_CALLDATA" \
#     --rpc-url "$RPC_URL" \
#     --private-key "$PRIVATE_KEY"

echo "⚠️  To execute this transaction, uncomment the execution code in this script"
echo "   or use the Gnosis Safe web interface"
echo ""
echo "Transaction details:"
echo "  To: $MULTISIG_ADDRESS"
echo "  Data: $EXECUTE_CALLDATA"
echo "  Value: 0 ETH"

