#!/bin/bash

# Full test suite for comprehensive validation
# This script tests all stages and end-to-end workflows

set -e

echo "🧪 Running full 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

# Create test work directory
TEST_DIR="/tmp/particle-os-test-$(date +%s)"
mkdir -p "$TEST_DIR"

echo "Test work directory: $TEST_DIR"

# Cleanup function
cleanup() {
    echo -e "\n🧹 Cleaning up test directories..."
    if [ -d "$TEST_DIR" ]; then
        sudo rm -rf "$TEST_DIR"
        echo "Test directories cleaned up"
    fi
}

# Set trap to cleanup on exit
trap cleanup EXIT

# Test function
run_test() {
    local test_name="$1"
    local test_command="$2"
    local work_subdir="$3"
    
    echo -e "\n📋 Test: $test_name"
    echo "Command: $test_command"
    
    # Create work subdirectory if specified
    if [ -n "$work_subdir" ]; then
        local full_work_dir="$TEST_DIR/$work_subdir"
        mkdir -p "$full_work_dir"
        test_command=$(echo "$test_command" | sed "s|--work-dir [^ ]*|--work-dir $full_work_dir|")
        echo "Work directory: $full_work_dir"
    fi
    
    if eval "$test_command" >/dev/null 2>&1; then
        echo -e "✅ PASS: $test_name"
        ((TESTS_PASSED++))
    else
        echo -e "❌ FAIL: $test_name"
        ((TESTS_FAILED++))
        
        # Show error details for failed tests
        echo "Error details:"
        eval "$test_command" 2>&1 | head -20
    fi
}

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

run_test "Container listing" "./bib/particle-os container list"
run_test "Container inspection" "./bib/particle-os container inspect debian:trixie-slim"
run_test "Recipe listing" "./bib/particle-os list"
run_test "Recipe validation" "./bib/particle-os validate recipes/minimal-debug.yml"

# Phase 2: Stage Execution Testing
echo -e "\n${BLUE}=== Phase 2: Stage Execution Testing ===${NC}"

echo -e "\n${YELLOW}Note: These tests require the binary to be recompiled with sudo fixes${NC}"
echo "If tests fail with permission errors, the binary needs recompilation"

run_test "apt stage (minimal-debug)" "./bib/particle-os build --work-dir /tmp/test-apt recipes/minimal-debug.yml --verbose" "apt"
run_test "locale stage (minimal-debug-locale)" "./bib/particle-os build --work-dir /tmp/test-locale recipes/minimal-debug-locale.yml --verbose" "locale"
run_test "complete workflow (simple-cli-bootable)" "./bib/particle-os build --work-dir /tmp/test-cli recipes/simple-cli-bootable.yml --verbose" "cli"

# Phase 3: QEMU Stage Testing
echo -e "\n${BLUE}=== Phase 3: QEMU Stage Testing ===${NC}"

run_test "QEMU stage (multiple formats)" "./bib/particle-os build --work-dir /tmp/test-qemu recipes/qemu-test.yml --verbose" "qemu"

# Phase 4: Error Handling Testing
echo -e "\n${BLUE}=== Phase 4: Error Handling Testing ===${NC}"

# Test with invalid recipe (should fail gracefully)
echo -e "\n📋 Test: Invalid recipe handling"
if ./bib/particle-os build --work-dir /tmp/test-error invalid-recipe.yml --verbose 2>&1 | grep -q "error\|failed"; then
    echo -e "✅ PASS: Invalid recipe handled gracefully"
    ((TESTS_PASSED++))
else
    echo -e "❌ FAIL: Invalid recipe not handled properly"
    ((TESTS_FAILED++))
fi

# Phase 5: Resource Validation
echo -e "\n${BLUE}=== Phase 5: Resource Validation ===${NC}"

# Check if any images were created
echo -e "\n📋 Test: Image creation validation"
if find "$TEST_DIR" -name "*.img" -o -name "*.qcow2" -o -name "*.vmdk" -o -name "*.vdi" | grep -q .; then
    echo -e "✅ PASS: Images were created successfully"
    echo "Created images:"
    find "$TEST_DIR" -name "*.img" -o -name "*.qcow2" -o -name "*.vmdk" -o -name "*.vdi" | head -10
    ((TESTS_PASSED++))
else
    echo -e "❌ FAIL: No images were created"
    ((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 fully functional."
    echo "The tool is ready for production use."
else
    echo -e "\n⚠️  Some tests failed. Please review the failures and address issues."
    echo ""
    echo "Common failure causes:"
    echo "1. Binary not recompiled with sudo fixes"
    echo "2. Insufficient disk space"
    echo "3. Missing system tools"
    echo "4. Permission issues"
fi

echo -e "\n📝 Next steps:"
if [ $TESTS_FAILED -eq 0 ]; then
    echo "1. ✅ All tests passed - tool is production ready"
    echo "2. Run performance testing"
    echo "3. Test with real-world recipes"
    echo "4. Deploy to CI/CD systems"
else
    echo "1. Recompile binary with sudo fixes (if permission errors)"
    echo "2. Address disk space issues (if space errors)"
    echo "3. Install missing tools (if tool errors)"
    echo "4. Re-run test suite after fixes"
fi

echo -e "\n📚 Documentation:"
echo "- Testing strategy: docs/TESTING_STRATEGY.md"
echo "- Usage guide: docs/HOW-TO-USE.md"
echo "- CI/CD guide: docs/HOW-TO-USE-AS-CICD.md"
echo "- Project status: todo"
