scripts/modules/environment/setup-environment.ps1

# strangeloop Setup - Environment Setup Module
# Version: 1.0.0

param(
    [hashtable]$Requirements,
    [switch]${what-if}
)

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

function Initialize-Environment {
    param(
        [hashtable]$Requirements,
        [switch]${what-if}
    )
    
    Write-Step "Setting up Development Environment..."
    
    if (${what-if}) {
        Write-Host "what if: Would analyze environment requirements" -ForegroundColor Yellow
        if ($Requirements -and $Requirements.RequiresWSL) {
            Write-Host "what if: Would set up WSL environment by calling setup-wsl-env.ps1" -ForegroundColor Yellow
            Write-Host "what if: Would configure Docker WSL integration" -ForegroundColor Yellow
        } else {
            Write-Host "what if: Would set up Windows environment by calling setup-windows-env.ps1" -ForegroundColor Yellow
        }
        return $true
    }
    
    try {
        if (-not $Requirements) {
            Write-Warning "No environment requirements specified, using Windows environment"
            $Requirements = @{ RequiresWindows = $true; RequiresWSL = $false; Platform = "Windows" }
        }
        
        $platform = $Requirements.Platform
        Write-Info "Setting up $platform environment..."
        
        # Ensure RequiresWSL property exists
        if (-not $Requirements.ContainsKey('RequiresWSL')) {
            $Requirements['RequiresWSL'] = $false
        }
        
        if ($Requirements.RequiresWSL) {
            # WSL Environment Setup
            Write-Progress "Setting up WSL environment..."
            if (${what-if}) {
                Write-Host "what if: Would execute WSL environment setup script" -ForegroundColor Yellow
                $wslResult = $true
            } else {
                $wslResult = & "$PSScriptRoot\wsl\setup-wsl-env.ps1" -what-if:${what-if}
            }
            if (-not $wslResult) {
                Write-Warning "WSL environment setup failed"
                return $false
            }
            Write-Success "WSL environment setup completed"
            
            # Configure Docker WSL Integration
            Write-Progress "Configuring Docker for WSL integration..."
            if (${what-if}) {
                Write-Host "what if: Would configure Docker WSL integration" -ForegroundColor Yellow
                $dockerWSLResult = @{ Success = $true }
            } else {
                $dockerWSLResult = & "$PSScriptRoot\configure-docker-wsl-integration.ps1" -RequiresWSL -what-if:${what-if}
            }
            if ($dockerWSLResult -and $dockerWSLResult.Success) {
                Write-Success "Docker WSL integration configured"
            } else {
                Write-Warning "Docker WSL integration configuration failed - may need manual setup"
                Write-Info "Manual setup instructions:"
                Write-Info " 1. Open Docker Desktop"
                Write-Info " 2. Go to Settings -> General -> 'Use the WSL 2 based engine'"
                Write-Info " 3. Go to Settings -> Resources -> WSL integration -> 'Enable integration with my default WSL distro'"
            }
        } else {
            # Windows Environment Setup
            Write-Progress "Setting up Windows environment..."
            if (${what-if}) {
                Write-Host "what if: Would execute Windows environment setup script" -ForegroundColor Yellow
                $winResult = $true
            } else {
                $winResult = & "$PSScriptRoot\windows\setup-windows-env.ps1" -what-if:${what-if}
            }
            if (-not $winResult) {
                Write-Warning "Windows environment setup failed"
                return $false
            }
            Write-Success "Windows environment setup completed"
        }
        
        return $true
        
    } catch {
        Write-Error "Environment setup failed: $($_.Exception.Message)"
        return $false
    }
}

# Main execution
if ($MyInvocation.InvocationName -ne '.') {
    $result = Initialize-Environment -Requirements $Requirements -what-if:${what-if}
    
    if ($result) {
        Write-Success "Environment setup completed successfully"
        return @{ Success = $true; Phase = "Environment"; Message = "Environment setup completed successfully" }
    } else {
        Write-Error "Environment setup failed"
        return @{ Success = $false; Phase = "Environment"; Message = "Environment setup failed" }
    }
}

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