scripts/modules/authentication/setup-authentication.ps1

# strangeloop Setup - Authentication Setup Module
# Version: 1.0.0


param(
    [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-Authentication {
    param(
        [switch]${what-if}
    )
    
    Write-Step "Setting up Azure Authentication..."
    
    if (${what-if}) {
        Write-Host "what if: Would check if Azure CLI is installed" -ForegroundColor Yellow
        Write-Host "what if: Would check current Azure login status with 'az account show'" -ForegroundColor Yellow
        Write-Host "what if: Would perform Azure login with 'az login' if not already logged in" -ForegroundColor Yellow
        Write-Host "what if: Would verify login and display account information" -ForegroundColor Yellow
        return $true
    }
    
    try {
        # Check if Azure CLI is available
        if (-not (Test-Command "az")) {
            Write-Error "Azure CLI is not installed. Please install Azure CLI first."
            return $false
        }
        
        # Check current login status
        Write-Progress "Checking Azure login status..."
        
        try {
            $account = az account show --output json 2>$null | ConvertFrom-Json
            if ($account) {
                Write-Success "Already logged in to Azure as: $($account.user.name)"
                Write-Info "Current subscription: $($account.name)"
                return $true
            }
        } catch {
            Write-Info "Not currently logged in to Azure"
        }
        
        # Interactive Azure login
        Write-Progress "Initiating Azure login..."
        
        try {
            az login --output table
            
            # Verify login
            $account = az account show --output json 2>$null | ConvertFrom-Json
            if ($account) {
                Write-Success "Azure login successful"
                Write-Info "Logged in as: $($account.user.name)"
                Write-Info "Current subscription: $($account.name)"
                return $true
            } else {
                Write-Error "Azure login verification failed"
                return $false
            }
        } catch {
            Write-Error "Azure login failed: $($_.Exception.Message)"
            return $false
        }
        
    } catch {
        Write-Error "Authentication setup failed: $($_.Exception.Message)"
        return $false
    }
}

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

# Export functions for module usage
# Note: Functions are available when this file is dot-sourced