#!/bin/bash

# apt-ostree Compatibility Validator
# Tests CLI compatibility with rpm-ostree

set -e

echo "🔍 Validating apt-ostree compatibility with rpm-ostree..."
echo "=================================================="

# Check if both commands are available
if ! command -v rpm-ostree &> /dev/null; then
    echo "❌ rpm-ostree not found. Install it first for comparison."
    exit 1
fi

if ! command -v apt-ostree &> /dev/null; then
    echo "❌ apt-ostree not found. Build it first with 'cargo build --release'"
    exit 1
fi

echo "✅ Both commands available"

# Test command structure
echo ""
echo "📋 Testing command structure..."

# Get command lists
rpm_ostree_commands=$(rpm-ostree --help 2>/dev/null | grep -E "^  [a-z-]+" | awk '{print $1}' | sort)
apt_ostree_commands=$(apt-ostree --help 2>/dev/null | grep -E "^  [a-z-]+" | awk '{print $1}' | sort)

echo "rpm-ostree commands: $(echo "$rpm_ostree_commands" | wc -l)"
echo "apt-ostree commands: $(echo "$apt_ostree_commands" | wc -l)"

# Find missing commands
missing_commands=$(comm -23 <(echo "$rpm_ostree_commands") <(echo "$apt_ostree_commands"))
extra_commands=$(comm -13 <(echo "$rpm_ostree_commands") <(echo "$apt_ostree_commands"))

if [ -n "$missing_commands" ]; then
    echo "❌ Missing commands:"
    echo "$missing_commands" | sed 's/^/  - /'
else
    echo "✅ All rpm-ostree commands are available"
fi

if [ -n "$extra_commands" ]; then
    echo "ℹ️  Extra commands:"
    echo "$extra_commands" | sed 's/^/  + /'
fi

# Test help command compatibility
echo ""
echo "📖 Testing help command compatibility..."

# Compare help output structure
rpm_help_lines=$(rpm-ostree --help 2>/dev/null | wc -l)
apt_help_lines=$(apt-ostree --help 2>/dev/null | wc -l)

echo "rpm-ostree help lines: $rpm_help_lines"
echo "apt-ostree help lines: $apt_help_lines"

# Test basic command execution
echo ""
echo "🧪 Testing basic command execution..."

# Test status command
echo "Testing 'status' command..."
if apt-ostree status &>/dev/null; then
    echo "✅ Status command works"
else
    echo "❌ Status command failed"
fi

# Test help command
echo "Testing '--help' flag..."
if apt-ostree --help &>/dev/null; then
    echo "✅ Help flag works"
else
    echo "❌ Help flag failed"
fi

# Test version command
echo "Testing '--version' flag..."
if apt-ostree --version &>/dev/null; then
    echo "✅ Version flag works"
else
    echo "❌ Version flag failed"
fi

# Test invalid command handling
echo ""
echo "⚠️  Testing error handling..."

# Test invalid command
echo "Testing invalid command..."
if apt-ostree invalid-command &>/dev/null; then
    echo "❌ Invalid command should have failed"
else
    echo "✅ Invalid command properly rejected"
fi

# Test invalid flag
echo "Testing invalid flag..."
if apt-ostree --invalid-flag &>/dev/null; then
    echo "❌ Invalid flag should have failed"
else
    echo "✅ Invalid flag properly rejected"
fi

echo ""
echo "🎯 Compatibility validation complete!"
echo ""
echo "Summary:"
echo "- Commands available: $(echo "$apt_ostree_commands" | wc -l)/$(echo "$rpm_ostree_commands" | wc -l)"
echo "- Missing: $(echo "$missing_commands" | wc -l)"
echo "- Extra: $(echo "$extra_commands" | wc -l)"

if [ -z "$missing_commands" ]; then
    echo "🎉 100% command compatibility achieved!"
else
    echo "⚠️  Some commands still need implementation"
fi
