#!/bin/bash
# Test script for Debian Atomic Terminal Installer using podman container
# This script tests the installation process in a controlled container environment

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}[TEST]${NC} $1"
}

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

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

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

# Test container run
test_container_run() {
    log "Testing container run..."
    
    # Run container in background
    podman run --rm -d --name test-tui-installer debian-atomic-tui-installer:latest
    
    # Wait a moment for container to start
    sleep 2
    
    # Check if container is running
    if podman ps | grep -q test-tui-installer; then
        log "✅ Container is running"
        
        # Test basic functionality
        if podman exec test-tui-installer which install-debian-atomic; then
            log "✅ Install script found"
        else
            warn "⚠️ Install script not found"
        fi
        
        if podman exec test-tui-installer which welcome; then
            log "✅ Welcome script found"
        else
            warn "⚠️ Welcome script not found"
        fi
        
        # Stop container
        podman stop test-tui-installer
        log "✅ Container stopped"
    else
        error "❌ Container failed to start"
    fi
}

# Test ISO creation
test_iso_creation() {
    log "Testing ISO creation..."
    
    if just create-iso; then
        log "✅ ISO created successfully"
        
        # Check if ISO file exists
        if [ -f build/debian-atomic-tui-installer.iso ]; then
            log "✅ ISO file exists"
            ls -lh build/debian-atomic-tui-installer.iso
        else
            error "❌ ISO file not found"
        fi
    else
        error "❌ ISO creation failed"
    fi
}

# Test installation script syntax
test_script_syntax() {
    log "Testing 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 configuration file
test_config_file() {
    log "Testing configuration file..."
    
    if [ -f config/installer.conf ]; then
        log "✅ Configuration file exists"
        
        # Check if file is readable
        if [ -r config/installer.conf ]; then
            log "✅ Configuration file is readable"
        else
            warn "⚠️ Configuration file is not readable"
        fi
    else
        error "❌ Configuration file not found"
    fi
}

# Test 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
}

# Run all tests
run_tests() {
    log "Starting container-based tests..."
    
    test_script_syntax
    test_config_file
    test_justfile
    test_container_build
    test_container_run
    test_iso_creation
    
    log "All container-based tests completed successfully!"
}

# Main function
main() {
    echo "Debian Atomic Terminal Installer - Container Test Suite"
    echo "====================================================="
    echo ""
    
    run_tests
    
    echo ""
    echo "✅ All container-based tests passed!"
    echo "The terminal installer is ready for use."
    echo ""
    echo "Next steps:"
    echo "1. Run 'just test-iso' to test the ISO in QEMU"
    echo "2. Run 'just create-test-vm' to create a test VM"
    echo "3. Use the ISO in a real VM or on hardware"
}

# Run main function
main "$@"