scripts/modules/prerequisites/setup-prerequisites.ps1

# strangeloop Setup - Prerequisites Installation Orchestrator
# Version: 1.0.0


param(
    [switch]${test-only},
    [switch]${wsl-mode},
    [switch]$RequiresWSL,
    [switch]${what-if}
)

# Import shared modules
$SharedPath = Split-Path $PSScriptRoot -Parent | Join-Path -ChildPath "shared"
. "$SharedPath\write-functions.ps1"
. "$SharedPath\test-functions.ps1"

function Install-Prerequisites {
    param(
        [switch]${test-only},
        [switch]${wsl-mode},
        [switch]$RequiresWSL,
        [switch]${what-if}
    )
    
    Write-Step "Setting up Prerequisites..."
    
    if (${what-if}) {
        Write-Host "what if: Would process the following prerequisite tools:" -ForegroundColor Yellow
        Write-Host "what if: - Git (Required)" -ForegroundColor Yellow
        Write-Host "what if: - Azure CLI (Required)" -ForegroundColor Yellow
        Write-Host "what if: - Python (Required)" -ForegroundColor Yellow
        Write-Host "what if: - Poetry (Required)" -ForegroundColor Yellow
        Write-Host "what if: - Docker (Optional)" -ForegroundColor Yellow
        Write-Host "what if: - Git LFS (Optional)" -ForegroundColor Yellow
        Write-Host "what if: - strangeloop CLI (Required)" -ForegroundColor Yellow
        Write-Host "what if: Would test each tool and install if missing" -ForegroundColor Yellow
        return $true
    }
    
    $results = @{}
    $overallSuccess = $true
    
    try {
        # Define prerequisite tools in order of installation
        $prerequisites = @(
            @{
                Name = "Git"
                InstallScript = "git\install-git.ps1"
                Required = $true
            },
            @{
                Name = "Azure CLI"
                InstallScript = "azure-cli\install-azure-cli.ps1"
                Required = $true
            },
            @{
                Name = "Python"
                InstallScript = "python\install-python.ps1"
                Required = $true
            },
            @{
                Name = "Poetry"
                InstallScript = "poetry\install-poetry.ps1"
                Required = $true
            },
            @{
                Name = "Docker"
                InstallScript = "docker\install-docker.ps1"
                Required = $false
            },
            @{
                Name = "Git LFS"
                InstallScript = "git-lfs\install-git-lfs.ps1"
                Required = $false
            },
            @{
                Name = "strangeloop CLI"
                InstallScript = "strangeloop-cli\install-strangeloop-cli.ps1"
                Required = $true
            }
        )
        
        Write-Info "Processing $($prerequisites.Count) prerequisite tools..."
        
        foreach ($prereq in $prerequisites) {
            Write-Host ""
            Write-Progress "Processing $($prereq.Name)..."
            
            $toolSuccess = $false
            
            # All tools now support test-only parameter, so use consistent approach
            $installPath = Join-Path $PSScriptRoot $prereq.InstallScript
            if (Test-Path $installPath) {
                try {
                    Write-Info "Testing $($prereq.Name) installation..."
                    
                    # Prepare test parameters
                    $testParams = @{
                        'test-only' = $true
                    }
                    
                    # Add what-if parameter if supported
                    if (${what-if}) {
                        $testParams['what-if'] = $true
                    }
                    
                    # Add WSLMode for tools that support it
                    if ($prereq.Name -in @("Python", "Poetry") -and ${wsl-mode}) {
                        $testParams['WSLMode'] = $true
                    }
                    
                    # Add RequiresWSL for Docker
                    if ($prereq.Name -eq "Docker" -and $RequiresWSL) {
                        $testParams['RequiresWSL'] = $true
                    }
                    
                    $testResult = & $installPath @testParams
                    if ($testResult -eq $true -or $LASTEXITCODE -eq 0) {
                        Write-Success "$($prereq.Name) is already installed and working"
                        $toolSuccess = $true
                    }
                } catch {
                    Write-Warning "$($prereq.Name) test failed: $($_.Exception.Message)"
                }
            } else {
                Write-Warning "Installation script not found for $($prereq.Name): $installPath"
            }
            
            # Install if not found
            if (-not $toolSuccess -and -not ${test-only}) {
                $installPath = Join-Path $PSScriptRoot $prereq.InstallScript
                if (Test-Path $installPath) {
                    try {
                        Write-Info "Installing $($prereq.Name)..."
                        
                        # Prepare installation parameters
                        $installParams = @{}
                        
                        # Add what-if parameter if supported
                        if (${what-if}) {
                            $installParams['what-if'] = $true
                        }
                        
                        # Add AutoStart for Docker to automatically start Docker Desktop
                        if ($prereq.Name -eq "Docker") {
                            $installParams['AutoStart'] = $true
                            if ($RequiresWSL) {
                                $installParams['RequiresWSL'] = $true
                            }
                        }
                        
                        # Add WSLMode for tools that support it
                        if ($prereq.Name -in @("Python", "Poetry") -and ${wsl-mode}) {
                            $installParams['WSLMode'] = $true
                        }
                        
                        $installResult = & $installPath @installParams
                        if ($installResult) {
                            Write-Success "$($prereq.Name) installed successfully"
                            $toolSuccess = $true
                        } else {
                            Write-Warning "$($prereq.Name) installation returned false"
                        }
                    } catch {
                        Write-Warning "$($prereq.Name) installation failed: $($_.Exception.Message)"
                    }
                } else {
                    Write-Warning "Installation script not found for $($prereq.Name): $installPath"
                }
            }
            
            # Record result
            $results[$prereq.Name] = $toolSuccess
            
            # Check if required tool failed
            if ($prereq.Required -and -not $toolSuccess) {
                Write-Error "$($prereq.Name) is required but installation failed"
                $overallSuccess = $false
            } elseif (-not $prereq.Required -and -not $toolSuccess) {
                Write-Warning "$($prereq.Name) installation failed (optional tool)"
            }
        }
        
        # Summary
        Write-Host ""
        Write-Step "Prerequisites Installation Summary"
        
        foreach ($result in $results.GetEnumerator()) {
            $status = if ($result.Value) { "✓ Success" } else { "✗ Failed" }
            $color = if ($result.Value) { "Green" } else { "Red" }
            Write-Host " $($result.Key): " -NoNewline
            Write-Host $status -ForegroundColor $color
        }
        
        if ($overallSuccess) {
            Write-Success "All required prerequisites are ready"
        } else {
            Write-Error "Some required prerequisites failed to install"
        }
        
        return $overallSuccess
        
    } catch {
        Write-Error "Prerequisites setup failed: $($_.Exception.Message)"
        return $false
    }
}

# Main execution
if ($MyInvocation.InvocationName -ne '.') {
    $result = Install-Prerequisites -test-only:${test-only} -WSLMode:${wsl-mode} -RequiresWSL:$RequiresWSL -what-if:${what-if}
    
    if ($result) {
        Write-Success "Prerequisites setup completed successfully"
        return @{ Success = $true; Phase = "Prerequisites"; Message = "Prerequisites setup completed successfully" }
    } else {
        Write-Error "Prerequisites setup failed"
        return @{ Success = $false; Phase = "Prerequisites"; Message = "Prerequisites setup failed" }
    }
}

# Note: Functions are available when this file is dot-sourced