#!/bin/bash
# XDC Gap Analysis: Kimi Research + Opus 4.7 Validation Workflow
# Run this on a machine with both Kimi access and Claude Code (Opus) access

set -euo pipefail

RESEARCH_DIR="/Users/anilchinchawale/github/XDCNetwork"
GP5_PATH="$RESEARCH_DIR/XDC-Geth"
V268_PATH="$RESEARCH_DIR/v268-official"
OUTPUT_DIR="$RESEARCH_DIR/xdc-gap-analysis-$(date +%Y%m%d)"

mkdir -p "$OUTPUT_DIR"

echo "=== XDC Gap Analysis Workflow ==="
echo "GP5 Path: $GP5_PATH"
echo "v2.6.8 Path: $V268_PATH"
echo "Output: $OUTPUT_DIR"
echo ""

# Step 1: Verify repositories exist
echo "[1/5] Verifying repositories..."
if [ ! -d "$GP5_PATH/.git" ]; then
    echo "ERROR: GP5 repository not found at $GP5_PATH"
    exit 1
fi
if [ ! -d "$V268_PATH/.git" ]; then
    echo "Cloning v2.6.8 official repository..."
    git clone --depth 1 --branch v2.6.8 https://github.com/XinFinOrg/XDPoSChain.git "$V268_PATH"
fi

echo "GP5 branch: $(cd $GP5_PATH && git branch --show-current)"
echo "GP5 commit: $(cd $GP5_PATH && git log --oneline -1)"
echo "v2.6.8 commit: $(cd $V268_PATH && git log --oneline -1)"
echo ""

# Step 2: Generate file lists for comparison
echo "[2/5] Generating file comparison lists..."

# Critical directories to compare
CRITICAL_DIRS="consensus/XDPoS core eth params cmd/geth rpc p2p"

for dir in $CRITICAL_DIRS; do
    echo "  Comparing $dir..."
    
    # List files in both
    (cd "$V268_PATH" && find "$dir" -name "*.go" -not -name "*_test.go" | sort) > "$OUTPUT_DIR/v268-${dir//\//_}-files.txt" 2>/dev/null || true
    (cd "$GP5_PATH" && find "$dir" -name "*.go" -not -name "*_test.go" | sort) > "$OUTPUT_DIR/gp5-${dir//\//_}-files.txt" 2>/dev/null || true
    
    # Find files only in v268 (missing from GP5)
    if [ -f "$OUTPUT_DIR/v268-${dir//\//_}-files.txt" ] && [ -f "$OUTPUT_DIR/gp5-${dir//\//_}-files.txt" ]; then
        comm -23 <(cat "$OUTPUT_DIR/v268-${dir//\//_}-files.txt") <(cat "$OUTPUT_DIR/gp5-${dir//\//_}-files.txt") > "$OUTPUT_DIR/missing-from-gp5-${dir//\//_}.txt" 2>/dev/null || true
    fi
done

echo ""

# Step 3: Generate key file diffs
echo "[3/5] Generating key file diffs..."

KEY_FILES=(
    "consensus/XDPoS/engines/engine_v2/engine.go"
    "consensus/XDPoS/engines/engine_v1/engine.go"
    "consensus/XDPoS/utils.go"
    "consensus/XDPoS/api.go"
    "core/blockchain.go"
    "core/state_processor.go"
    "core/types/block.go"
    "eth/handler.go"
    "params/config.go"
    "params/bootnodes.go"
)

for file in "${KEY_FILES[@]}"; do
    if [ -f "$V268_PATH/$file" ] && [ -f "$GP5_PATH/$file" ]; then
        echo "  Diffing $file..."
        diff -u "$GP5_PATH/$file" "$V268_PATH/$file" > "$OUTPUT_DIR/diff-$(basename $file).txt" 2>/dev/null || true
    elif [ -f "$V268_PATH/$file" ] && [ ! -f "$GP5_PATH/$file" ]; then
        echo "  MISSING in GP5: $file"
        echo "$file" >> "$OUTPUT_DIR/missing-files-gp5.txt"
    fi
done

echo ""

# Step 4: Search for XDPoS v2 keywords
echo "[4/5] Searching for XDPoS v2 keywords..."

KEYWORDS=("SwitchBlock" "SwitchEpoch" "SnapshotVersion" "v2.SwitchBlock" "timeout" "penalty" "masternode" "epoch" "checkpoint" "eth/100" "eth/66" "forkID" "Eip1559Block")

for keyword in "${KEYWORDS[@]}"; do
    echo "  Searching: $keyword"
    safe_keyword=$(echo "$keyword" | tr '/ ' '_')
    (cd "$V268_PATH" && grep -r "$keyword" --include="*.go" -l 2>/dev/null | head -20) > "$OUTPUT_DIR/v268-keyword-$safe_keyword.txt" || true
    (cd "$GP5_PATH" && grep -r "$keyword" --include="*.go" -l 2>/dev/null | head -20) > "$OUTPUT_DIR/gp5-keyword-$safe_keyword.txt" || true
done

echo ""

# Step 5: Generate summary stats
echo "[5/5] Generating summary statistics..."

cat > "$OUTPUT_DIR/analysis-summary.txt" << EOF
XDC Gap Analysis Summary
Generated: $(date)

GP5 Repository: $GP5_PATH
  Branch: $(cd $GP5_PATH && git branch --show-current)
  Commit: $(cd $GP5_PATH && git log --oneline -1)

v2.6.8 Repository: $V268_PATH
  Tag: v2.6.8
  Commit: $(cd $V268_PATH && git log --oneline -1)

Files Missing from GP5:
$(cat "$OUTPUT_DIR/missing-files-gp5.txt" 2>/dev/null | wc -l) files

Key Files with Differences:
$(ls -1 "$OUTPUT_DIR"/diff-*.txt 2>/dev/null | wc -l) files

Next Steps:
1. Review diffs in $OUTPUT_DIR/diff-*.txt
2. Check missing files in $OUTPUT_DIR/missing-files-gp5.txt
3. Run Kimi deep research with RESEARCH_PROMPT_XDC_GAP_ANALYSIS.md
4. Validate results with Opus 4.7 using VALIDATION_PROMPT_OPUS_47.md
EOF

cat "$OUTPUT_DIR/analysis-summary.txt"

echo ""
echo "=== Analysis Complete ==="
echo "Output directory: $OUTPUT_DIR"
echo ""
echo "Next steps:"
echo "1. Review generated diffs and missing files"
echo "2. Run Kimi with: cat $GP5_PATH/RESEARCH_PROMPT_XDC_GAP_ANALYSIS.md"
echo "3. After Kimi completes, validate with Opus 4.7 using: $GP5_PATH/VALIDATION_PROMPT_OPUS_47.md"
echo "4. Create GitHub issue with validated findings"
