#!/bin/bash
# weave_cleanup.sh — Reset NRP FABRIC SENSE weave to a clean state
set -e

WEAVE_DIR="$(cd "$(dirname "$0")" && pwd)"

echo "=== NRP FABRIC SENSE Cleanup ==="

# Clear logs
: > "${WEAVE_DIR}/weave.log"
echo "  Cleared weave.log"

# Remove state files
rm -f "${WEAVE_DIR}"/.state-*.json
echo "  Removed state files"

# Clear notebook outputs
if command -v python3 &>/dev/null; then
  python3 -c "
import json, glob
for path in glob.glob('${WEAVE_DIR}/notebooks/*.ipynb'):
    with open(path) as f:
        nb = json.load(f)
    for cell in nb.get('cells', []):
        if cell.get('cell_type') == 'code':
            cell['outputs'] = []
            cell['execution_count'] = None
    with open(path, 'w') as f:
        json.dump(nb, f, indent=1)
        f.write('\n')
    print(f'  Cleared outputs: {path.split(\"/\")[-1]}')
"
fi

rm -rf "${WEAVE_DIR}/notebooks/.ipynb_checkpoints"
echo "Done. Weave is clean and ready for a fresh run."
