#!/usr/bin/env bash
# Generate manual verification instructions with all required information
# Usage: ./verify-manual-instructions.sh

set -euo pipefail

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

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

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }
log_step() { echo -e "${CYAN}[STEP]${NC} $1"; }

CONTRACT_ADDRESS="0x89dd12025bfCD38A168455A44B400e913ED33BE2"
CONTRACT_NAME="CCIPWETH9Bridge"
STANDARD_JSON_FILE="$DOCS_DIR/CCIPWETH9Bridge_standard_json.json"
CONSTRUCTOR_ARGS="0x0000000000000000000000004a666f96fc8764181194447a7dfdb7d471b301c8"

log_info "========================================="
log_info "Manual Verification Instructions"
log_info "========================================="
log_info ""

log_step "Step 1: Open Etherscan"
log_info "URL: https://etherscan.io/address/${CONTRACT_ADDRESS}#code"
log_info ""

log_step "Step 2: Navigate to Verification"
log_info "1. Click the 'Contract' tab"
log_info "2. Click 'Verify and Publish' button"
log_info ""

log_step "Step 3: Select Verification Method"
log_info "Select: 'Standard JSON Input' (NOT 'Single file')"
log_info ""

log_step "Step 3.5: CRITICAL - Select Compiler Version and Settings"
log_warn "⚠️  IMPORTANT: Select compiler version v0.8.20+commit.a1b79de6"
log_warn "⚠️  DO NOT use 0.8.19 (this causes bytecode mismatch!)"
log_warn "⚠️  CRITICAL: Set 'Via IR' to NO/FALSE (contract was NOT deployed with via-ir!)"
log_warn "⚠️  CRITICAL: Do NOT force EVM version (let it default)"
log_info "The contract was deployed with 0.8.20, WITHOUT via-ir, must match exactly"
log_info ""

log_step "Step 4: Upload Standard JSON"
log_info "File location: $STANDARD_JSON_FILE"
log_info ""

if [ -f "$STANDARD_JSON_FILE" ]; then
    log_success "Standard JSON file found"
    log_info "File size: $(du -h "$STANDARD_JSON_FILE" | cut -f1)"
    log_info ""
    log_info "You can:"
    log_info "  - Copy the file path above"
    log_info "  - Or view contents: cat $STANDARD_JSON_FILE"
else
    log_error "Standard JSON file not found: $STANDARD_JSON_FILE"
fi

log_info ""

log_step "Step 5: Enter Constructor Arguments"
log_warn "⚠️  CRITICAL: Constructor takes ONLY ONE address argument"
log_info "Constructor Arguments (ABI-encoded):"
log_info "0x0000000000000000000000004a666f96fc8764181194447a7dfdb7d471b301c8"
log_info ""
log_info "This is the deployer/admin address (single argument, not 3!)"
log_info ""

log_step "Step 6: Submit"
log_info "Click 'Verify and Publish' button"
log_info "Wait 30-60 seconds for verification"
log_info ""

log_info "========================================="
log_info "Quick Copy Commands"
log_info "========================================="
log_info ""

log_info "Open Etherscan:"
echo "xdg-open https://etherscan.io/address/${CONTRACT_ADDRESS}#code 2>/dev/null || echo 'Open manually: https://etherscan.io/address/${CONTRACT_ADDRESS}#code'"
log_info ""

log_info "View Standard JSON file:"
echo "cat $STANDARD_JSON_FILE"
log_info ""

log_info "Copy constructor args (single address):"
echo "echo '0x0000000000000000000000004a666f96fc8764181194447a7dfdb7d471b301c8' | xclip -selection clipboard 2>/dev/null || echo '0x0000000000000000000000004a666f96fc8764181194447a7dfdb7d471b301c8'"
log_info ""

log_success "Instructions generated!"
log_info ""
log_warn "Note: Automated verification failed due to Etherscan API V2 migration."
log_info "Manual verification via UI is currently the most reliable method."
log_info ""

