#!/bin/bash

# Quick test suite for basic functionality
# This script tests the core functionality of particle-os

set -e

echo "🧪 Running quick test suite..."

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Test counter
TESTS_PASSED=0
TESTS_FAILED=0

# Test function
run_test() {
    local test_name="$1"
    local test_command="$2"
    
    echo -e "\n📋 Test: $test_name"
    echo "Command: $test_command"
    
    if eval "$test_command" >/dev/null 2>&1; then
        echo -e "✅ PASS: $test_name"
        ((TESTS_PASSED++))
    else
        echo -e "❌ FAIL: $test_name"
        ((TESTS_FAILED++))
    fi
}

# Test 1: Basic functionality
echo -e "\n${BLUE}=== Test 1: Basic Functionality ===${NC}"

run_test "Version command" "./bib/particle-os --version"
run_test "Help command" "./bib/particle-os --help"
run_test "List recipes" "./bib/particle-os list"

# Test 2: Container operations
echo -e "\n${BLUE}=== Test 2: Container Operations ===${NC}"

run_test "Container list" "./bib/particle-os container list"
run_test "Container inspect" "./bib/particle-os container inspect debian:trixie-slim"

# Test 3: Recipe validation
echo -e "\n${BLUE}=== Test 3: Recipe Validation ===${NC}"

run_test "Validate minimal-debug.yml" "./bib/particle-os validate recipes/minimal-debug.yml"
run_test "Validate simple-cli-bootable.yml" "./bib/particle-os validate recipes/simple-cli-bootable.yml"

# Test 4: Tool availability
echo -e "\n${BLUE}=== Test 4: Tool Availability ===${NC}"

run_test "parted available" "which parted"
run_test "mkfs.ext4 available" "which mkfs.ext4"
run_test "extlinux available" "which extlinux"
run_test "qemu-img available" "which qemu-img"

# Test 5: System resources
echo -e "\n${BLUE}=== Test 5: System Resources ===${NC}"

# Check sudo access
if sudo -n true 2>/dev/null; then
    echo -e "✅ PASS: Sudo access (passwordless)"
    ((TESTS_PASSED++))
else
    echo -e "⚠️  WARN: Sudo access (may require password)"
fi

# Check disk space
TMP_SPACE=$(df -BG /tmp | tail -1 | awk '{print $4}' | sed 's/G//')
if [ "$TMP_SPACE" -ge 5 ]; then
    echo -e "✅ PASS: Sufficient disk space in /tmp (${TMP_SPACE}GB >= 5GB)"
    ((TESTS_PASSED++))
else
    echo -e "❌ FAIL: Insufficient disk space in /tmp (${TMP_SPACE}GB < 5GB)"
    ((TESTS_FAILED++))
fi

# Summary
echo -e "\n${BLUE}=== Test Summary ===${NC}"
echo "Tests passed: $TESTS_PASSED"
echo "Tests failed: $TESTS_FAILED"
echo "Total tests: $((TESTS_PASSED + TESTS_FAILED))"

if [ $TESTS_FAILED -eq 0 ]; then
    echo -e "\n🎉 All tests passed! particle-os is ready for full testing."
    echo "Next step: Recompile binary with sudo fixes and run full test suite."
else
    echo -e "\n⚠️  Some tests failed. Please address these issues before proceeding."
    echo "Failed tests may indicate missing dependencies or configuration issues."
fi

echo -e "\n📝 Next steps:"
echo "1. Install Go 1.21+ to recompile binary"
echo "2. Recompile binary with sudo fixes"
echo "3. Run full test suite: scripts/full-test-suite.sh"
echo "4. Execute testing strategy: docs/TESTING_STRATEGY.md"
