Private/namespaces-functions.ps1

function Show-EmptyNamespaces {
    param(
        [int]$PageSize = 10, # Number of namespaces per page
        [switch]$Html        # If specified, return an HTML table
    )

    if (-not $Global:MakeReport -and -not $Html) { Clear-Host }
    Write-Host "`n[šŸ“‚ Empty Namespaces]" -ForegroundColor Cyan
    Write-Host -NoNewline "`nšŸ¤– Fetching Namespace Data..." -ForegroundColor Yellow

    # Fetch all namespaces
    $namespaces = kubectl get namespaces -o json | ConvertFrom-Json |
    Select-Object -ExpandProperty items |
    Select-Object -ExpandProperty metadata |
    Select-Object -ExpandProperty name

    # Fetch all pods and their namespaces
    $pods = kubectl get pods --all-namespaces -o json | ConvertFrom-Json |
    Select-Object -ExpandProperty items |
    Group-Object { $_.metadata.namespace }

    # Extract namespaces that have at least one pod
    $namespacesWithPods = $pods.Name

    # Get only namespaces that are completely empty
    $emptyNamespaces = $namespaces | Where-Object { $_ -notin $namespacesWithPods }

    $totalNamespaces = $emptyNamespaces.Count

    if ($totalNamespaces -eq 0) {
        Write-Host "`ršŸ¤– āœ… No empty namespaces found." -ForegroundColor Green

        if ($Global:MakeReport -and -not $Html) {
            Write-ToReport "`n[šŸ“‚ Empty Namespaces]`n"
            Write-ToReport "āœ… No empty namespaces found."
        }

        # If not in report mode or HTML mode, prompt to continue
        if (-not $Global:MakeReport -and -not $Html) {
            Read-Host "šŸ¤– Press Enter to return to the menu"
        }
        if ($Html) {
            return "<p><strong>āœ… No empty namespaces found.</strong></p>"
        }
        return
    }

    Write-Host "`ršŸ¤– āœ… Namespaces fetched. ($totalNamespaces empty namespaces detected)" -ForegroundColor Green

    # ----- HTML SWITCH -----
    if ($Html) {
        # Build an HTML table. Each row => one namespace
        # Convert the array into PSCustomObjects first
        $namespacesData = $emptyNamespaces | ForEach-Object {
            [PSCustomObject]@{
                "Namespace" = $_
            }
        }

        # Convert to HTML
        $htmlTable = $namespacesData |
        ConvertTo-Html -Fragment -Property "Namespace" |
        Out-String

        # Insert a note about total empty
        $htmlTable = "<p><strong>āš ļø Total Empty Namespaces:</strong> $totalNamespaces</p>" + $htmlTable

        return $htmlTable
    }
    # ----- END HTML SWITCH -----

    # ----- If in report mode, but no -Html switch, do original ascii printing -----
    if ($Global:MakeReport) {
        Write-ToReport "`n[šŸ“‚ Empty Namespaces]`n"
        Write-ToReport "āš ļø Total Empty Namespaces: $totalNamespaces"
        Write-ToReport "---------------------------------"
        foreach ($namespace in $emptyNamespaces) {
            Write-ToReport "$namespace"
        }
        return
    }

    # ----- Otherwise, do console pagination as before -----
    $currentPage = 0
    $totalPages = [math]::Ceiling($totalNamespaces / $PageSize)

    do {
        Clear-Host
        Write-Host "`n[šŸ“‚ Empty Namespaces - Page $($currentPage + 1) of $totalPages]" -ForegroundColor Cyan

        # Speech bubble
        $msg = @(
            "šŸ¤– Empty namespaces exist but contain no running pods.",
            "",
            "šŸ“Œ These may be unused namespaces that can be cleaned up.",
            "šŸ“Œ If needed, verify if they contain other resources (Secrets, PVCs).",
            "šŸ“Œ Deleting an empty namespace will remove all associated resources.",
            "",
            "āš ļø Total Empty Namespaces: $totalNamespaces"
        )
        if ($currentPage -eq 0) {
            Write-SpeechBubble -msg $msg -color "Cyan" -icon "šŸ¤–" -lastColor "Red" -delay 50 # first page only
        }

        # Display current page
        $startIndex = $currentPage * $PageSize
        $endIndex = [math]::Min($startIndex + $PageSize, $totalNamespaces)

        $tableData = @()
        for ($i = $startIndex; $i -lt $endIndex; $i++) {
            $namespace = $emptyNamespaces[$i]
            $tableData += [PSCustomObject]@{ "Namespace" = $namespace }
        }

        if ($tableData) {
            $tableData | Format-Table Namespace -AutoSize
        }

        # Pagination
        $newPage = Show-Pagination -currentPage $currentPage -totalPages $totalPages
        if ($newPage -eq -1) { break }
        
        $currentPage = $newPage
    } while ($true)
}