#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2025-2026 Marcus Quinn

# TOON Integration Test for AI DevOps Framework
# Demonstrates TOON format capabilities and integration

set -e

echo "🎒 TOON Format Integration Test"
echo "================================"
echo ""

# Test 1: Basic conversion
echo "📝 Test 1: Basic JSON to TOON conversion"
echo '{"project": "AI DevOps", "version": "1.0", "active": true}' > test-basic.json
./.agents/scripts/toon-helper.sh encode test-basic.json test-basic.toon
echo "✅ Basic conversion completed"
echo ""

# Test 2: Tabular data (most efficient)
echo "📊 Test 2: Tabular data conversion"
cat > test-tabular.json << 'EOF'
{
  "servers": [
    {"id": 1, "name": "web-01", "cpu": 4, "memory": 8192, "status": "running"},
    {"id": 2, "name": "db-01", "cpu": 8, "memory": 16384, "status": "running"},
    {"id": 3, "name": "api-01", "cpu": 2, "memory": 4096, "status": "stopped"}
  ]
}
EOF
./.agents/scripts/toon-helper.sh encode test-tabular.json test-tabular.toon ',' true
echo "✅ Tabular conversion with stats completed"
echo ""

# Test 3: Tab delimiter for better efficiency
echo "🔤 Test 3: Tab delimiter conversion"
./.agents/scripts/toon-helper.sh encode test-tabular.json test-tabular-tab.toon '\t' true
echo "✅ Tab delimiter conversion completed"
echo ""

# Test 4: Round-trip validation
echo "🔄 Test 4: Round-trip validation"
./.agents/scripts/toon-helper.sh decode test-tabular.toon test-restored.json
# Use jq to normalize JSON for comparison (semantic comparison)
if command -v jq &> /dev/null; then
    if jq -S . test-tabular.json > test-normalized.json && jq -S . test-restored.json > test-restored-normalized.json; then
        if diff -q test-normalized.json test-restored-normalized.json > /dev/null; then
            echo "✅ Round-trip validation successful"
        else
            echo "❌ Round-trip validation failed (semantic difference)"
            exit 1
        fi
    else
        echo "⚠️  jq normalization failed, skipping semantic comparison"
    fi
else
    echo "⚠️  jq not available, skipping round-trip validation"
fi
echo ""

# Test 5: TOON validation
echo "✅ Test 5: TOON format validation"
./.agents/scripts/toon-helper.sh validate test-tabular.toon
echo ""

# Test 6: Stdin processing
echo "📥 Test 6: Stdin processing"
echo '{"name": "stdin-test", "items": ["a", "b", "c"]}' | ./.agents/scripts/toon-helper.sh stdin-encode ',' true
echo ""

# Test 7: Comparison analysis
echo "📈 Test 7: Token efficiency comparison"
./.agents/scripts/toon-helper.sh compare test-tabular.json
echo ""

# Show generated files
echo "📁 Generated files:"
ls -la test-*.json test-*.toon 2>/dev/null || true
echo ""

# Display TOON examples
echo "🎯 TOON Format Examples:"
echo ""
echo "Basic format:"
cat test-basic.toon
echo ""
echo "Tabular format (comma-delimited):"
cat test-tabular.toon
echo ""
echo "Tabular format (tab-delimited):"
cat test-tabular-tab.toon
echo ""

# Cleanup
echo "🧹 Cleaning up test files..."
rm -f test-*.json test-*.toon test-*normalized*.json
echo "✅ Cleanup completed"
echo ""

echo "🎉 TOON Integration Test Completed Successfully!"
echo ""
echo "Key Benefits Demonstrated:"
echo "• 20-60% token reduction vs JSON"
echo "• Human-readable tabular format"
echo "• Perfect round-trip conversion"
echo "• Multiple delimiter options"
echo "• Stdin/stdout processing"
echo "• Format validation"
echo "• Token efficiency analysis"
