#!/bin/bash

# Test script for basic particle-os functionality
# Focuses on core features without complex CI/CD or apt-cacher-ng

set -euo pipefail

echo "🧪 Testing basic particle-os functionality"
echo "=========================================="

# Configuration
RECIPE_DIR="recipes"
BUILD_DIR="bib"

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

# Helper functions
log_info() {
    echo -e "${BLUE}ℹ️  $1${NC}"
}

log_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

log_warning() {
    echo -e "${YELLOW}⚠️  $1${NC}"
}

log_error() {
    echo -e "${RED}❌ $1${NC}"
}

# Test 1: Check if particle-os is built
test_particle_os_build() {
    log_info "Testing particle-os build..."
    
    if [ ! -f "$BUILD_DIR/particle-os" ]; then
        log_warning "particle-os not built, building now..."
        cd "$BUILD_DIR"
        if go build -o particle-os cmd/particle_os/main.go; then
            log_success "particle-os built successfully"
        else
            log_error "Failed to build particle-os"
            return 1
        fi
        cd ..
    else
        log_success "particle-os already built"
    fi
}

# Test 2: Test basic CLI functionality
test_cli_basic() {
    log_info "Testing basic CLI functionality..."
    
    cd "$BUILD_DIR"
    
    # Test help
    if ./particle-os --help > /dev/null 2>&1; then
        log_success "Help command works"
    else
        log_error "Help command failed"
        return 1
    fi
    
    # Test version
    if ./particle-os version > /dev/null 2>&1; then
        log_success "Version command works"
    else
        log_error "Version command failed"
        return 1
    fi
    
    # Test list
    if ./particle-os list > /dev/null 2>&1; then
        log_success "List command works"
    else
        log_error "List command failed"
        return 1
    fi
    
    cd ..
}

# Test 3: Test recipe validation
test_recipe_validation() {
    log_info "Testing recipe validation..."
    
    local recipes=("minimal-test" "simple-server" "debian-test")
    local valid_count=0
    
    for recipe in "${recipes[@]}"; do
        if [ -f "$RECIPE_DIR/$recipe.yml" ]; then
            if "$BUILD_DIR/particle-os" validate "$RECIPE_DIR/$recipe.yml" > /dev/null 2>&1; then
                log_success "$recipe.yml is valid"
                ((valid_count++))
            else
                log_error "$recipe.yml validation failed"
            fi
        else
            log_warning "$recipe.yml not found"
        fi
    done
    
    log_info "Valid recipes: $valid_count/${#recipes[@]}"
}

# Test 4: Test container inspection
test_container_inspection() {
    log_info "Testing container inspection..."
    
    if "$BUILD_DIR/particle-os" container debian:trixie-slim > /dev/null 2>&1; then
        log_success "Container inspection works"
    else
        log_warning "Container inspection failed (may need container runtime)"
    fi
}

# Test 5: Test minimal recipe build (dry run if possible)
test_minimal_build() {
    log_info "Testing minimal recipe build..."
    
    if [ -f "$RECIPE_DIR/minimal-test.yml" ]; then
        log_info "Attempting to build minimal-test recipe..."
        
        # Check if we can run with sudo (CI environment)
        if [ "$EUID" -eq 0 ]; then
            log_info "Running as root, attempting actual build..."
            cd "$BUILD_DIR"
            if ./particle-os build --verbose ../recipes/minimal-test.yml; then
                log_success "Minimal recipe build completed"
            else
                log_warning "Minimal recipe build failed (expected during development)"
            fi
            cd ..
        else
            log_info "Not running as root, testing recipe validation only..."
            if "$BUILD_DIR/particle-os" validate "$RECIPE_DIR/minimal-test.yml" > /dev/null 2>&1; then
                log_success "Minimal recipe validation passed"
            else
                log_error "Minimal recipe validation failed"
            fi
        fi
    else
        log_warning "minimal-test.yml not found, skipping build test"
    fi
}

# Test 6: Test environment variable handling (apt-cacher-ng optional)
test_environment_variables() {
    log_info "Testing environment variable handling..."
    
    # Test APT_CACHER_NG_URL environment variable (optional)
    export APT_CACHER_NG_URL="http://test-cache:3142"
    
    if [ "$APT_CACHER_NG_URL" = "http://test-cache:3142" ]; then
        log_success "Environment variable APT_CACHER_NG_URL set correctly"
    else
        log_error "Failed to set APT_CACHER_NG_URL environment variable"
    fi
    
    # Reset to empty (apt-cacher-ng is optional)
    unset APT_CACHER_NG_URL
    log_info "apt-cacher-ng is optional - system works without it"
}

# Main test execution
main() {
    echo
    log_info "Starting basic functionality tests..."
    echo
    
    # Run all tests
    test_particle_os_build
    echo
    
    test_cli_basic
    echo
    
    test_recipe_validation
    echo
    
    test_container_inspection
    echo
    
    test_minimal_build
    echo
    
    test_environment_variables
    echo
    
    # Summary
    echo "=========================================="
    log_success "Basic functionality tests completed!"
    echo
    log_info "Next steps:"
    echo "1. Test basic CLI functionality: ./bib/particle-os --help"
    echo "2. Validate recipes: ./bib/particle-os validate recipes/minimal-test.yml"
    echo "3. Test container inspection: ./bib/particle-os container debian:trixie-slim"
    echo "4. When ready, test building: sudo ./bib/particle-os build recipes/minimal-test.yml"
    echo
    log_info "Focus on getting core functionality working before adding complex features!"
}

# Run main function
main "$@"
