#!/usr/bin/env bash
# Instructions and helper for fixing wallet display issues
# Usage: ./fix-wallet-display.sh [weth9|weth10|both]

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
source "$PROJECT_ROOT/scripts/lib/address-inventory.sh"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }

load_explorer_runtime_env
WETH9_ADDRESS="$(resolve_address_value WETH9_ADDRESS WETH9_ADDRESS 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2)"
WETH10_ADDRESS="$(resolve_address_value WETH10_ADDRESS WETH10_ADDRESS 0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f)"

TOKEN="${1:-both}"

log_info "========================================="
log_info "Wallet Display Fix Instructions"
log_info "========================================="
log_info ""

if [ "$TOKEN" = "weth9" ] || [ "$TOKEN" = "both" ]; then
    log_info "WETH9 Display Fix:"
    log_info "  Contract: $WETH9_ADDRESS"
    log_info "  Issue: decimals() returns 0 instead of 18"
    log_info "  Impact: Incorrect display in wallets"
    log_info ""
    log_info "  MetaMask Fix:"
    log_info "    1. Open MetaMask"
    log_info "    2. Go to Import Tokens"
    log_info "    3. Enter contract address: $WETH9_ADDRESS"
    log_info "    4. Token Symbol: WETH"
    log_info "    5. Decimals: 18 ⚠️ IMPORTANT: Set to 18 (not 0)"
    log_info "    6. Click Add Custom Token"
    log_info ""
fi

if [ "$TOKEN" = "weth10" ] || [ "$TOKEN" = "both" ]; then
    log_info "WETH10 Display Fix:"
    log_info "  Contract: $WETH10_ADDRESS"
    log_info "  Status: ✅ decimals() returns 18 (correct)"
    log_info "  Note: Should display correctly in wallets"
    log_info ""
fi

log_info "========================================="
log_info "Alternative: Use Token List"
log_info "========================================="
log_info ""
log_info "Token list with correct decimals available at:"
log_info "  docs/METAMASK_TOKEN_LIST_FIXED.json"
log_info ""
log_info "To use:"
log_info "  1. Host the JSON file (GitHub, IPFS, etc.)"
log_info "  2. Add URL to MetaMask: Settings → Security & Privacy → Token Lists"
log_info "  3. Tokens will appear with correct decimals"
log_info ""

log_info "========================================="
log_info "Developer Notes"
log_info "========================================="
log_info ""
log_info "Always use decimals = 18 in code:"
log_info "  - Don't read decimals() from WETH9 contract"
log_info "  - Always hardcode: const decimals = 18;"
log_info "  - WETH10 decimals() is correct, but still use 18 for consistency"
log_info ""
