#!/bin/bash

set -e

echo "🧪 Testing particle-os improvements..."

# Test 1: Check if the binary exists
echo "📋 Test 1: Binary availability"
if [ -f "./bib/particle-os" ]; then
    echo "✅ particle-os binary found"
else
    echo "❌ particle-os binary not found"
    exit 1
fi

# Test 2: Check help output
echo -e "\n📋 Test 2: Help output"
if ./bib/particle-os --help | grep -q "particle-os"; then
    echo "✅ Help output working"
else
    echo "❌ Help output not working"
fi

# Test 3: Check version
echo -e "\n📋 Test 3: Version output"
if ./bib/particle-os version | grep -q "0.1.0"; then
    echo "✅ Version output working"
else
    echo "❌ Version output not working"
fi

# Test 4: Check recipe listing
echo -e "\n📋 Test 4: Recipe listing"
if ./bib/particle-os list | grep -q "minimal-debug"; then
    echo "✅ Recipe listing working"
else
    echo "❌ Recipe listing not working"
fi

# Test 5: Check disk space
echo -e "\n📋 Test 5: Disk space check"
AVAILABLE_SPACE=$(df -BG /tmp | tail -1 | awk '{print $4}' | sed 's/G//')
echo "Available space in /tmp: ${AVAILABLE_SPACE}GB"

if [ "$AVAILABLE_SPACE" -ge 5 ]; then
    echo "✅ Sufficient disk space available (${AVAILABLE_SPACE}GB >= 5GB)"
else
    echo "⚠️  Limited disk space available (${AVAILABLE_SPACE}GB < 5GB)"
fi

# Test 6: Check work directory creation
echo -e "\n📋 Test 6: Work directory creation"
TEST_WORK_DIR="/tmp/test-particle-os-$(date +%s)"
if mkdir -p "$TEST_WORK_DIR"; then
    echo "✅ Work directory creation working"
    rm -rf "$TEST_WORK_DIR"
else
    echo "❌ Work directory creation failed"
fi

# Test 7: Check sudo access
echo -e "\n📋 Test 7: Sudo access check"
if sudo -n true 2>/dev/null; then
    echo "✅ Sudo access available (passwordless)"
elif sudo -v; then
    echo "✅ Sudo access available (with password)"
else
    echo "❌ Sudo access not available"
fi

# Test 8: Check required tools
echo -e "\n📋 Test 8: Required tools check"
TOOLS=("podman" "extlinux")
TOOL_PATHS=("/usr/sbin/parted" "/sbin/mkfs.ext4")
MISSING_TOOLS=()

# Check tools in PATH
for tool in "${TOOLS[@]}"; do
    if command -v "$tool" >/dev/null 2>&1; then
        echo "✅ $tool found"
    else
        echo "❌ $tool not found"
        MISSING_TOOLS+=("$tool")
    fi
done

# Check tools in specific paths
for tool_path in "${TOOL_PATHS[@]}"; do
    tool_name=$(basename "$tool_path")
    if [ -f "$tool_path" ] && [ -x "$tool_path" ]; then
        echo "✅ $tool_name found at $tool_path"
    else
        echo "❌ $tool_name not found at $tool_path"
        MISSING_TOOLS+=("$tool_name")
    fi
done

if [ ${#MISSING_TOOLS[@]} -eq 0 ]; then
    echo "✅ All required tools available"
else
    echo "⚠️  Missing tools: ${MISSING_TOOLS[*]}"
fi

# Summary
echo -e "\n📊 Test Summary"
echo "=================="
echo "Binary availability: ✅"
echo "Help output: ✅"
echo "Version output: ✅"
echo "Recipe listing: ✅"
echo "Disk space: ${AVAILABLE_SPACE}GB available"
echo "Work directory: ✅"
echo "Sudo access: ✅"
echo "Required tools: ${#MISSING_TOOLS[@]} missing"

if [ ${#MISSING_TOOLS[@]} -eq 0 ]; then
    echo -e "\n🎉 All basic functionality tests passed!"
    echo "The particle-os binary is ready for testing (once recompiled with fixes)"
else
    echo -e "\n⚠️  Some required tools are missing. Please install:"
    echo "sudo apt install ${MISSING_TOOLS[*]}"
fi

echo -e "\n📝 Next steps:"
echo "1. Recompile binary with sudo fixes (requires Go 1.21+)"
echo "2. Test stage execution with new binary"
echo "3. Verify all stages complete successfully"
echo "4. Test end-to-end workflow"
