#!/bin/bash
# Safe Testing Script for Debian Atomic Terminal Installer
# This script ONLY uses containers and never touches the host system

set -e

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

log() {
    echo -e "${GREEN}[SAFE-TEST]${NC} $1"
}

warn() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

error() {
    echo -e "${RED}[ERROR]${NC} $1"
    exit 1
}

# Test 1: Container build
test_container_build() {
    log "Testing container build..."
    
    if just build; then
        log "✅ Container built successfully"
        return 0
    else
        error "❌ Container build failed"
        return 1
    fi
}

# Test 2: Container functionality (safe)
test_container_functionality() {
    log "Testing container functionality..."
    
    # Test if container can start
    if podman run --rm --entrypoint /bin/bash debian-atomic-tui-installer:latest -c "echo 'Container is working'"; then
        log "✅ Container can start and run commands"
    else
        error "❌ Container cannot start"
    fi
    
    # Test if install script exists
    if podman run --rm --entrypoint /bin/bash debian-atomic-tui-installer:latest -c "test -f /usr/local/bin/install-debian-atomic"; then
        log "✅ Install script exists in container"
    else
        error "❌ Install script not found in container"
    fi
    
    # Test if welcome script exists
    if podman run --rm --entrypoint /bin/bash debian-atomic-tui-installer:latest -c "test -f /usr/local/bin/welcome"; then
        log "✅ Welcome script exists in container"
    else
        error "❌ Welcome script not found in container"
    fi
    
    # Test if help script exists
    if podman run --rm --entrypoint /bin/bash debian-atomic-tui-installer:latest -c "test -f /usr/local/bin/help"; then
        log "✅ Help script exists in container"
    else
        error "❌ Help script not found in container"
    fi
}

# Test 3: Script syntax validation
test_script_syntax() {
    log "Testing script syntax..."
    
    # Test installation script syntax
    if bash -n scripts/install.sh; then
        log "✅ Installation script syntax is valid"
    else
        error "❌ Installation script has syntax errors"
    fi
    
    # Test test script syntax
    if bash -n scripts/test-container.sh; then
        log "✅ Test script syntax is valid"
    else
        error "❌ Test script has syntax errors"
    fi
}

# Test 4: Configuration files
test_config_files() {
    log "Testing configuration files..."
    
    if [ -f config/installer.conf ]; then
        log "✅ Configuration file exists"
    else
        error "❌ Configuration file not found"
    fi
    
    if [ -f README.md ]; then
        log "✅ README exists"
    else
        error "❌ README not found"
    fi
    
    if [ -f QUICK_START.md ]; then
        log "✅ Quick start guide exists"
    else
        error "❌ Quick start guide not found"
    fi
}

# Test 5: Justfile commands
test_justfile() {
    log "Testing justfile commands..."
    
    # Test help command
    if just help > /dev/null 2>&1; then
        log "✅ Help command works"
    else
        warn "⚠️ Help command failed"
    fi
    
    # Test status command
    if just status > /dev/null 2>&1; then
        log "✅ Status command works"
    else
        warn "⚠️ Status command failed"
    fi
}

# Test 6: Container filesystem extraction (safe)
test_filesystem_extraction() {
    log "Testing filesystem extraction..."
    
    # Create a temporary container to extract filesystem
    local temp_container="temp-extract-$(date +%s)"
    
    # Create container
    podman create --name "$temp_container" debian-atomic-tui-installer:latest
    
    # Extract filesystem to build directory
    if podman export "$temp_container" | tar -x -C build/ 2>/dev/null; then
        log "✅ Filesystem extraction works"
        
        # Check for key files
        if [ -f build/boot/vmlinuz-* ]; then
            log "✅ Kernel found in extracted filesystem"
        else
            warn "⚠️ Kernel not found in extracted filesystem"
        fi
        
        if [ -f build/boot/initrd.img-* ]; then
            log "✅ Initrd found in extracted filesystem"
        else
            warn "⚠️ Initrd not found in extracted filesystem"
        fi
        
        if [ -f build/usr/local/bin/install-debian-atomic ]; then
            log "✅ Install script found in extracted filesystem"
        else
            warn "⚠️ Install script not found in extracted filesystem"
        fi
    else
        error "❌ Filesystem extraction failed"
    fi
    
    # Clean up
    podman rm "$temp_container" > /dev/null 2>&1 || true
}

# Test 7: Container cleanup
test_container_cleanup() {
    log "Testing container cleanup..."
    
    # Remove any existing test containers
    podman rm -f test-tui-installer 2>/dev/null || true
    
    log "✅ Container cleanup completed"
}

# Run all safe tests
run_safe_tests() {
    log "Starting safe container-based tests..."
    echo ""
    
    test_script_syntax
    test_config_files
    test_justfile
    test_container_build
    test_container_functionality
    test_filesystem_extraction
    test_container_cleanup
    
    log "All safe tests completed successfully!"
}

# Main function
main() {
    echo "Debian Atomic Terminal Installer - Safe Test Suite"
    echo "================================================"
    echo ""
    echo "This test suite ONLY uses containers and never touches the host system."
    echo ""
    
    run_safe_tests
    
    echo ""
    echo "✅ All safe tests passed!"
    echo ""
    echo "The terminal installer is ready for use."
    echo ""
    echo "Next steps:"
    echo "1. Install genisoimage: sudo dnf install genisoimage"
    echo "2. Create ISO: just create-iso"
    echo "3. Test ISO: just test-iso"
    echo "4. Use in VM or on hardware"
    echo ""
    echo "Note: All testing is done safely in containers only."
}

# Run main function
main "$@" 