functions/public/Stop-KlippyEmergency.ps1

function Stop-KlippyEmergency {
    <#
    .SYNOPSIS
        Performs an emergency stop on a Klipper printer.

    .DESCRIPTION
        Immediately halts all printer operations by sending an emergency stop (M112).
        This will shut down the printer and require a firmware restart to recover.
        Use this only in emergency situations.

    .PARAMETER Id
        The unique identifier of the printer.

    .PARAMETER PrinterName
        The friendly name of the printer.

    .PARAMETER InputObject
        A printer object from pipeline input.

    .PARAMETER Force
        Skip confirmation prompt.

    .EXAMPLE
        Stop-KlippyEmergency
        Emergency stops the default printer (with confirmation).

    .EXAMPLE
        Stop-KlippyEmergency -PrinterName "voronv2" -Force
        Emergency stops without confirmation.

    .NOTES
        After an emergency stop, you must use Restart-KlippyFirmware to recover.

    .OUTPUTS
        None.
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
    param(
        [Parameter(ParameterSetName = 'ById')]
        [ValidateNotNullOrEmpty()]
        [string]$Id,

        [Parameter(ParameterSetName = 'ByName', Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]$PrinterName,

        [Parameter(ParameterSetName = 'ByObject', ValueFromPipeline = $true)]
        [PSCustomObject]$InputObject,

        [Parameter()]
        [switch]$Force
    )

    process {
        # Resolve printer
        $resolveParams = @{}
        switch ($PSCmdlet.ParameterSetName) {
            'ById' { $resolveParams['Id'] = $Id }
            'ByName' { $resolveParams['PrinterName'] = $PrinterName }
            'ByObject' { $resolveParams['InputObject'] = $InputObject }
        }

        $printer = Resolve-KlippyPrinterTarget @resolveParams

        $confirmMessage = "EMERGENCY STOP on '$($printer.PrinterName)'? This will halt all operations immediately."

        if ($Force -or $PSCmdlet.ShouldProcess($printer.PrinterName, "Emergency Stop")) {
            if (-not $Force) {
                $confirm = Read-Host "Type 'STOP' to confirm emergency stop"
                if ($confirm -ne 'STOP') {
                    Write-Warning "Emergency stop cancelled."
                    return
                }
            }

            try {
                Write-Warning "Sending emergency stop to '$($printer.PrinterName)'..."
                $null = Invoke-KlippyJsonRpc -Printer $printer -Method "printer/emergency_stop"
                Write-Host "Emergency stop sent. Printer is now in shutdown state." -ForegroundColor Red
                Write-Host "Use 'Restart-KlippyFirmware' to recover." -ForegroundColor Yellow
            }
            catch {
                Write-Error "Failed to send emergency stop to '$($printer.PrinterName)': $_"
            }
        }
    }
}