Public/Search/New-JIMPredefinedSearchCriteriaGroup.ps1

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

function New-JIMPredefinedSearchCriteriaGroup {
    <#
    .SYNOPSIS
        Creates a new criteria group on a Predefined Search.
 
    .DESCRIPTION
        Creates a criteria group on a Predefined Search, either top-level or nested under an existing
        group. The group type determines how criteria within it are evaluated:
        - All: All criteria must match (AND logic)
        - Any: At least one criterion must match (OR logic)
 
        Within a group, criteria and nested child groups are combined per the group's type; top-level
        groups are combined with OR. Use -ParentGroupId to nest a group, for example to express
        (A OR B) AND C.
 
    .PARAMETER PredefinedSearchId
        The ID of the Predefined Search.
 
    .PARAMETER ParentGroupId
        The ID of an existing group to nest this one in. A top-level group when omitted.
 
    .PARAMETER Type
        How the group combines what it holds: All (AND) or Any (OR).
 
    .PARAMETER Position
        The group's place among its siblings.
 
    .PARAMETER ChangeReason
        Why the change was made, recorded on the audit Activity and shown in the Predefined Search's Configuration
        Change History.
 
    .PARAMETER PassThru
        Returns the created group.
 
    .OUTPUTS
        With -PassThru, the created group, with a PredefinedSearchId property added naming its search; otherwise
        nothing.
 
    .EXAMPLE
        New-JIMPredefinedSearchCriteriaGroup -PredefinedSearchId 3 -Type All
 
        Creates a top-level criteria group with AND logic.
 
    .EXAMPLE
        New-JIMPredefinedSearchCriteriaGroup -PredefinedSearchId 3 -ParentGroupId 10 -Type Any -PassThru
 
        Creates a child group with OR logic nested under group 10.
 
    .LINK
        Get-JIMPredefinedSearchCriteriaGroup
        Set-JIMPredefinedSearchCriteriaGroup
        Remove-JIMPredefinedSearchCriteriaGroup
        New-JIMPredefinedSearchCriterion
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
    [OutputType('JIM.PredefinedSearchCriteriaGroupDto')]
    param(
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [Alias('Id')]
        [int]$PredefinedSearchId,

        [Parameter()]
        [int]$ParentGroupId,

        [Parameter()]
        [ValidateSet('All', 'Any')]
        [string]$Type = 'All',

        [Parameter()]
        [int]$Position = 0,

        [ValidateNotNullOrEmpty()]
        [string]$ChangeReason,

        [switch]$PassThru
    )

    process {
        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
        }

        $body = @{
            type = $Type
            position = $Position
        }
        if ($ChangeReason) {
            $body.changeReason = $ChangeReason
        }

        $endpoint = if ($PSBoundParameters.ContainsKey('ParentGroupId')) {
            "/api/v1/predefined-searches/$PredefinedSearchId/criteria-groups/$ParentGroupId/child-groups"
        }
        else {
            "/api/v1/predefined-searches/$PredefinedSearchId/criteria-groups"
        }

        $target = if ($PSBoundParameters.ContainsKey('ParentGroupId')) {
            "Predefined Search $PredefinedSearchId (under group $ParentGroupId)"
        }
        else {
            "Predefined Search $PredefinedSearchId"
        }

        if ($PSCmdlet.ShouldProcess($target, "Create Criteria Group ($Type)")) {
            Write-Verbose "Creating criteria group for Predefined Search $PredefinedSearchId"
            try {
                $result = Invoke-JIMApi -Endpoint $endpoint -Method 'POST' -Body $body
                Write-Verbose "Created criteria group ID: $($result.id)"
                if ($PassThru) {
                    $result | Add-Member -NotePropertyName 'PredefinedSearchId' -NotePropertyValue $PredefinedSearchId -PassThru -Force
                }
            }
            catch {
                Write-Error "Failed to create criteria group: $_"
            }
        }
    }
}