scripts/modules/prerequisites/strangeloop-cli/install-strangeloop-cli.ps1

# strangeloop Setup - strangeloop CLI Installation Module
# Version: 1.0.0

param(
    [string]$Version = "latest",
    [switch]${test-only},
    [switch]$Detailed,
    [switch]${what-if}
)

# Import shared modules
$SharedPath = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent | Join-Path -ChildPath "shared"
Import-Module "$SharedPath\write-functions.ps1" -Force -DisableNameChecking
Import-Module "$SharedPath\test-functions.ps1" -Force -DisableNameChecking
Import-Module "$SharedPath\path-functions.ps1" -Force -DisableNameChecking

function Test-strangeloopCLI {
    param(
        [switch]$Detailed
    )
    
    try {
        Write-Info "Testing strangeloop CLI installation..."
        
        # Check if strangeloop command is available
        if (-not (Test-Command "strangeloop")) {
            Write-Warning "strangeloop CLI command 'strangeloop' not found"
            return $false
        }
        
        # Get version using the correct command
        $version = strangeloop version 2>$null
        if (-not $version -or -not ($version -match "strangeloop")) {
            Write-Warning "Could not determine strangeloop CLI version"
            return $false
        }
        
        # Extract version number for display
        $versionNumber = ($version -split '\n' | Where-Object {$_ -match 'strangeloop \d'}) -replace '.*strangeloop ([0-9.]+).*','$1'
        Write-Success "strangeloop CLI is properly installed: $versionNumber"
        
        # Test basic functionality
        $helpOutput = strangeloop --help 2>$null
        if (-not $helpOutput) {
            Write-Warning "strangeloop CLI functionality test failed"
            return $false
        }
        
        Write-Success "strangeloop CLI functionality test passed"
        
        # Detailed testing if requested
        if ($Detailed) {
            Write-Info "Running detailed functionality tests..."
            
            # Test library access
            Write-Info "Testing library access..."
            try {
                $libraryOutput = strangeloop library loops 2>$null
                if ($libraryOutput) {
                    Write-Success "strangeloop library access test passed"
                    $loopCount = ($libraryOutput | Where-Object { $_ -match "^[a-z]" -and $_ -notmatch "INFO|loading|__" }).Count
                    Write-Info "Available loops: $loopCount"
                } else {
                    Write-Warning "strangeloop library access test failed"
                }
            } catch {
                Write-Warning "strangeloop library access test failed: $($_.Exception.Message)"
            }
            
            # Test version command output
            try {
                Write-Info "Version command output verification..."
                $versionOutput = strangeloop version 2>$null
                if ($versionOutput) {
                    Write-Info "Version command output: $($versionOutput -join '; ')"
                } else {
                    Write-Warning "Version command returned no output"
                }
            } catch {
                Write-Warning "Version command test failed: $($_.Exception.Message)"
            }
        }
        
        return $true
        
    } catch {
        Write-Warning "strangeloop CLI test failed: $($_.Exception.Message)"
        return $false
    }
}

function Install-strangeloopCLI {
    param(
        [string]$Version = "latest",
        [switch]${test-only},
        [switch]$Detailed,
        [switch]${what-if}
    )
    
    # If test-only mode, just test current installation
    if (${test-only}) {
        return Test-strangeloopCLI -Detailed:$Detailed
    }
    
    # If what-if mode, show what would be done
    if (${what-if}) {
        Write-Host "what if: Would test if strangeloop CLI is already installed" -ForegroundColor Yellow
        Write-Host "what if: Would check internet connection" -ForegroundColor Yellow
        Write-Host "what if: Would download and install strangeloop CLI version '$Version'" -ForegroundColor Yellow
        Write-Host "what if: Would verify strangeloop CLI installation and functionality" -ForegroundColor Yellow
        return $true
    }
    
    Write-Step "Installing strangeloop CLI..."
    
    try {
        # Check if already installed
        if (Test-Command "strangeloop") {
            $currentVersion = strangeloop version 2>$null
            if ($currentVersion -and $currentVersion -match "strangeloop") {
                $versionNumber = ($currentVersion -split '\n' | Where-Object {$_ -match 'strangeloop \d'}) -replace '.*strangeloop ([0-9.]+).*','$1'
                Write-Success "strangeloop CLI is already installed: $versionNumber"
                return $true
            }
        }
        
        # Check internet connection
        if (-not (Test-InternetConnection)) {
            Write-Error "Internet connection required for strangeloop CLI installation"
            return $false
        }
        
        Write-Info "strangeloop CLI installation would proceed here..."
        Write-Info "This implementation focuses on testing existing installations"
        Write-Warning "Full installation logic not implemented in this simplified version"
        
        return $false
        
    } catch {
        Write-Error "strangeloop CLI installation failed: $($_.Exception.Message)"
        return $false
    }
}

# Main execution
if ($MyInvocation.InvocationName -ne '.') {
    $result = Install-strangeloopCLI -Version $Version -test-only:${test-only} -Detailed:$Detailed -what-if:${what-if}
    
    if ($result) {
        if (${test-only}) {
            Write-Success "strangeloop CLI test completed successfully"
        } else {
            Write-Success "strangeloop CLI installation completed successfully"
        }
        return @{ Success = $true; Phase = "strangeloop CLI"; Message = "strangeloop CLI operation completed successfully" }
    } else {
        if (${test-only}) {
            Write-Error "strangeloop CLI test failed"
        } else {
            Write-Error "strangeloop CLI installation failed"
        }
        return @{ Success = $false; Phase = "strangeloop CLI"; Message = "strangeloop CLI operation failed" }
    }
}