functions/public/Add-KlippyJobQueue.ps1

function Add-KlippyJobQueue {
    <#
    .SYNOPSIS
        Adds a print job to the queue.

    .DESCRIPTION
        Adds one or more G-code files to the print queue for sequential printing.

    .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 FileName
        The G-code file(s) to add to the queue.

    .EXAMPLE
        Add-KlippyJobQueue -FileName "benchy.gcode"
        Adds a file to the job queue.

    .EXAMPLE
        Add-KlippyJobQueue -FileName "part1.gcode", "part2.gcode", "part3.gcode"
        Adds multiple files to the queue.

    .EXAMPLE
        Get-KlippyGcodeFile -Path "batch" | Add-KlippyJobQueue
        Adds all files from a folder to the queue.

    .OUTPUTS
        None.
    #>

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

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

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

        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias('Path', 'Name')]
        [string[]]$FileName
    )

    begin {
        $filesToAdd = [System.Collections.Generic.List[string]]::new()
        $printer = $null
    }

    process {
        # Resolve printer
        $resolveParams = @{}
        if ($Id) { $resolveParams['Id'] = $Id }
        elseif ($PrinterName) { $resolveParams['PrinterName'] = $PrinterName }
        elseif ($InputObject) {
            if ($InputObject.PSObject.Properties['PrinterId']) {
                $resolveParams['Id'] = $InputObject.PrinterId
            }
            else {
                $resolveParams['InputObject'] = $InputObject
            }
        }

        if (-not $printer) {
            $printer = Resolve-KlippyPrinterTarget @resolveParams
        }

        foreach ($file in $FileName) {
            $filesToAdd.Add($file)
        }
    }

    end {
        if (-not $printer) {
            Write-Error "No printer specified."
            return
        }

        if ($filesToAdd.Count -eq 0) {
            Write-Warning "No files specified to add to queue."
            return
        }

        try {
            $action = "Add $($filesToAdd.Count) file(s) to job queue"
            if ($PSCmdlet.ShouldProcess($printer.PrinterName, $action)) {
                # Build the filenames parameter
                $filesJson = $filesToAdd | ConvertTo-Json -Compress
                if ($filesToAdd.Count -eq 1) {
                    $filesJson = "[$filesJson]"
                }
                $encodedFiles = [System.Uri]::EscapeDataString($filesJson)

                $endpoint = "server/job_queue/add?filenames=$encodedFiles"
                $null = Invoke-KlippyJsonRpc -Printer $printer -Method $endpoint

                Write-Host "Added $($filesToAdd.Count) file(s) to job queue on '$($printer.PrinterName)'." -ForegroundColor Green
                foreach ($file in $filesToAdd) {
                    Write-Verbose " Added: $file"
                }
            }
        }
        catch {
            Write-Error "Failed to add to job queue on '$($printer.PrinterName)': $_"
        }
    }
}