Public/Metaverse/Get-JIMMetaverseAttributeDeletionPreview.ps1

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

function Get-JIMMetaverseAttributeDeletionPreview {
    <#
    .SYNOPSIS
        Previews the impact of deleting a custom Metaverse Attribute.
 
    .DESCRIPTION
        Returns a non-destructive assessment of what deleting a custom Metaverse Attribute would
        entail: whether it is built-in, how many Metaverse Objects hold a stored value for it (a
        hard block on deletion), the per-Object-Type value breakdown, the configuration that reads
        it (Scoping Criteria and Expressions, also a hard block, in Readers), and the configuration
        references (bindings, and Attribute Flows and Object Matching Rules that map the attribute
        directly) that would be cascade-removed. Use this to inspect an attribute before calling
        Remove-JIMMetaverseAttribute.
 
    .PARAMETER Id
        The ID of the attribute to assess.
 
    .PARAMETER InputObject
        An attribute from the pipeline, for example from Get-JIMMetaverseAttribute.
 
    .OUTPUTS
        What deleting the attribute would do: whether stored values block it, how many Metaverse Objects of each
        Object Type hold one, and the configuration that would be removed with it.
 
    .EXAMPLE
        Get-JIMMetaverseAttributeDeletionPreview -Id 42
 
        Previews the impact of deleting attribute 42.
 
    .EXAMPLE
        Get-JIMMetaverseAttribute -Name "CostCentre" | Get-JIMMetaverseAttributeDeletionPreview
 
        Previews the impact of deleting the CostCentre attribute (from the pipeline).
 
    .LINK
        Remove-JIMMetaverseAttribute
        Get-JIMMetaverseAttribute
    #>

    [CmdletBinding(DefaultParameterSetName = 'ById')]
    [OutputType('JIM.AttributeDeletionImpact')]
    param(
        [Parameter(Mandatory, ParameterSetName = 'ById', ValueFromPipelineByPropertyName)]
        [int]$Id,

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

    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
        }

        $attrId = if ($InputObject) { $InputObject.id } else { $Id }

        try {
            Invoke-JIMApi -Endpoint "/api/v1/metaverse/attributes/$attrId/deletion-preview"
        }
        catch {
            Write-Error "Failed to preview deletion of Metaverse Attribute ${attrId}: $_"
        }
    }
}