#!/bin/bash
# XDC Gap Analysis: Complete Workflow Script
# Usage: ./create-gap-analysis-issue.sh
# This script:
# 1. Runs the gap analysis
# 2. Packages results for Kimi deep research
# 3. Creates a GitHub issue with findings

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)"
ISSUE_FILE="$OUTPUT_DIR/github-issue.md"

echo "=========================================="
echo "XDC Gap Analysis: Kimi + Opus 4.7 Workflow"
echo "=========================================="
echo ""

# Step 1: Run analysis script
if [ -f "$GP5_PATH/run-gap-analysis.sh" ]; then
    echo "[1/3] Running gap analysis..."
    cd "$GP5_PATH"
    bash run-gap-analysis.sh
else
    echo "ERROR: run-gap-analysis.sh not found"
    exit 1
fi

echo ""
echo "[2/3] Packaging results for research..."

# Create a summary of key findings for the issue
cat > "$ISSUE_FILE" << 'HEADER'
# XDCIndia/go-ethereum vs Official XDC v2.6.8: Gap Analysis

## Overview
Systematic comparison between GP5 fork (`xdc-network` branch) and official XDC v2.6.8 to identify missing features, protocol divergences, and consensus-critical gaps.

## Repositories Compared
| | XDCIndia/go-ethereum | XinFinOrg/XDPoSChain |
|---|---|---|
| **Branch/Tag** | `xdc-network` | `v2.6.8` |
| **Base** | Geth 1.17 fork | XDPoSChain |
| **Latest Commit** | `576871c11` | `146252a` |
| **Key Fix** | Snapshot Version gate (#385/#387) | SwitchEpoch comparison removal |

## Methodology
1. File-level comparison of critical directories
2. Diff analysis of key consensus/protocol files
3. Keyword search for XDPoS v2 specific features
4. Cross-reference with known production issues

## Key Files Analyzed
- `consensus/XDPoS/engines/engine_v2/engine.go` — V2 consensus engine
- `consensus/XDPoS/engines/engine_v1/engine.go` — V1 consensus engine
- `consensus/XDPoS/api.go` — XDPoS RPC APIs
- `core/blockchain.go` — Block import pipeline
- `core/state_processor.go` — State transition
- `core/types/block.go` — Block structure
- `eth/handler.go` — Protocol handler
- `params/config.go` — Chain configuration
- `params/bootnodes.go` — Bootnode lists

## Findings Summary

### Critical Issues (Consensus/Sync Failure Risk)
*To be filled by Kimi deep research + Opus 4.7 validation*

### High Priority Missing Features
*To be filled by Kimi deep research + Opus 4.7 validation*

### Medium Priority Divergences
*To be filled by Kimi deep research + Opus 4.7 validation*

### Low Priority / Future Issues
*To be filled by Kimi deep research + Opus 4.7 validation*

## Known Issues Reference
- #196: Gas limit sync failures
- #218: Sync blocked at specific block
- #219: Fix for #218
- #315: Handshake TD mismatch
- #316: Fork ID issues
- #320: Metrics differences
- #321: XDC divergences
- #385/#387: Snapshot Version gate bug

## Next Steps
1. **Kimi Deep Research**: Run detailed analysis using `RESEARCH_PROMPT_XDC_GAP_ANALYSIS.md`
2. **Opus 4.7 Validation**: Validate findings using `VALIDATION_PROMPT_OPUS_47.md`
3. **Implementation**: Prioritize and fix validated gaps
4. **Testing**: Verify fixes against v2.6.8 mainnet/apothem

## Attachments
- Full diffs: See `xdc-gap-analysis-YYYYMMDD/diff-*.txt`
- Missing files: See `xdc-gap-analysis-YYYYMMDD/missing-*.txt`
- Keyword search results: See `xdc-gap-analysis-YYYYMMDD/*-keyword-*.txt`

---
*This issue was created by automated gap analysis workflow.*
HEADER

echo "Issue template created at: $ISSUE_FILE"
echo ""

# Step 3: Print instructions
echo "[3/3] Workflow Complete!"
echo ""
echo "=========================================="
echo "NEXT STEPS:"
echo "=========================================="
echo ""
echo "1. KIMI DEEP RESEARCH:"
echo "   Copy the contents of:"
echo "   $GP5_PATH/RESEARCH_PROMPT_XDC_GAP_ANALYSIS.md"
echo "   Paste into Kimi with deep research enabled"
echo "   Provide the analysis output directory: $OUTPUT_DIR"
echo ""
echo "2. OPUS 4.7 VALIDATION:"
echo "   After Kimi completes, copy:"
echo "   $GP5_PATH/VALIDATION_PROMPT_OPUS_47.md"
echo "   Run with Claude Code CLI using Opus model:"
echo "   claude --model claude-opus-4-7-20251001"
echo "   Provide Kimi's report as input"
echo ""
echo "3. CREATE GITHUB ISSUE:"
echo "   Use the issue template: $ISSUE_FILE"
echo "   Post to: https://github.com/XDCIndia/go-ethereum/issues/new"
echo "   Include validated findings from Opus 4.7"
echo ""
echo "4. FILES READY FOR RESEARCH:"
echo "   Output directory: $OUTPUT_DIR"
echo "   - $(ls $OUTPUT_DIR/diff-*.txt 2>/dev/null | wc -l) diff files"
echo "   - $(ls $OUTPUT_DIR/*keyword*.txt 2>/dev/null | wc -l) keyword search results"
echo "   - analysis-summary.txt for quick reference"
echo ""
echo "=========================================="
