#Requires -Version 7.4

function Format-AnalyzeSummary {
    <#
    .SYNOPSIS
        Formats analyze results in terraform-plan style.

    .DESCRIPTION
        Produces progressive disclosure output:
        - Default: Summary line + per-plugin group counts
        - Verbose: Full action breakdown per group

    .PARAMETER PluginResults
        Array of plugin analysis results from Get-PluginAnalysis.

    .PARAMETER Detailed
        If true, include detailed action breakdown.
    #>
    [CmdletBinding()]
    [OutputType([string[]])]
    param(
        [Parameter(Mandatory)]
        [AllowEmptyCollection()]
        [array]$PluginResults,

        [switch]$Detailed
    )

    $output = @()

    # Calculate totals
    $totalPlugins = $PluginResults.Count
    $totalGroups = ($PluginResults | Measure-Object -Property TotalGroups -Sum).Sum
    $executeGroups = ($PluginResults | Measure-Object -Property ExecuteGroups -Sum).Sum
    $skipGroups = ($PluginResults | Measure-Object -Property SkipGroups -Sum).Sum
    $changeActions = ($PluginResults | Measure-Object -Property ChangeActions -Sum).Sum
    $noOpActions = ($PluginResults | Measure-Object -Property NoOpActions -Sum).Sum
    $skippedActions = ($PluginResults | Measure-Object -Property SkippedActions -Sum).Sum
    $commandCount = ($PluginResults | Measure-Object -Property CommandCount -Sum).Sum

    # Header
    $output += "=== Analyze Summary ==="
    $output += ""

    # Summary line
    $output += "$executeGroups groups execute, $skipGroups skipped | $changeActions changes, $($noOpActions + $skippedActions) no-op"
    $output += ""

    # Rollback coverage
    $rollbackPlugins = @($PluginResults | Where-Object { $_.RollbackCapable })
    $nonRollbackPlugins = @($PluginResults | Where-Object { -not $_.RollbackCapable })

    if ($commandCount -gt 0 -or $nonRollbackPlugins.Count -gt 0) {
        $partialReason = @()
        if ($commandCount -gt 0) { $partialReason += "$commandCount commands not reversible" }
        if ($nonRollbackPlugins.Count -gt 0) { $partialReason += "$($nonRollbackPlugins.Count) plugins no rollback" }
        $output += "Rollback: partial ($($partialReason -join ', '))"
    }
    else {
        $output += "Rollback: full"
    }

    # Baseline notice
    $predictedId = Get-Date -Format 'yyyyMMdd_HHmmss'
    $output += "Seal will create baseline $predictedId"
    $output += ""

    # Per-plugin breakdown
    $output += "--- Plugins ---"

    foreach ($plugin in $PluginResults) {
        $rollbackStr = if ($plugin.RollbackCapable) { "rollback: yes" } else { "rollback: no" }
        $changeStr = "$($plugin.ChangeActions) changes"

        $output += "  $($plugin.Plugin): $changeStr ($rollbackStr)"

        if ($Detailed -and $plugin.Groups.Count -gt 0) {
            foreach ($group in $plugin.Groups) {
                if ($group.WillExecute) {
                    $groupChanges = @($group.Actions | Where-Object { $_.WillChange }).Count
                    $output += "    [+] $($group.Name): $groupChanges changes"

                    foreach ($action in $group.Actions) {
                        $symbol = if ($action.Skipped) {
                            [char]0x2298  # Circled division slash
                        }
                        elseif ($action.WillChange) {
                            [char]0x2192  # Right arrow
                        }
                        else {
                            [char]0x2713  # Check mark
                        }
                        $output += "      $symbol $($action.Type): $($action.Target)"
                    }
                }
                else {
                    $output += "    [-] $($group.Name): SKIPPED ($($group.SkipReason))"
                }
            }
        }
    }

    $output += ""

    # Legend (only in detailed mode)
    if ($Detailed) {
        $output += "Legend: $([char]0x2192) will change, $([char]0x2713) already correct, $([char]0x2298) condition skipped"
    }

    return $output
}
