#!/usr/bin/env bash
set -euo pipefail

cd "$(dirname "$0")"

echo "🎨 Running nickel-roundtrip with TUI backend..."
echo ""

# Check if config.ncl exists
if [ ! -f "config.ncl" ]; then
    echo "❌ config.ncl not found! Run ./01-generate-initial-config.sh first"
    exit 1
fi

# Backup current config
cp config.ncl "config.ncl.backup.$(date +%Y%m%d_%H%M%S)"

# Run roundtrip with TUI backend
cargo run -p typedialog-tui --release -- nickel-roundtrip \
    --input config.ncl \
    --form ci-form.ncl \
    --output config.ncl \
    --ncl-template config.ncl.j2 \
    --verbose

echo ""
echo "✅ Roundtrip completed!"
echo ""
echo "Review changes:"
echo "  cat config.ncl"
echo ""
echo "Run again:"
echo "  ./03-roundtrip-tui.sh"
