Public/RunProfiles/Remove-JIMRunProfile.ps1

# Copyright (c) Tetron Limited. All rights reserved.
# Licensed under the Tetron Commercial License. See LICENSE file in the project root.

function Remove-JIMRunProfile {
    <#
    .SYNOPSIS
        Removes a Run Profile from JIM.
 
    .DESCRIPTION
        Permanently deletes a Run Profile from a Connected System.
 
    .PARAMETER ConnectedSystemId
        The Connected System the Run Profile belongs to.
 
    .PARAMETER ConnectedSystemName
        The name of the Connected System the Run Profile belongs to. Must be an exact match.
 
    .PARAMETER RunProfileId
        The Run Profile to delete.
 
    .PARAMETER InputObject
        The Run Profile to delete, as Get-JIMRunProfile returns it.
 
    .PARAMETER Force
        Suppresses the confirmation prompt.
 
    .PARAMETER PassThru
        Returns the deleted Run Profile.
 
    .OUTPUTS
        With -PassThru, the deleted Run Profile, as it was before the deletion; otherwise nothing.
 
    .EXAMPLE
        Remove-JIMRunProfile -ConnectedSystemId 1 -RunProfileId 1
 
        Removes the Run Profile (prompts for confirmation).
 
    .EXAMPLE
        Remove-JIMRunProfile -ConnectedSystemName 'Contoso AD' -RunProfileId 1 -Force
 
        Removes the Run Profile using the Connected System name.
 
    .EXAMPLE
        Remove-JIMRunProfile -ConnectedSystemId 1 -RunProfileId 1 -Force
 
        Removes the Run Profile without confirmation.
 
    .EXAMPLE
        Get-JIMRunProfile -ConnectedSystemId 1 | Where-Object { $_.name -like "Test*" } | Remove-JIMRunProfile -Force
 
        Removes all Run Profiles with names starting with "Test".
 
    .LINK
        Get-JIMRunProfile
        New-JIMRunProfile
        Set-JIMRunProfile
        Start-JIMRunProfile
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High', DefaultParameterSetName = 'ById')]
    [OutputType('JIM.RunProfileDto')]
    param(
        [Parameter(Mandatory, ParameterSetName = 'ById')]
        [int]$ConnectedSystemId,

        [Parameter(Mandatory, ParameterSetName = 'ByName')]
        [string]$ConnectedSystemName,

        [Parameter(Mandatory, ParameterSetName = 'ById')]
        [Parameter(Mandatory, ParameterSetName = 'ByName')]
        [int]$RunProfileId,

        [Parameter(Mandatory, ParameterSetName = 'ByInputObject', ValueFromPipeline)]
        [PSCustomObject]$InputObject,

        [switch]$Force,

        [switch]$PassThru
    )

    process {
        # Check connection first
        if (-not $script:JIMConnection) {
            Write-Error "You are not connected to JIM. Run Connect-JIM -Url <your JIM URL> to authenticate, then try again."
            return
        }

        # Resolve ConnectedSystemName to ConnectedSystemId if specified
        if ($PSBoundParameters.ContainsKey('ConnectedSystemName')) {
            $connectedSystem = Resolve-JIMConnectedSystem -Name $ConnectedSystemName
            $ConnectedSystemId = $connectedSystem.id
        }

        $csId = if ($InputObject) { $InputObject.connectedSystemId } else { $ConnectedSystemId }
        $profileId = if ($InputObject) { $InputObject.id } else { $RunProfileId }

        if (-not $csId) {
            Write-Error "ConnectedSystemId is required. Provide -ConnectedSystemId parameter or pipe an object with connectedSystemId property."
            return
        }

        # Get the profile first for confirmation message and PassThru
        $existing = $null
        if ($InputObject) {
            $existing = $InputObject
        }
        else {
            try {
                $profiles = Invoke-JIMApi -Endpoint "/api/v1/synchronisation/connected-systems/$csId/run-profiles"
                $existing = $profiles | Where-Object { $_.id -eq $profileId } | Select-Object -First 1
            }
            catch {
                Write-Error "Failed to retrieve Run Profile: $_"
                return
            }
        }

        if (-not $existing) {
            Write-Error "Run Profile not found: $profileId"
            return
        }

        $displayName = $existing.name ?? $profileId

        if (Confirm-JIMAction -Cmdlet $PSCmdlet -Target $displayName -Action "Delete Run Profile" -Force:$Force) {
            Write-Verbose "Deleting Run Profile: $profileId from Connected System $csId"

            try {
                Invoke-JIMApi -Endpoint "/api/v1/synchronisation/connected-systems/$csId/run-profiles/$profileId" -Method 'DELETE'

                Write-Verbose "Deleted Run Profile: $profileId"

                if ($PassThru) {
                    $existing
                }
            }
            catch {
                Write-Error "Failed to delete Run Profile: $_"
            }
        }
    }
}