functions/Sync-ALZPolicyFromLibrary.ps1

function Sync-ALZPolicyFromLibrary {
Param(
    [Parameter(Mandatory = $true)]
    [string] $DefinitionsRootFolder,

    [ValidateSet("ALZ", "AMBA", "FSI", "SLZ", "MLZ")]
    [string] $Type = "ALZ",

    [Parameter(Mandatory = $true)]
    [string] $PacEnvironmentSelector,

    [string] $LibraryPath,

    [string] $Tag,

    [switch] $CreateGuardrailAssignments,

    [switch] $EnableOverrides,

    [switch] $SyncAssignmentsOnly,

    [switch] $SyncAMBAExtendedPolicies,

    [string] $ParameterFile

)

# Dot Source Helper Scripts

if (-not [string]::IsNullOrWhiteSpace($ParameterFile) -and $Type -ne "AMBA") {
    throw "-ParameterFile is only supported when -Type is AMBA."
}

# MLZ is sourced from Azure/missionlz which has no usable release tag, so the tag validation - which
# calls the Azure Landing Zones Library API - is only applied to the library backed types.
if ($Tag -and $Type -ne "MLZ") {
    if ("refs/tags/$Tag" -notin (Invoke-RestMethod -Uri 'https://api.github.com/repos/Azure/Azure-Landing-Zones-Library/git/refs/tags/').ref) {
        throw "Tag must be a valid tag."
    }
}

# Resolves the final archetype name after the mid-pipeline renames that are applied while
# building the archetype array. Used in both the override-population path and the archetype-build
# path so the storage and lookup keys for assignment name overrides can never drift apart.
function Get-ALZFinalArchetypeName {
    param(
        [Parameter(Mandatory = $true)]
        [AllowEmptyString()]
        [string] $ArchetypeName,

        [Parameter(Mandatory = $true)]
        [string] $Type
    )

    switch ($ArchetypeName) {
        "landingzones" { return $Type -eq "AMBA" ? "amba_landing_zones" : "landingzones" }
        "alz" { return $Type -eq "AMBA" ? "amba_root" : "root" }
        "sovereign_root" { return "slz" }
        default { return $Type -eq "AMBA" ? "amba_$ArchetypeName" : $ArchetypeName }
    }
}

# Resolves the set of archetype names a `based_on` reference may match. Because archetypes are renamed
# mid-pipeline (e.g. the library "root" archetype is referenced by users as "alz", and "landingzones"
# maps to the library "landing_zones"), a `based_on` value supplied with the user-facing name must also
# match the library / final names. Returns every candidate name so the lookup cannot miss the parent
# archetype just because the user used a different but equivalent name.
function Get-ALZBasedOnMatchNames {
    param(
        [Parameter(Mandatory = $true)]
        [AllowEmptyString()]
        [string] $BasedOn,

        [Parameter(Mandatory = $true)]
        [string] $Type
    )

    $names = [System.Collections.Generic.List[string]]::new()
    $addName = {
        param([string] $Name)
        if (-not [string]::IsNullOrWhiteSpace($Name) -and -not $names.Contains($Name)) {
            $names.Add($Name)
        }
    }

    & $addName $BasedOn
    & $addName (Get-ALZFinalArchetypeName -ArchetypeName $BasedOn -Type $Type)
    switch ($BasedOn) {
        "alz" { & $addName "root" }
        "landingzones" { & $addName "landing_zones" }
    }

    return $names.ToArray()
}

function ConvertTo-ALZPrivateDnsZoneRegionCode {
    param(
        [AllowEmptyString()]
        [string] $Region
    )

    if ([string]::IsNullOrWhiteSpace($Region)) {
        return ""
    }

    $normalizedRegion = $Region.Trim().ToLowerInvariant()
    $regionCodeMap = @{
        'westeurope' = 'we'
        'northeurope' = 'ne'
        'eastus' = 'eus'
        'eastus2' = 'eus2'
        'westus' = 'wus'
        'westus2' = 'wus2'
        'westus3' = 'wus3'
        'centralus' = 'cus'
        'southcentralus' = 'scus'
        'uksouth' = 'uks'
        'ukwest' = 'ukw'
        'francecentral' = 'frc'
        'francesouth' = 'frs'
        'germanywestcentral' = 'gwc'
        'germanynorth' = 'gn'
        'swedencentral' = 'sdc'
        'swedensouth' = 'sds'
        'australiaeast' = 'aue'
        'australiasoutheast' = 'ase'
        'australiacentral' = 'auc'
        'japaneast' = 'jpe'
        'japanwest' = 'jpw'
        'koreacentral' = 'krc'
        'koreasouth' = 'krs'
        'canadacentral' = 'cac'
        'canadaeast' = 'cae'
        'brazilsouth' = 'brs'
        'brazilsoutheast' = 'brse'
    }

    if ($regionCodeMap.ContainsKey($normalizedRegion)) {
        return $regionCodeMap[$normalizedRegion]
    }

    if ($normalizedRegion -in $regionCodeMap.Values) {
        return $normalizedRegion
    }

    Write-ModernStatus -Message "Unknown Azure Backup region or geo-code '$Region'; using '$normalizedRegion' unchanged. Verify private_dns_zone_region against https://learn.microsoft.com/azure/backup/scripts/geo-code-list before deploying." -Status "warning" -Indent 2
    return $normalizedRegion
}

function Test-ALZParameterValueEqual {
    param(
        [AllowNull()]
        [object] $Left,

        [AllowNull()]
        [object] $Right
    )

    if ($null -eq $Left -or $null -eq $Right) {
        return $null -eq $Left -and $null -eq $Right
    }

    if ($Left -is [pscustomobject] -or $Right -is [pscustomobject]) {
        if ($Left -isnot [pscustomobject] -or $Right -isnot [pscustomobject]) {
            return $false
        }

        $leftProperties = @($Left.PSObject.Properties)
        $rightProperties = @($Right.PSObject.Properties)
        if ($leftProperties.Count -ne $rightProperties.Count) {
            return $false
        }

        foreach ($leftProperty in $leftProperties) {
            $rightProperty = $rightProperties | Where-Object { $_.Name -ceq $leftProperty.Name } | Select-Object -First 1
            if ($null -eq $rightProperty -or -not (Test-ALZParameterValueEqual -Left $leftProperty.Value -Right $rightProperty.Value)) {
                return $false
            }
        }
        return $true
    }

    if ($Left -is [System.Collections.IDictionary] -or $Right -is [System.Collections.IDictionary]) {
        if ($Left -isnot [System.Collections.IDictionary] -or $Right -isnot [System.Collections.IDictionary] -or $Left.Count -ne $Right.Count) {
            return $false
        }

        foreach ($leftKey in $Left.Keys) {
            $rightKey = @($Right.Keys) | Where-Object { "$_" -ceq "$leftKey" } | Select-Object -First 1
            if ($null -eq $rightKey -or -not (Test-ALZParameterValueEqual -Left $Left[$leftKey] -Right $Right[$rightKey])) {
                return $false
            }
        }
        return $true
    }

    if (($Left -is [System.Collections.IEnumerable] -and $Left -isnot [string]) -or
        ($Right -is [System.Collections.IEnumerable] -and $Right -isnot [string])) {
        if (($Left -isnot [System.Collections.IEnumerable] -or $Left -is [string]) -or
            ($Right -isnot [System.Collections.IEnumerable] -or $Right -is [string])) {
            return $false
        }

        $leftItems = @($Left)
        $rightItems = @($Right)
        if ($leftItems.Count -ne $rightItems.Count) {
            return $false
        }

        for ($index = 0; $index -lt $leftItems.Count; $index++) {
            if (-not (Test-ALZParameterValueEqual -Left $leftItems[$index] -Right $rightItems[$index])) {
                return $false
            }
        }
        return $true
    }

    return [object]::Equals($Left, $Right)
}

function ConvertTo-AMBAExtendedPolicySelector {
    param(
        [object] $Selector,
        [string] $Path
    )

    if ($Selector -isnot [pscustomobject]) {
        throw "$Path must be a selector object or an AND/OR group."
    }
    $keys = @($Selector.PSObject.Properties.Name)
    if ($keys.Count -eq 2 -and "type" -cin $keys -and "pattern" -cin $keys) {
        if ($Selector.type -cnotin @("folder", "file") -or
            $Selector.pattern -isnot [string] -or $Selector.pattern.Length -eq 0) {
            throw "$Path requires type folder or file and a non-empty regex pattern."
        }
        try {
            $regex = [regex]::new($Selector.pattern, [System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor
                [System.Text.RegularExpressions.RegexOptions]::CultureInvariant, [timespan]::FromSeconds(1))
        }
        catch {
            throw "Invalid regex in $Path.pattern ('$($Selector.pattern)'): $($_.Exception.Message)"
        }
        return @{ Type = $Selector.type; Regex = $regex }
    }
    if ($keys.Count -ne 1 -or $keys[0] -cnotin @("AND", "OR") -or $Selector.($keys[0]) -isnot [array]) {
        throw "$Path must contain only type/pattern or a single AND/OR array."
    }
    $operator = $keys[0]
    $children = @(
        $index = 0
        foreach ($child in $Selector.$operator) {
            ConvertTo-AMBAExtendedPolicySelector -Selector $child -Path "$Path.$operator[$index]"
            $index++
        }
    )
    return @{ Operator = $operator; Children = $children }
}

function Test-AMBAExtendedPolicySelector {
    param(
        [string] $Folder,
        [string] $FileName,
        [hashtable] $Selector
    )

    if ($Selector.ContainsKey("Regex")) {
        $value = $Selector.Type -eq "folder" ? $Folder : $FileName
        return $Selector.Regex.IsMatch($value)
    }
    foreach ($child in $Selector.Children) {
        $matchesChild = Test-AMBAExtendedPolicySelector -Folder $Folder -FileName $FileName -Selector $child
        if ($Selector.Operator -eq "AND" -and -not $matchesChild) {
            return $false
        }
        if ($Selector.Operator -eq "OR" -and $matchesChild) {
            return $true
        }
    }
    return $Selector.Operator -eq "AND"
}

function Get-AMBAExtendedOutputPath {
    param([string] $Root, [string] $RelativePath)

    $parts = $RelativePath.Split('/')
    if ($parts.Count -ne 3 -or $parts[-1] -notmatch '\.json$' -or
        @($parts | Where-Object { $_ -in @("", ".", "..") -or $_ -match '[\\:*?"<>|\x00-\x1f]|[. ]$' }).Count -gt 0) {
        throw "Invalid AMBA extended output path '$RelativePath'."
    }
    $path = $Root
    foreach ($part in $parts) {
        if ((Test-Path -LiteralPath $path) -and
            ((Get-Item -LiteralPath $path -Force -ErrorAction Stop).Attributes -band [IO.FileAttributes]::ReparsePoint)) {
            throw "AMBA extended output must not traverse a symbolic link or junction: '$path'."
        }
        $path = Join-Path $path $part
    }
    if ((Test-Path -LiteralPath $path) -and
        ((Get-Item -LiteralPath $path -Force -ErrorAction Stop).Attributes -band [IO.FileAttributes]::ReparsePoint)) {
        throw "AMBA extended output must not be a symbolic link: '$path'."
    }
    return $path
}

# Latest tag values
if ($Tag -eq "") {
    switch ($Type) {
        'ALZ' {
            $Tag = "platform/alz/2026.10.0"
        }
        'FSI' {
            $Tag = "platform/fsi/2025.03.0"
        }
        'AMBA' {
            $Tag = "platform/amba/2026.06.2"
        }
        'SLZ' {
            $Tag = "platform/slz/2026.10.0"
        }
    }
}

if ($Type -eq "MLZ") {
    Write-ModernHeader -Title "Syncing Policies From Library" -Subtitle "Type: MLZ (Azure/missionlz)"
}
else {
    Write-ModernHeader -Title "Syncing Policies From Library" -Subtitle "Type: $Type, Tag: $Tag"
}

$ownedTempPaths = [System.Collections.Generic.List[string]]::new()
try {
if ($LibraryPath -eq "" -and $Type -ne "MLZ") {
    $LibraryPath = Join-Path -Path ([IO.Path]::GetTempPath()) -ChildPath "epac-alz-$([guid]::NewGuid())"
    $ownedTempPaths.Add($LibraryPath)
    Write-ModernStatus -Message "Cloning Azure Landing Zones Library repository..." -Status "processing" -Indent 2
    git clone --config advice.detachedHead=false --depth 1 --branch $Tag https://github.com/Azure/Azure-Landing-Zones-Library.git $LibraryPath
    if ($LASTEXITCODE -eq 0) {
        Write-ModernStatus -Message "Repository cloned successfully" -Status "success" -Indent 4
    }
    else {
        Write-ModernStatus -Message "Failed to clone repository" -Status "error" -Indent 4
        throw "Failed to clone Azure Landing Zones Library repository."
    }
}

if ($Type -eq "AMBA" -and $SyncAMBAExtendedPolicies) {
    $AMBALibraryPath = Join-Path -Path ([IO.Path]::GetTempPath()) -ChildPath "epac-amba-$([guid]::NewGuid())"
    $ownedTempPaths.Add($AMBALibraryPath)
    Write-ModernStatus -Message "Cloning Azure Monitor Baseline Alerts repository for AMBA extended policies..." -Status "processing" -Indent 2
    # Avoid checking out the deeply nested documentation tree, which can exceed Windows path limits.
    git clone --config advice.detachedHead=false --config core.longpaths=true --depth 1 --sparse https://github.com/Azure/azure-monitor-baseline-alerts.git $AMBALibraryPath
    if ($LASTEXITCODE -eq 0) {
        git -C $AMBALibraryPath sparse-checkout set services
        if ($LASTEXITCODE -ne 0) {
            throw "Failed to check out AMBA extended policy services."
        }
        Write-ModernStatus -Message "AMBA extended policies repository cloned successfully" -Status "success" -Indent 4
    }
    else {
        Write-ModernStatus -Message "Failed to clone AMBA extended policies repository" -Status "error" -Indent 4
        throw "Failed to clone AMBA extended policies repository."
    }
}

if ($DefinitionsRootFolder -eq "") {
    if ($null -eq $env:PAC_DEFINITIONS_FOLDER) {
        if ($ModuleRoot) {
            $DefinitionsRootFolder = "./Definitions"
        }
        else {
            $DefinitionsRootFolder = "$PSScriptRoot/../../Definitions"
        }
    }
    else {
        $DefinitionsRootFolder = $env:PAC_DEFINITIONS_FOLDER
    }
}

# Ensure the output directory exists
$structureDirectory = "$DefinitionsRootFolder\policyStructures"
if (-not (Test-Path -Path $structureDirectory)) {
    New-Item -ItemType Directory -Path $structureDirectory
}

try {
    $telemetryEnabled = (Get-Content $DefinitionsRootFolder/global-settings.jsonc | ConvertFrom-Json).telemetryOptOut
    $deploymentRootScope = (Get-Content $DefinitionsRootFolder/global-settings.jsonc | ConvertFrom-Json).pacEnvironments[0]
    if (!($telemetryEnabled)) {
        Write-ModernStatus -Message "Telemetry is enabled" -Status "info" -Indent 2
        Submit-EPACTelemetry -Cuapid "pid-adaa7564-1962-46e6-92b4-735e91f76d43" -DeploymentRootScope $deploymentRootScope
    }
    else {
        Write-ModernStatus -Message "Telemetry is disabled" -Status "info" -Indent 2
    }
}
catch {
    Write-ModernStatus -Message "Telemetry could not be enabled: $($_.Exception.Message)" -Status "warning" -Indent 2
}

$syncedPolicyDefinitionNames = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
$syncedPolicyDefinitionPaths = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)

#region MLZ
# Mission Landing Zone (https://github.com/Azure/missionlz) shares no layout with the Azure Landing
# Zones Library, so it gets a completely self contained code path. It has no custom policy or policy
# set definitions - every baseline is a built-in initiative - so all this produces is assignments.
if ($Type -eq "MLZ") {

    foreach ($switchName in @("CreateGuardrailAssignments", "EnableOverrides", "SyncAMBAExtendedPolicies")) {
        if ($PSBoundParameters.ContainsKey($switchName) -and $PSBoundParameters[$switchName]) {
            Write-ModernStatus -Message "-$switchName is not applicable to MLZ and is ignored" -Status "warning" -Indent 2
        }
    }
    if ($SyncAssignmentsOnly) {
        Write-ModernStatus -Message "-SyncAssignmentsOnly has no effect for MLZ because assignments are the only output" -Status "info" -Indent 2
    }

    #region Resolve the cloud
    # policy-assignment.bicep branches on environment().name, so the equivalent here is the cloud of
    # the PAC environment being synced.
    $mlzCloud = "AzureCloud"
    try {
        $mlzPacEnvironment = (Get-Content -Path "$DefinitionsRootFolder/global-settings.jsonc" -Raw -ErrorAction Stop | ConvertFrom-Json).pacEnvironments |
        Where-Object { $_.pacSelector -eq $PacEnvironmentSelector } | Select-Object -First 1
        if ($null -eq $mlzPacEnvironment) {
            throw "no PAC environment with pacSelector '$PacEnvironmentSelector' was found"
        }
        if ([string]::IsNullOrWhiteSpace($mlzPacEnvironment.cloud)) {
            throw "PAC environment '$PacEnvironmentSelector' does not specify a cloud"
        }
        $mlzCloud = $mlzPacEnvironment.cloud
    }
    catch {
        Write-ModernStatus -Message "Could not resolve the cloud from global-settings.jsonc ($($_.Exception.Message)) - defaulting to 'AzureCloud'" -Status "warning" -Indent 2
    }
    # The bicep comparison is case insensitive, so this one is too.
    $mlzIsCommercialCloud = $mlzCloud -eq "AzureCloud"
    Write-ModernStatus -Message "Cloud: $mlzCloud" -Status "info" -Indent 2
    #endregion Resolve the cloud

    #region Source repository
    $mlzTempPath = $null
    if ($LibraryPath -eq "") {
        $mlzTempPath = Join-Path -Path ([IO.Path]::GetTempPath()) -ChildPath "epac-mlz-$([guid]::NewGuid())"
        $LibraryPath = $mlzTempPath
        $ownedTempPaths.Add($LibraryPath)
        # missionlz has no usable release tag - its only tag predates the current src/policies content
        # - so the default branch is cloned.
        Write-ModernStatus -Message "Cloning Mission Landing Zone repository..." -Status "processing" -Indent 2
        git clone --config advice.detachedHead=false --depth 1 https://github.com/Azure/missionlz.git $LibraryPath
        if ($LASTEXITCODE -eq 0) {
            Write-ModernStatus -Message "Repository cloned successfully" -Status "success" -Indent 4
        }
        else {
            Write-ModernStatus -Message "Failed to clone repository" -Status "error" -Indent 4
            throw "Failed to clone Mission Landing Zone repository."
        }
    }

    $mlzPolicyPath = Join-Path -Path $LibraryPath -ChildPath "src/policies"
    if (-not (Test-Path -Path $mlzPolicyPath)) {
        Write-ModernStatus -Message "Could not find 'src/policies' in '$LibraryPath' - is this a Mission Landing Zone repository?" -Status "error" -Indent 2
        throw "Could not find 'src/policies' in '$LibraryPath' - is this a Mission Landing Zone repository?"
    }
    #endregion Source repository

    try {
        #region Structure file
        try {
            $structureFilePath = Get-ChildItem -Path $structureDirectory -Filter "*.$PacEnvironmentSelector.jsonc" -Recurse |
            Where-Object { $_.Name -match "mlz.policy_default_structure" } | Select-Object -First 1
            $structureFile = Get-Content -Path $structureFilePath.FullName -Raw -ErrorAction Stop | ConvertFrom-Json
            Write-ModernStatus -Message "Policy default structure file: $structureFilePath" -Status "info" -Indent 2
        }
        catch {
            Write-ModernStatus -Message "Error reading the policy default structure file: $($_.Exception.Message)" -Status "error" -Indent 2
            Write-ModernStatus -Message "Please run New-ALZPolicyDefaultStructure.ps1 -Type MLZ first" -Status "warning" -Indent 2
            throw
        }

        # Flatten the structure file stubs into assignment name -> parameter name -> value. Both the
        # schema's array form and the object form emitted for the other types are accepted so hand
        # edited files in either style keep working.
        $mlzStubValues = @{}
        $mlzEmptyStubNames = [System.Collections.Generic.List[string]]::new()
        foreach ($stub in $structureFile.defaultParameterValues.PSObject.Properties) {
            foreach ($entry in @($stub.Value)) {
                foreach ($parameter in @($entry.parameters)) {
                    if ([string]::IsNullOrWhiteSpace("$($parameter.value)") -and -not $mlzEmptyStubNames.Contains($stub.Name)) {
                        $mlzEmptyStubNames.Add($stub.Name)
                    }
                    foreach ($assignmentName in @($entry.policy_assignment_name)) {
                        if (-not $mlzStubValues.ContainsKey($assignmentName)) {
                            $mlzStubValues[$assignmentName] = [ordered]@{}
                        }
                        $mlzStubValues[$assignmentName][$parameter.parameter_name] = $parameter.value
                    }
                }
            }
        }
        #endregion Structure file

        # These two only exist on the CMMC assignment in Azure commercial.
        $mlzCommercialOnlyParameters = @(
            "MembersToExclude-69bf4abd-ca1e-4cf6-8b5a-762d42e61d4f"
            "MembersToInclude-30f71ea1-ac77-4f26-9fc5-2d926bbd4ba7"
        )

        $mlzBaselines = @(
            [ordered]@{
                name          = "CMMC"
                displayName   = "CMMC Level 3"
                description   = "Mission Landing Zone CMMC Level 3 baseline."
                policySetId   = "/providers/Microsoft.Authorization/policySetDefinitions/b5629c75-5c77-4422-87b9-2509e680f8de"
                parameterFile = "CMMC-policyAssignmentParameters.json"
            }
            [ordered]@{
                name          = "IL5"
                displayName   = "DoD Impact Level 5"
                description   = "Mission Landing Zone DoD Impact Level 5 baseline. Not available in Azure commercial, where NIST SP 800-53 Rev. 4 is assigned instead."
                policySetId   = "/providers/Microsoft.Authorization/policySetDefinitions/f9a961fa-3241-4b20-adc4-bbf8ad9d7197"
                parameterFile = "IL5-policyAssignmentParameters.json"
            }
            [ordered]@{
                name          = "NISTRev4"
                displayName   = "NIST SP 800-53 Rev. 4"
                description   = "Mission Landing Zone NIST SP 800-53 Rev. 4 baseline."
                policySetId   = "/providers/Microsoft.Authorization/policySetDefinitions/cf25b9c1-bd23-4eb6-bd2c-f4f3ac644a5f"
                parameterFile = "NISTRev4-policyAssignmentParameters.json"
            }
            [ordered]@{
                name          = "NISTRev5"
                displayName   = "NIST SP 800-53 Rev. 5"
                description   = "Mission Landing Zone NIST SP 800-53 Rev. 5 baseline."
                policySetId   = "/providers/Microsoft.Authorization/policySetDefinitions/179d1daa-458f-4e47-8086-2a68d0d6c38f"
                parameterFile = "NISTRev5-policyAssignmentParameters.json"
            }
            [ordered]@{
                name          = "Deploy-VMSS-Agents"
                displayName   = "Deploy VMSS Agents"
                description   = "Mission Landing Zone virtual machine scale set monitoring agent deployment."
                policySetId   = "/providers/Microsoft.Authorization/policySetDefinitions/75714362-cae7-409e-9b99-a8e5075b7fad"
                parameterFile = $null
            }
            [ordered]@{
                name          = "Deploy-VM-Agents"
                displayName   = "Deploy VM Agents"
                description   = "Mission Landing Zone virtual machine monitoring agent deployment."
                policySetId   = "/providers/Microsoft.Authorization/policySetDefinitions/55f3eceb-5573-4f18-9695-226972c6d74a"
                parameterFile = $null
            }
        )

        if ($mlzIsCommercialCloud) {
            Write-ModernStatus -Message "IL5 is skipped in Azure commercial - the deployment maps it to NIST SP 800-53 Rev. 4, which is generated as NISTRev4.jsonc" -Status "warning" -Indent 2
            $mlzBaselines = $mlzBaselines | Where-Object { $_.name -ne "IL5" }
        }

        Write-ModernSection -Title "Creating Policy Assignment Objects" -Indent 0

        $mlzAssignmentRoot = "$DefinitionsRootFolder/policyAssignments/MLZ/$PacEnvironmentSelector"
        $mlzExistingAssignmentPaths = @(Get-ChildItem -Path $mlzAssignmentRoot -Recurse -File -Include *.jsonc -ErrorAction SilentlyContinue | ForEach-Object { $_.FullName })
        $mlzCreatedAssignmentPaths = [System.Collections.Generic.List[string]]::new()
        $mlzPlaceholderScopes = [System.Collections.Generic.List[string]]::new()

        foreach ($scopeMapping in $structureFile.managementGroupNameMappings.PSObject.Properties) {
            $scopeValues = @($scopeMapping.Value.value)
            if ($scopeValues -contains "/subscriptions/00000000-0000-0000-0000-000000000000") {
                $mlzPlaceholderScopes.Add($scopeMapping.Name)
            }

            $nodeNamePrefix = $scopeMapping.Value.management_group_function
            if ([string]::IsNullOrWhiteSpace($nodeNamePrefix)) {
                $nodeNamePrefix = $scopeMapping.Name
            }
            $folderName = ($nodeNamePrefix -replace '[\\/:*?"<>|]', '-')
            $outputFolder = Join-Path -Path $mlzAssignmentRoot -ChildPath $folderName

            foreach ($baseline in $mlzBaselines) {
                $parameters = [ordered]@{}

                if ($baseline.parameterFile) {
                    $parameterFilePath = Join-Path -Path $mlzPolicyPath -ChildPath $baseline.parameterFile
                    if (-not (Test-Path -Path $parameterFilePath)) {
                        Write-ModernStatus -Message "Parameter file '$($baseline.parameterFile)' was not found - skipping $($baseline.name)" -Status "warning" -Indent 2
                        continue
                    }
                    # missionlz stores parameters as { "name": { "value": x } }; EPAC assignments
                    # take { "name": x }.
                    $sourceParameters = Get-Content -Path $parameterFilePath -Raw | ConvertFrom-Json
                    foreach ($sourceParameter in $sourceParameters.PSObject.Properties) {
                        $parameters[$sourceParameter.Name] = $sourceParameter.Value.value
                    }
                }

                if ($mlzStubValues.ContainsKey($baseline.name)) {
                    foreach ($parameterName in $mlzStubValues[$baseline.name].Keys) {
                        if (-not $mlzIsCommercialCloud -and $parameterName -in $mlzCommercialOnlyParameters) {
                            continue
                        }
                        $parameters[$parameterName] = $mlzStubValues[$baseline.name][$parameterName]
                    }
                }

                $baseTemplate = [ordered]@{
                    '$schema'       = "https://raw.githubusercontent.com/Azure/enterprise-azure-policy-as-code/main/Schemas/policy-assignment-schema.json"
                    nodeName        = "$nodeNamePrefix/$($baseline.name)"
                    assignment      = [ordered]@{
                        name        = $baseline.name
                        displayName = $baseline.displayName
                        description = $baseline.description
                    }
                    definitionEntry = [ordered]@{
                        displayName = $baseline.displayName
                        policySetId = $baseline.policySetId
                    }
                    enforcementMode = $structureFile.enforcementMode
                    parameters      = $parameters
                    scope           = [ordered]@{
                        $PacEnvironmentSelector = $scopeValues
                    }
                }

                $outputPath = Join-Path -Path $outputFolder -ChildPath "$($baseline.name).jsonc"
                $null = New-Item -Path $outputFolder -ItemType Directory -Force
                ($baseTemplate | ConvertTo-Json -Depth 50) | Set-Content -Path $outputPath -Encoding utf8 -Force
                $mlzCreatedAssignmentPaths.Add((Get-Item -Path $outputPath).FullName)
                Write-ModernStatus -Message "Created assignment '$($baseline.name)' with $($parameters.Count) parameters" -Status "success" -Indent 2
            }
        }

        # Remove assignments that were not created in this run - this is what removes a previously
        # generated IL5.jsonc when the PAC environment moves to Azure commercial.
        foreach ($existingPath in $mlzExistingAssignmentPaths) {
            if ($existingPath -notin $mlzCreatedAssignmentPaths) {
                Remove-Item -Path $existingPath -Force -ErrorAction SilentlyContinue
                Write-ModernStatus -Message "Removed '$existingPath' as it is no longer generated for this cloud" -Status "info" -Indent 2
            }
        }

        foreach ($placeholderScope in $mlzPlaceholderScopes) {
            Write-ModernStatus -Message "Scope '$placeholderScope' is still the placeholder subscription id - update the structure file before deploying" -Status "warning" -Indent 2
        }
        foreach ($emptyStubName in $mlzEmptyStubNames) {
            Write-ModernStatus -Message "Structure file value '$emptyStubName' is empty - populate it before deploying" -Status "warning" -Indent 2
        }

        Write-ModernStatus -Message "MLZ Policy sync completed successfully" -Status "success" -Indent 0
    }
    finally {
        if ($mlzTempPath -and $LibraryPath -eq $mlzTempPath) {
            Remove-Item -Path $LibraryPath -Recurse -Force -ErrorAction SilentlyContinue
        }
    }

    return
}
#endregion MLZ

# Read the structure before writing definitions so extended filters are validated before any output.
try {
    $structureFilePath = Get-ChildItem -Path $structureDirectory -Filter "*.$PacEnvironmentSelector.jsonc" -Recurse | Where-Object { $_.Name -match "$($Type.ToLower()).policy_default_structure" } | Select-Object -First 1
    $structureFile = Get-Content -Path $structureFilePath.FullName -Raw -ErrorAction Stop | ConvertFrom-Json
    Write-ModernStatus -Message "Policy default structure file: $structureFilePath" -Status "info" -Indent 2
}
catch {
    Write-ModernStatus -Message "Error reading the policy default structure file: $($_.Exception.Message)" -Status "error" -Indent 2
    Write-ModernStatus -Message "Please run New-ALZPolicyDefaultStructure.ps1 first" -Status "warning" -Indent 2
    throw
}

$ambaExtendedSelectors = @{}
if (-not $SyncAssignmentsOnly -and $Type -eq "AMBA" -and $SyncAMBAExtendedPolicies -and
    $null -ne $structureFile.PSObject.Properties["ambaExtendedPolicyFilters"]) {
    $filters = $structureFile.ambaExtendedPolicyFilters
    if ($filters -isnot [pscustomobject]) {
        throw "ambaExtendedPolicyFilters must be an object."
    }
    foreach ($filter in $filters.PSObject.Properties) {
        if ($filter.Name -cnotin @("include", "exclude")) {
            throw "ambaExtendedPolicyFilters supports only include and exclude expressions."
        }
        $ambaExtendedSelectors[$filter.Name] = ConvertTo-AMBAExtendedPolicySelector -Selector $filter.Value -Path "ambaExtendedPolicyFilters.$($filter.Name)"
    }
}

if (-not($SyncAssignmentsOnly) -and $Type -ne "SLZ") {
    Write-ModernSection -Title "Creating Policy Definition Objects" -Indent 0
    #region Create policy definition objects
    foreach ($file in Get-ChildItem -Path "$LibraryPath/platform/$($Type.ToLower())/policy_definitions" -Recurse -File -Include *.json) {
        $fileContent = Get-Content -Path $file.FullName -Raw | ConvertFrom-Json
        $baseTemplate = [ordered]@{
            '$schema'  = "https://raw.githubusercontent.com/Azure/enterprise-azure-policy-as-code/main/Schemas/policy-definition-schema.json"
            name       = $fileContent.name
            properties = $fileContent.properties
        }
        $category = $baseTemplate.properties.Metadata.category
        ([PSCustomObject]$baseTemplate | Select-Object -Property "`$schema", name, properties | ConvertTo-Json -Depth 50) -replace "\[\[", "[" | New-Item -Path "$DefinitionsRootFolder/policyDefinitions/$Type/$category" -ItemType File -Name "$($fileContent.name).json" -Force -ErrorAction Stop
        [void]$syncedPolicyDefinitionNames.Add(($fileContent.name -replace "/", "_" -replace "%", "pc"))
        [void]$syncedPolicyDefinitionPaths.Add([IO.Path]::GetFullPath("$DefinitionsRootFolder/policyDefinitions/$Type/$category/$($fileContent.name).json"))
    }
    Write-ModernSection -Title "Creating Policy Set Definition Objects" -Indent 0
    #endregion Create policy definition objects

    #region Create policy set definition objects
    foreach ($file in Get-ChildItem -Path "$LibraryPath/platform/$($Type.ToLower())/policy_set_definitions" -Recurse -File -Include *.json) {
        $fileContent = Get-Content -Path $file.FullName -Raw | ConvertFrom-Json
        $baseTemplate = [ordered]@{
            "`$schema" = "https://raw.githubusercontent.com/Azure/enterprise-azure-policy-as-code/main/Schemas/policy-set-definition-schema.json"
            name       = $fileContent.name
            properties = [ordered]@{
                description            = $fileContent.properties.description
                displayName            = $fileContent.properties.displayName
                metadata               = $fileContent.properties.metadata
                parameters             = $fileContent.properties.parameters
                policyType             = $fileContent.properties.policyType
                policyDefinitionGroups = $fileContent.properties.policyDefinitionGroups
            }
        }
        $policyDefinitions = @()
        # Fix the policyDefinitionIds for custom policies
        foreach ($policyDefinition in $fileContent.properties.policyDefinitions) {
            $obj = [ordered]@{
                parameters                  = $policyDefinition.parameters
                groupNames                  = $policyDefinition.groupNames
                policyDefinitionReferenceId = $policyDefinition.policyDefinitionReferenceId
            }
            if ($policyDefinition.policyDefinitionId -match "managementGroups") {
                $obj.Add("policyDefinitionName", $policyDefinition.policyDefinitionId.split("/")[ - 1])
            }
            else {
                $obj.Add("policyDefinitionId", $policyDefinition.policyDefinitionId)
            }
            if ($policyDefinition.definitionVersion) {
                $obj.Add("definitionVersion", $policyDefinition.definitionVersion)
            }
            $policyDefinitions += $obj
        }
        $baseTemplate.properties.policyDefinitions = $policyDefinitions

        # Force property order
        $orderedProps = [ordered]@{
            description            = $baseTemplate.properties.description
            displayName            = $baseTemplate.properties.displayName
            metadata               = $baseTemplate.properties.metadata
            parameters             = $baseTemplate.properties.parameters
            policyDefinitions      = $baseTemplate.properties.policyDefinitions
            policyType             = $baseTemplate.properties.policyType
            policyDefinitionGroups = $baseTemplate.properties.policyDefinitionGroups
        }
        $baseTemplate.properties = $orderedProps

        $category = $baseTemplate.properties.Metadata.category
        ([PSCustomObject]$baseTemplate | Select-Object -Property "`$schema", name, properties | ConvertTo-Json -Depth 50) -replace "\[\[", "[" `
            -replace "variables\('scope'\)", "'/providers/Microsoft.Management/managementGroups/$managementGroupId'" `
            -replace "(?<!'true)', '(?!false)", "" `
            -replace "\[concat\(('(.+)')\)\]", "`$2" | New-Item -Path "$DefinitionsRootFolder/policySetDefinitions/$Type/$category" -ItemType File -Name "$($fileContent.name).json" -Force -ErrorAction Stop
    }
    Write-ModernSection -Title "Creating Assignment Objects" -Indent 0
}
#endregion Create policy set definition objects

function Sync-AMBAExtendedPolicyDefinitions {
    [CmdletBinding()]
    param()

    $ErrorActionPreference = "Stop"
    Write-ModernSection -Title "Creating AMBA Extended Policy Definition Objects" -Indent 0
    #region Create AMBA extended policy definition objects
    $extendedOutputRoot = [IO.Path]::GetFullPath("$DefinitionsRootFolder/policyDefinitions/AMBA")
    # Build and validate the complete catalogue before writing or deleting any extended definitions.
    $catalogue = @{}
    $desiredPolicies = @{}
    $sourceFiles = @(Get-ChildItem -LiteralPath "$AMBALibraryPath/services" -Recurse -File -Filter *.json -ErrorAction Stop |
        Where-Object FullName -match "[\\/]+policy[\\/]+")
    if ($sourceFiles.Count -eq 0) {
        throw "No AMBA extended source policies found; refusing to prune local definitions."
    }
    foreach ($file in $sourceFiles) {
        $fileContent = Get-Content -LiteralPath $file.FullName -Raw -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
        if ([string]::IsNullOrWhiteSpace($fileContent.name) -or $null -eq $fileContent.properties) {
            throw "Invalid AMBA extended source policy '$($file.FullName)'."
        }
        $policyName = $fileContent.name -replace "/", "_" -replace "%", "pc"
        $relativeFolder = [System.IO.Path]::GetRelativePath((Join-Path $AMBALibraryPath "services"), $file.DirectoryName).Replace('\', '/')
        $folderParts = $relativeFolder.Split('/')
        if ($folderParts.Count -lt 3) {
            throw "Unexpected AMBA extended source path '$($file.FullName)'."
        }
        $relativePath = "$($folderParts[0])/$($folderParts[1])/$($file.Name)"
        $outputPath = Get-AMBAExtendedOutputPath -Root $extendedOutputRoot -RelativePath $relativePath
        if ($catalogue.ContainsKey($relativePath)) {
            throw "Multiple AMBA extended source policies map to '$relativePath'; refusing to reconcile."
        }
        $json = ([ordered]@{
            '$schema' = "https://raw.githubusercontent.com/Azure/enterprise-azure-policy-as-code/main/Schemas/policy-definition-schema.json"
            name = $policyName
            properties = $fileContent.properties
        } | ConvertTo-Json -Depth 50) -replace '\[\[', '['
        $catalogue[$relativePath] = @{
            Path = $outputPath
            Json = $json
        }
        if ($syncedPolicyDefinitionPaths.Contains($outputPath)) {
            Write-ModernStatus -Message "Preserving ALZ Library output '$outputPath' instead of the extended policy." -Status "warning" -Indent 2
            continue
        }
        if ($syncedPolicyDefinitionNames.Contains($policyName)) {
            Write-ModernStatus -Message "Skipping duplicate policy definition '$policyName' - already synced from ALZ Library" -Status "warning" -Indent 2
            continue
        }
        if ($ambaExtendedSelectors.ContainsKey("include") -and
            -not (Test-AMBAExtendedPolicySelector -Folder $relativeFolder -FileName $file.Name -Selector $ambaExtendedSelectors.include)) {
            continue
        }
        if ($ambaExtendedSelectors.ContainsKey("exclude") -and
            (Test-AMBAExtendedPolicySelector -Folder $relativeFolder -FileName $file.Name -Selector $ambaExtendedSelectors.exclude)) {
            continue
        }
        $desiredPolicies[$relativePath] = $catalogue[$relativePath]
    }

    $ambiguousPaths = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
    $cleanupCandidates = @{}
    if (Test-Path -LiteralPath $extendedOutputRoot) {
        foreach ($existingFile in Get-ChildItem -LiteralPath $extendedOutputRoot -Recurse -File -Filter *.json -ErrorAction Stop) {
            if ($syncedPolicyDefinitionPaths.Contains($existingFile.FullName)) {
                continue
            }
            $relativePath = [IO.Path]::GetRelativePath($extendedOutputRoot, $existingFile.FullName).Replace('\', '/')
            if ($catalogue.ContainsKey($relativePath)) {
                # Without persisted ownership, only exact generated content can identify an extended file.
                $existingContent = Get-Content -LiteralPath $existingFile.FullName -Raw -ErrorAction Stop
                try {
                    $existingPolicy = $existingContent | ConvertFrom-Json -ErrorAction Stop
                }
                catch {
                    [void]$ambiguousPaths.Add($relativePath)
                    Write-ModernStatus -Message "Ambiguous AMBA policy file '$($existingFile.FullName)' is not valid JSON; leaving it untouched: $($_.Exception.Message)" -Status "warning" -Indent 2
                    continue
                }
                $expectedPolicy = $catalogue[$relativePath].Json | ConvertFrom-Json -ErrorAction Stop
                if (Test-ALZParameterValueEqual -Left $existingPolicy -Right $expectedPolicy) {
                    $cleanupCandidates[$relativePath] = $existingContent
                    continue
                }
            }
            [void]$ambiguousPaths.Add($relativePath)
            Write-ModernStatus -Message "Ambiguous AMBA policy file '$($existingFile.FullName)' is untracked or modified; leaving it untouched." -Status "warning" -Indent 2
        }
    }

    foreach ($relativePath in $desiredPolicies.Keys) {
        if ($ambiguousPaths.Contains($relativePath)) {
            continue
        }
        $policy = $desiredPolicies[$relativePath]
        $null = New-Item -ItemType Directory -Path (Split-Path $policy.Path) -Force -ErrorAction Stop
        [IO.File]::WriteAllText($policy.Path, $policy.Json, [Text.UTF8Encoding]::new($false))
    }
    # Prune only after all desired writes succeed, and recheck for concurrent local edits.
    foreach ($relativePath in $cleanupCandidates.Keys) {
        $outputPath = Get-AMBAExtendedOutputPath -Root $extendedOutputRoot -RelativePath $relativePath
        if ($syncedPolicyDefinitionPaths.Contains($outputPath) -or $desiredPolicies.ContainsKey($relativePath)) {
            continue
        }
        if (Test-Path -LiteralPath $outputPath) {
            if ((Get-Content -LiteralPath $outputPath -Raw -ErrorAction Stop) -cne $cleanupCandidates[$relativePath]) {
                Write-ModernStatus -Message "Ambiguous AMBA policy file '$outputPath' changed during sync; leaving it untouched." -Status "warning" -Indent 2
                continue
            }
            Remove-Item -LiteralPath $outputPath -Force -ErrorAction Stop
            Write-ModernStatus -Message "Removed extended policy definition '$outputPath' because it is no longer selected." -Status "info" -Indent 2
        }
    }
    if (Test-Path -LiteralPath $extendedOutputRoot) {
        $directories = Get-ChildItem -LiteralPath $extendedOutputRoot -Directory -Recurse -Force -ErrorAction Stop |
            Sort-Object { $_.FullName.Length } -Descending
        foreach ($directory in $directories) {
            if ($directory.Attributes -band [IO.FileAttributes]::ReparsePoint) {
                continue
            }
            if (@(Get-ChildItem -LiteralPath $directory.FullName -Force -ErrorAction Stop).Count -eq 0) {
                [IO.Directory]::Delete($directory.FullName, $false)
                Write-ModernStatus -Message "Removed empty AMBA policy folder '$($directory.FullName)'." -Status "info" -Indent 2
            }
        }
    }
}

if (-not($SyncAssignmentsOnly) -and $Type -eq "AMBA" -and $SyncAMBAExtendedPolicies) {
    try {
        Sync-AMBAExtendedPolicyDefinitions -ErrorAction Stop
    }
    catch {
        Write-ModernStatus -Message "Error reconciling AMBA extended policies: $($_.Exception.Message)" -Status "error" -Indent 2
        throw
    }
}

#region Create assignment objects
$parameterFileValues = $null
$policySetDefinitionsByName = @{}
$missingParameterFilePolicySets = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
if (-not [string]::IsNullOrWhiteSpace($ParameterFile)) {
    try {
        $resolvedParameterFile = (Resolve-Path -Path $ParameterFile -ErrorAction Stop).Path
        $parameterFileValues = Get-Content -Path $resolvedParameterFile -Raw -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
        if ($null -eq $parameterFileValues) {
            throw "The parameter file is empty."
        }

        foreach ($policySetFile in Get-ChildItem -Path "$LibraryPath/platform/$($Type.ToLower())/policy_set_definitions" -Recurse -File -Include *.json) {
            $policySetDefinition = Get-Content -Path $policySetFile.FullName -Raw -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
            if ([string]::IsNullOrWhiteSpace($policySetDefinition.name)) {
                throw "Policy set definition '$($policySetFile.FullName)' does not contain a name."
            }
            $policySetDefinitionsByName[$policySetDefinition.name] = $policySetDefinition
        }

        Write-ModernStatus -Message "Policy set parameter file: $resolvedParameterFile" -Status "info" -Indent 2
        if ($EnableOverrides -and $null -ne $structureFile.overrides.parameters) {
            Write-ModernStatus -Message "Parameter file specified: overrides.parameters will be ignored; archetype and enforcement overrides remain enabled." -Status "info" -Indent 2
        }
    }
    catch {
        Write-ModernStatus -Message "Error reading the policy set parameter file: $($_.Exception.Message)" -Status "error" -Indent 2
        throw
    }
}

# Gather existing files
$existingAssignments = Get-ChildItem -Path "$DefinitionsRootFolder/policyAssignments/$Type/$PacEnvironmentSelector" -Recurse -File -Include *.jsonc -ErrorAction SilentlyContinue | `
    ForEach-Object {
    $content = Get-Content -Path $_.FullName -Raw | ConvertFrom-Json
    [PSCustomObject]@{
        Path = $_.FullName
        Name = $content.assignment.name
    }
}

$policyAssignmentNameOverrides = @{}
$resolvedAssignmentNameOverrideKeys = [System.Collections.Generic.HashSet[string]]::new()

if ($EnableOverrides) {
    Write-ModernStatus -Message "Overrides enabled: Custom management group structures and assignments will be used where available." -Status "info" -Indent 2
    if ($structureFile.overrides.archetypes.ignore) {
        $ignoreArchetypes = $structureFile.overrides.archetypes.ignore
    }
    if ($structureFile.overrides.archetypes.custom) {
        $customArchetypes = $structureFile.overrides.archetypes.custom
    }
}

try {
    # Determine if custom archetypes should be injected
    $archetypeArray = @()
    foreach ($file in Get-ChildItem -Path "$LibraryPath/platform/$($Type.ToLower())/archetype_definitions" -Recurse -File -Include *.json) {
        $customArchetypeContent = Get-Content -Path $file.FullName -Raw | ConvertFrom-Json
        $archetypeArray += $customArchetypeContent
    }
    foreach ($customArchetype in $customArchetypes) {
        #Check if included in management group mappings
        if (-not ($structureFile.managementGroupNameMappings.PSObject.Properties.Name -contains $customArchetype.name) -and (-not ($Type -eq "SLZ" -and (($structureFile.managementGroupNameMappings.PSObject.Properties.Name -contains $customArchetype.name) -or ($structureFile.overrides.archetypes.custom.name -contains $customArchetype.name) )))) {
            Write-ModernStatus -Message "Custom archetype '$($customArchetype.name)' not found in management group mappings. Skipping." -Status "warning" -Indent 2
            continue
        }
        if ($customArchetype.policy_assignments_to_add) {
            $normalizedPolicyAssignmentsToAdd = @()
            foreach ($policyAssignmentToAdd in @($customArchetype.policy_assignments_to_add)) {
                if ($policyAssignmentToAdd -is [string]) {
                    if ($policyAssignmentToAdd.Length -gt 24) {
                        throw "policy_assignments_to_add['$policyAssignmentToAdd'] would generate an assignment name of $($policyAssignmentToAdd.Length) chars but the target scope is a management group (24-char limit). Specify an assignment_name override, or omit this entry."
                    }
                    $normalizedPolicyAssignmentsToAdd += $policyAssignmentToAdd
                    continue
                }

                if ($policyAssignmentToAdd -is [psobject] -and
                    $policyAssignmentToAdd.PSObject.Properties.Name -contains "policy_name" -and
                    $policyAssignmentToAdd.PSObject.Properties.Name -contains "assignment_name") {
                    if ([string]::IsNullOrWhiteSpace($policyAssignmentToAdd.policy_name) -or [string]::IsNullOrWhiteSpace($policyAssignmentToAdd.assignment_name)) {
                        throw "policy_assignments_to_add for archetype '$($customArchetype.name)' contains an entry with empty policy_name or assignment_name."
                    }
                    if ($policyAssignmentToAdd.assignment_name.Length -gt 24) {
                        throw "policy_assignments_to_add['$($policyAssignmentToAdd.policy_name)'].assignment_name '$($policyAssignmentToAdd.assignment_name)' exceeds the management-group assignment name limit (24 chars)."
                    }
                    $normalizedPolicyAssignmentsToAdd += $policyAssignmentToAdd.policy_name
                    $finalArchetypeName = Get-ALZFinalArchetypeName -ArchetypeName $customArchetype.name -Type $Type
                    if (-not $policyAssignmentNameOverrides.ContainsKey($finalArchetypeName)) {
                        $policyAssignmentNameOverrides[$finalArchetypeName] = @{}
                    }
                    $policyAssignmentNameOverrides[$finalArchetypeName][$policyAssignmentToAdd.policy_name] = $policyAssignmentToAdd.assignment_name
                    continue
                }

                throw "policy_assignments_to_add for archetype '$($customArchetype.name)' must use either a string value or an object with policy_name and assignment_name."
            }

            $customArchetype.policy_assignments_to_add = @($normalizedPolicyAssignmentsToAdd)
        }
        $archetypeArray += $customArchetype
    }
    # Modify default archetypes if requested
    $finalArchetypeArray = @()
    $cleanupArchetype = @() # Track archetypes that end up with no assignments and need to be cleaned up from the final array
    # Modify anything that is existing
    foreach ($archetype in $archetypeArray | Where-Object { $_.type -eq "existing" }) {
        if ($archetype.PSObject.properties.name -contains "based_on") {
            $basedOnMatchNames = Get-ALZBasedOnMatchNames -BasedOn $archetype.based_on -Type $Type
            $archetypeObj = @{
                name               = $archetype.name
                policy_assignments = $archetypeArray | Where-Object { $_.name -in $basedOnMatchNames -and $_.PSObject.properties.name -notcontains "type" } | Select-Object -ExpandProperty policy_assignments | Where-Object { $_ -notin $archetype.policy_assignments_to_remove }
            }
        }
        else {
            if ($archetype.name -eq "landingzones") {
                $archetypeObj = @{
                    name               = Get-ALZFinalArchetypeName -ArchetypeName $archetype.name -Type $Type
                    policy_assignments = $archetypeArray | Where-Object { $_.name -match "landing_zones" -and $_.PSObject.properties.name -notcontains "type" } | Select-Object -ExpandProperty policy_assignments | Where-Object { $_ -notin $archetype.policy_assignments_to_remove }
                }
            }
            elseif ($archetype.name -eq "alz") {
                $archetypeObj = @{
                    name               = Get-ALZFinalArchetypeName -ArchetypeName $archetype.name -Type $Type
                    policy_assignments = $archetypeArray | Where-Object { $_.name -match "root" -and $_.PSObject.properties.name -notcontains "type" } | Select-Object -ExpandProperty policy_assignments | Where-Object { $_ -notin $archetype.policy_assignments_to_remove }
                }
            }
            elseif ($archetype.name -eq "sovereign_root") {
                $archetypeObj = @{
                    name               = Get-ALZFinalArchetypeName -ArchetypeName $archetype.name -Type $Type
                    policy_assignments = $archetypeArray | Where-Object { $_.name -match "sovereign_root" -and $_.PSObject.properties.name -notcontains "type" } | Select-Object -ExpandProperty policy_assignments | Where-Object { $_ -notin $archetype.policy_assignments_to_remove }
                }
            }
            else {
                $archetypeObj = @{
                    name               = Get-ALZFinalArchetypeName -ArchetypeName $archetype.name -Type $Type
                    policy_assignments = $archetypeArray | Where-Object { $_.name -match $archetype.name -and $_.PSObject.properties.name -notcontains "type" } | Select-Object -ExpandProperty policy_assignments | Where-Object { $_ -notin $archetype.policy_assignments_to_remove }
                }
            }
        }
        if ($archetype.policy_assignments_to_add) {
            if ($archetypeObj.policy_assignments -is [string]) {
                $archetypeObj.policy_assignments = @($archetypeObj.policy_assignments)
            }
            $archetypeObj.policy_assignments += $archetype.policy_assignments_to_add
        }
        if (-not($archetypeObj.policy_assignments | Measure-Object).Count -eq 0) {
            $finalArchetypeArray += $archetypeObj
        }
        else {
            Write-ModernStatus -Message "Archetype '$($archetype.name)' has no policy assignments after modifications. Skipping." -Status "warning" -Indent 2
            $cleanupArchetype += $archetype.name
        }

    }
    #Check again for new archetypes based on a custom archetype
    foreach ($archetype in $archetypeArray | Where-Object { $_.type -eq "existing" -and $_.name -notin ($finalArchetypeArray.name) -and (($_.name -notmatch "alz") -or ($Type -eq "AMBA" -and $_.name -eq "alz")) }) {
        if ($archetype.PSObject.properties.name -contains "based_on") {
            $basedOnMatchNames = Get-ALZBasedOnMatchNames -BasedOn $archetype.based_on -Type $Type
            $archetypeObj = @{
                name               = $archetype.name
                policy_assignments = $finalArchetypeArray | Where-Object { $_.name -in $basedOnMatchNames -and $_.PSObject.properties.name -notcontains "type" } | Select-Object -ExpandProperty policy_assignments | Where-Object { $_ -notin $archetype.policy_assignments_to_remove }
            }
        }
        if ($archetype.policy_assignments_to_add) {
            $archetypeObj.policy_assignments += $archetype.policy_assignments_to_add
        }
        if (-not($archetypeObj.policy_assignments | Measure-Object).Count -eq 0) {
            $finalArchetypeArray += $archetypeObj
        }
        else {
            Write-ModernStatus -Message "Archetype '$($archetype.name)' has no policy assignments after modifications. Skipping." -Status "warning" -Indent 2
            $cleanupArchetype += $archetype.name
        }
    }

    # Add any new archetypes
    foreach ($archetype in $archetypeArray | Where-Object { $_.type -ne "existing" -and $_.name -notin ($finalArchetypeArray.name) }) {
        $finalArchetypeArray += $archetype
    }

    # Get rid of a duplicate landing zones archetype if it exists
    if (($finalArchetypeArray.name -contains "landingzones" -and $finalArchetypeArray.name -contains "landing_zones")) {
        $finalArchetypeArray = $finalArchetypeArray | Where-Object { $_.name -ne "landing_zones" }
    }

    # In AMBA, overrides target base archetype names (e.g., alz/platform/connectivity) but final archetypes are amba_*.
    # Apply all override removals against the mapped AMBA archetype names.
    if ($Type -eq "AMBA") {
        $ambaArchetypeNameMap = @{
            "alz"           = "amba_root"
            "root"          = "amba_root"
            "landingzones"  = "amba_landing_zones"
            "landing_zones" = "amba_landing_zones"
        }

        foreach ($overrideArchetype in ($archetypeArray | Where-Object { $_.type -eq "existing" -and $_.PSObject.Properties.Name -contains "policy_assignments_to_remove" })) {
            $overrideRemovals = @($overrideArchetype.policy_assignments_to_remove)
            if (($overrideRemovals | Measure-Object).Count -eq 0) {
                continue
            }

            $mappedArchetypeName = if ($overrideArchetype.name -like "amba_*") {
                $overrideArchetype.name
            }
            elseif ($ambaArchetypeNameMap.ContainsKey($overrideArchetype.name)) {
                $ambaArchetypeNameMap[$overrideArchetype.name]
            }
            else {
                "amba_$($overrideArchetype.name)"
            }

            foreach ($archetype in ($finalArchetypeArray | Where-Object { $_.name -eq $mappedArchetypeName })) {
                $archetype.policy_assignments = @($archetype.policy_assignments | Where-Object { $_ -notin $overrideRemovals })
            }
        }
    }

    if ($cleanupArchetype -contains "alz") {
        $cleanupArchetype += "root"
    }

    if ($cleanupArchetype -contains "landingzones") {
        $cleanupArchetype += "landing_zones"
    }

    # Cleanup any archetypes that are based on modified archetypes but were not themselves modified and now have no assignments
    $finalArchetypeArray = $finalArchetypeArray | Where-Object { $_.name -notin $cleanupArchetype }
    $createdPolicyAssignments = @()

    if ($EnableOverrides) {
        $enforcementModeObject = $structureFile.overrides.enforcementMode
    }

    # Invert the enforcement mode object structure: each value in the arrays becomes a key, with the original key as its value
    $invertedEnforcementMode = @{}
    if ($null -ne $enforcementModeObject) {
        foreach ($property in $enforcementModeObject.PSObject.Properties) {
            foreach ($item in $property.Value) {
                $invertedEnforcementMode[$item] = $property.Name
            }
        }
    }

    foreach ($archetype in $finalArchetypeArray) {
        if ($archetype.name -in $ignoreArchetypes) {
            Write-ModernStatus -Message "Ignoring archetype: $($archetype.name)" -Status "info" -Indent 2
            continue
        }
        foreach ($requiredAssignment in ($archetype.policy_assignments | Where-Object {
                    -not [string]::IsNullOrWhiteSpace("$_") -and ($CreateGuardrailAssignments -or ($_ -notmatch "^Enforce-(GR|Encrypt)-\w+0"))
                })) {
            $assignedPolicySetName = $null
            $assignmentFromDefinition = $false
            $assignmentNameOverride = $null
            if ($policyAssignmentNameOverrides.ContainsKey($archetype.name) -and $policyAssignmentNameOverrides[$archetype.name].ContainsKey($requiredAssignment)) {
                $assignmentNameOverride = $policyAssignmentNameOverrides[$archetype.name][$requiredAssignment]
            }
            switch ($Type) {
                "ALZ" { $fileContent = Get-ChildItem -Path "$LibraryPath/platform/$($Type.ToLower())/policy_assignments" | Where-Object { $_.BaseName.Split(".")[0] -eq $requiredAssignment } | Get-Content -Raw | ConvertFrom-Json }
                "AMBA" { $fileContent = Get-ChildItem -Path "$LibraryPath/platform/$($Type.ToLower())/policy_assignments" | Where-Object { $_.BaseName.Split(".")[0].Replace("_", "-") -eq $requiredAssignment } | Get-Content -Raw | ConvertFrom-Json }
                "SLZ" { $fileContent = Get-ChildItem -Path "$LibraryPath/platform/$($Type.ToLower())/policy_assignments" | Where-Object { $_.BaseName.Split(".")[0].Replace("_", "-") -eq $requiredAssignment } | Get-Content -Raw | ConvertFrom-Json }
                "FSI" { $fileContent = Get-ChildItem -Path "$LibraryPath/platform/$($Type.ToLower())/policy_assignments" | Where-Object { $_.BaseName.Split(".")[0].Replace("_", "-") -eq $requiredAssignment } | Get-Content -Raw | ConvertFrom-Json }
                default { $fileContent = Get-ChildItem -Path "$LibraryPath/platform/$($Type.ToLower())/policy_assignments" | Where-Object { $_.BaseName.Split(".")[0] -eq $requiredAssignment } | Get-Content -Raw | ConvertFrom-Json }
            }

            if ($null -eq $fileContent -or [string]::IsNullOrWhiteSpace($fileContent.name)) {
                if ($Type -eq "ALZ") {
                    # Search for a policy definitions file that matches the required assignment name as a fallback for unresolved assignments
                    $fileContent = Get-ChildItem -Path "$LibraryPath/platform/$($Type.ToLower())/policy_definitions" -Recurse -File -Include *.json | Where-Object { $_.BaseName.Split(".")[0] -eq $requiredAssignment } | Get-Content -Raw | ConvertFrom-Json
                    $assignmentFromDefinition = $null -ne $fileContent -and -not [string]::IsNullOrWhiteSpace($fileContent.name)
                }
                if ($null -eq $fileContent -or [string]::IsNullOrWhiteSpace($fileContent.name)) {
                    Write-ModernStatus -Message "Skipping unresolved policy definition '$requiredAssignment' in archetype '$($archetype.name)'." -Status "warning" -Indent 2
                    continue
                }
            }

            $nodeNamePrefix = if ($archetype.name -eq "landingzones") { "landing_zones" } else { $archetype.name }
            $effectiveAssignmentName = if ([string]::IsNullOrWhiteSpace($assignmentNameOverride)) { $fileContent.Name } else { $assignmentNameOverride }

            $baseTemplate = [ordered]@{
                "`$schema"      = "https://raw.githubusercontent.com/Azure/enterprise-azure-policy-as-code/main/Schemas/policy-assignment-schema.json"
                nodeName        = "$($nodeNamePrefix)/$effectiveAssignmentName"
                assignment      = [ordered]@{
                    name        = $effectiveAssignmentName
                    displayName = $fileContent.properties.displayName
                    description = $fileContent.properties.description
                }
                definitionEntry = [ordered]@{
                    displayName = $fileContent.properties.displayName
                }
                parameters      = [ordered]@{}
                enforcementMode = if ($invertedEnforcementMode.ContainsKey("$($nodeNamePrefix)/$effectiveAssignmentName")) {
                    $invertedEnforcementMode["$($nodeNamePrefix)/$effectiveAssignmentName"]
                }
                elseif ($Type -eq "ALZ" -and $fileContent.name -match "^Enforce-(GR|Encrypt)-" -and
                    -not [string]::IsNullOrWhiteSpace($fileContent.properties.enforcementMode)) {
                    $fileContent.properties.enforcementMode
                }
                else {
                    $structureFile.enforcementMode
                }
                
            }

            # Definition Version
            if ($null -ne $fileContent.properties.definitionVersion) {
                $baseTemplate.Add("definitionVersion", $fileContent.properties.definitionVersion)
            }

            # Definition Entry
            if ($fileContent.properties.policyRule) {
                $baseTemplate.definitionEntry.Add("policyName", $fileContent.name)
            }
            elseif ($fileContent.properties.policyDefinitions) {
                $baseTemplate.definitionEntry.Add("policySetName", $fileContent.name)
                $assignedPolicySetName = $fileContent.name
            }
            elseif ($fileContent.properties.policyDefinitionId -match "placeholder.+policySetDefinition") {
                $assignedPolicySetName = ($fileContent.properties.policyDefinitionId).Split("/")[ - 1]
                $baseTemplate.definitionEntry.Add("policySetName", $assignedPolicySetName)
            }
            elseif ($fileContent.properties.policyDefinitionId -match "placeholder.+policyDefinition") {
                $baseTemplate.definitionEntry.Add("policyName", ($fileContent.properties.policyDefinitionId).Split("/")[ - 1])
            }
            elseif ($fileContent.properties.policyDefinitionId -match "policySetDefinitions") {
                $baseTemplate.definitionEntry.Add("policySetId", ($fileContent.properties.policyDefinitionId))
                $assignedPolicySetName = ($fileContent.properties.policyDefinitionId).Split("/")[ - 1]
            }
            else {
                $baseTemplate.definitionEntry.Add("policyId", ($fileContent.properties.policyDefinitionId))
            }

            $parameterFilePolicySet = $null
            if ($null -ne $parameterFileValues -and -not [string]::IsNullOrWhiteSpace($assignedPolicySetName)) {
                $parameterFilePolicySet = $parameterFileValues.PSObject.Properties | Where-Object { $_.Name -eq $assignedPolicySetName } | Select-Object -First 1
                if ($null -eq $parameterFilePolicySet -and $missingParameterFilePolicySets.Add($assignedPolicySetName)) {
                    Write-ModernStatus -Message "Policy set '$assignedPolicySetName' is not present in the parameter file; library assignment parameters will be retained." -Status "warning" -Indent 2
                }
            }
            $useParameterFileForAssignment = $null -ne $parameterFilePolicySet
            
            #Scope
            $scopeTrim = $archetype.name
            if ($scopeTrim -eq "root") {
                $scopeTrim = "alz"
            }
            if ($scopeTrim -like "amba_*") {
                $scopeTrim = $scopeTrim -replace "amba_", ""
                if ($scopeTrim -eq "root") {
                    $scopeTrim = "alz"
                }
            }
            if ($scopeTrim -eq "landing_zones") {
                $scopeTrim = "landingzones"
            }
            if ($scopeTrim -eq "global") {
                $scopeTrim = "mcfs"
            }
            if ($scopeTrim -eq "sovereign_root" -and $Type -eq "SLZ") {
                $scopeTrim = "slz"
            }
            if ($Type -eq "FSI" -and $scopeTrim -ne "confidential") {
                $scopeTrim = "fsi"
            }
            if ($scopeTrim -eq "confidential") {
                $scopes = foreach ($key in $structureFile.managementGroupNameMappings.psObject.Properties.Name) {
                    if ($structureFile.managementGroupNameMappings.$key.management_group_function -match $scopeTrim) {
                        # Handle both string and array values
                        $value = $structureFile.managementGroupNameMappings.$key.value
                        if ($value -is [array]) {
                            $value
                        }
                        else {
                            $value
                        }
                    }
                }
                $scope = [ordered]@{
                    $PacEnvironmentSelector = $scopes
                }
            }
            else {
                $scopeValueFromArchetypeMap = $null
                if ($Type -eq "SLZ" -and $null -ne $structureFile.archetypeScopeMappings) {
                    $scopeValueFromArchetypeMap = $structureFile.archetypeScopeMappings.$scopeTrim
                }

                if ($null -ne $scopeValueFromArchetypeMap) {
                    if ($scopeValueFromArchetypeMap -is [array]) {
                        $resolvedScopeValues = @($scopeValueFromArchetypeMap | Where-Object { -not [string]::IsNullOrWhiteSpace("$_") } | Select-Object -Unique)
                    }
                    else {
                        $resolvedScopeValues = @($scopeValueFromArchetypeMap)
                    }

                    $scope = [ordered]@{
                        $PacEnvironmentSelector = $resolvedScopeValues
                    }
                }
                else {
                    # Handle both string and array values for regular scope mappings
                    $scopeValue = $structureFile.managementGroupNameMappings.$scopeTrim.value
                    if ($scopeValue -is [array]) {
                        $scope = [ordered]@{
                            $PacEnvironmentSelector = $scopeValue
                        }
                    }
                    else {
                        $scope = [ordered]@{
                            $PacEnvironmentSelector = @(
                                $scopeValue
                            )
                        }
                    }
                }
            }


            $baseTemplate.Add("scope", $scope)

            $ambaAssignmentsRequiringAdditionalRoleAssignments = @(
                "Deploy-AMBA-Web",
                "Deploy-AMBA-VM",
                "Deploy-AMBA-HybridVM",
                "Deploy-AMBA-VMSS",
                "Deploy-AMBA-Management"
            )

            # Base Parameters
            if (-not $useParameterFileForAssignment -and $fileContent.name -ne "Deploy-Private-DNS-Zones" -and $assignmentFromDefinition -ne $true) {
                foreach ($parameter in $fileContent.properties.parameters.psObject.Properties.Name) {
                    $baseTemplate.parameters.Add($parameter, $fileContent.properties.parameters.$parameter.value)
                }
            }

            # Non-compliance messages
            if ($null -ne $fileContent.properties.nonComplianceMessages) {
                $enforcementModeText = switch ($baseTemplate.enforcementMode) {
                    "Default" { "must" }
                    "DoNotEnforce" { "should" }
                }
                $obj = @(
                    @{
                        message = $fileContent.properties.nonComplianceMessages.message -replace "{enforcementMode}", $enforcementModeText
                    }
                )
                $baseTemplate.Add("nonComplianceMessages", $obj)
            }


            # Check for explicit parameters
            if ($fileContent.name -ne "Deploy-Private-DNS-Zones") {
                if (-not $useParameterFileForAssignment) {
                    foreach ($key in $structureFile.defaultParameterValues.psObject.Properties.Name) {
                        foreach ($defaultParameterValue in @($structureFile.defaultParameterValues.$key)) {
                            if (@($defaultParameterValue.policy_assignment_name) -notcontains $fileContent.name) {
                                continue
                            }

                            foreach ($parameter in @($defaultParameterValue.parameters)) {
                                $baseTemplate.parameters[$parameter.parameter_name] = $parameter.value
                            }
                        }
                    }
                }
                # Check for override parameter values
                if ($EnableOverrides -and $null -eq $parameterFileValues) {
                    if ($structureFile.overrides.parameters.$($archetype.name)) {
                        foreach ($overrideParameters in $structureFile.overrides.parameters.$($archetype.name) | Where-Object { $_.policy_assignment_name -eq $fileContent.name }) {
                            foreach ($param in $overrideParameters.parameters) {
                                $baseTemplate.parameters[$param.parameter_name] = $param.value
                            }
                            # sort parameters alphabetically
                            $sortedParams = [ordered]@{}
                            foreach ($key in ($baseTemplate.parameters.Keys | Sort-Object)) {
                                $sortedParams[$key] = $baseTemplate.parameters[$key]
                            }
                            # Replace the original with the sorted version
                            $baseTemplate.parameters = $sortedParams
                        }

                    }
                }

                if ($Type -eq "AMBA" -and $fileContent.name -in $ambaAssignmentsRequiringAdditionalRoleAssignments) {
                    $managementScopeValue = $structureFile.managementGroupNameMappings.management.value
                    # Only add the additional role assignment when the identity remediating the policy needs
                    # access to the "management" management group from a different assignment scope. When the
                    # assignment is already scoped to "management" (e.g. Deploy-AMBA-Management), EPAC already
                    # auto-assigns the required roles at the assignment scope, so adding it here is redundant
                    # and results in a role assignment with an empty principalId at deployment time.
                    $assignmentScopeValues = @($scope[$PacEnvironmentSelector])
                    if ($assignmentScopeValues -notcontains $managementScopeValue) {
                        $additionalRoleAssignments = @{
                            $PacEnvironmentSelector = @(
                                [ordered]@{
                                    roleDefinitionId = "/providers/microsoft.authorization/roleDefinitions/f1a07417-d97a-45cb-824c-7a7467783830"
                                    scope            = $managementScopeValue
                                }
                            )
                        }
                        $baseTemplate.Add("additionalRoleAssignments", $additionalRoleAssignments)
                    }
                }

                $alzVmInsightsAssignmentsRequiringAdditionalRoleAssignments = @(
                    "Deploy-VM-Monitoring",
                    "Deploy-VM-ChangeTrack",
                    "Deploy-VMSS-Monitoring",
                    "Deploy-vmHybr-Monitoring"
                )

                if ($Type -eq "ALZ" -and $fileContent.name -in $alzVmInsightsAssignmentsRequiringAdditionalRoleAssignments) {
                    $additionalRoleAssignmentEntries = @()

                    # VM Insights / Change Tracking policies support bringing your own user-assigned managed
                    # identity that can live outside the assignment's own scope. When that is the case, the
                    # remediation identity needs Managed Identity Operator on the user-assigned identity so it
                    # can be assigned to VMs.
                    if ($baseTemplate.parameters.Contains("userAssignedIdentityResourceId") -and `
                            -not [string]::IsNullOrWhiteSpace([string]$baseTemplate.parameters["userAssignedIdentityResourceId"]) -and `
                            $baseTemplate.parameters.Contains("bringYourOwnUserAssignedManagedIdentity") -and `
                            $baseTemplate.parameters["bringYourOwnUserAssignedManagedIdentity"] -eq $true -and `
                        (-not $baseTemplate.parameters.Contains("restrictBringYourOwnUserAssignedIdentityToSubscription") -or `
                                $baseTemplate.parameters["restrictBringYourOwnUserAssignedIdentityToSubscription"] -eq $false)) {
                        $additionalRoleAssignmentEntries += [ordered]@{
                            roleDefinitionId = "/providers/microsoft.authorization/roleDefinitions/f1a07417-d97a-45cb-824c-7a7467783830"
                            scope            = $baseTemplate.parameters["userAssignedIdentityResourceId"]
                        }
                    }

                    # These same policies (and Deploy-vmHybr-Monitoring, which uses a system-assigned identity)
                    # read a Data Collection Rule that can also live outside the assignment's scope. The
                    # remediation identity needs Monitoring Reader on the DCR to read it.
                    if ($baseTemplate.parameters.Contains("dcrResourceId") -and `
                            -not [string]::IsNullOrWhiteSpace([string]$baseTemplate.parameters["dcrResourceId"])) {
                        $additionalRoleAssignmentEntries += [ordered]@{
                            roleDefinitionId = "/providers/microsoft.authorization/roleDefinitions/43d0d8ad-25c7-4714-9337-8ba259a9fe05"
                            scope            = $baseTemplate.parameters["dcrResourceId"]
                        }
                    }

                    if ($additionalRoleAssignmentEntries.Count -gt 0) {
                        $additionalRoleAssignments = @{
                            $PacEnvironmentSelector = @($additionalRoleAssignmentEntries)
                        }
                        $baseTemplate.Add("additionalRoleAssignments", $additionalRoleAssignments)
                    }
                }
            }
            else {
                $dnzZoneSubscription = $structureFile.defaultParameterValues.private_dns_zone_subscription_id.parameters.value
                $dnzZoneResourceGroupName = $structureFile.defaultParameterValues.private_dns_zone_resource_group_name.parameters.value
                if ([string]::IsNullOrWhiteSpace($dnzZoneSubscription)) {
                    Write-ModernStatus -Message "Skipping assignment '$effectiveAssignmentName' in archetype '$($archetype.name)': private_dns_zone_subscription_id is blank. Populate it to generate Deploy-Private-DNS-Zones." -Status "warning" -Indent 2
                    continue
                }
                if ([string]::IsNullOrWhiteSpace($dnzZoneResourceGroupName)) {
                    throw "private_dns_zone_resource_group_name is required for Deploy-Private-DNS-Zones."
                }
                $dnsZoneRegion = ConvertTo-ALZPrivateDnsZoneRegionCode -Region $structureFile.defaultParameterValues.private_dns_zone_region.parameters.value
                foreach ($parameter in $fileContent.properties.parameters.psObject.Properties.Name) {
                    $zoneName = $fileContent.properties.parameters.$parameter.value.split("/")[-1]
                    if (-not [string]::IsNullOrWhiteSpace($dnsZoneRegion)) {
                        $zoneName = $zoneName -replace '^privatelink\.ne\.', "privatelink.$dnsZoneRegion."
                    }
                    $value = "/subscriptions/$dnzZoneSubscription/resourceGroups/$dnzZoneResourceGroupName/providers/Microsoft.Network/privateDnsZones/$zoneName"
                    #$value = $fileContent.properties.parameters.$parameter.value -replace "00000000-0000-0000-0000-000000000000", $dnzZoneSubscription -replace "placeholder", $dnzZoneResourceGroupName
                    $baseTemplate.parameters.Add($parameter, $value)
                }

                $additionalRoleAssignments = @{
                    $PacEnvironmentSelector = @(
                        [ordered]@{
                            roleDefinitionId = "/providers/microsoft.authorization/roleDefinitions/b12aa53e-6015-4669-85d0-8515ebb3ae7f"
                            scope            = "/subscriptions/$($structureFile.defaultParameterValues.private_dns_zone_subscription_id.parameters.value)"
                        }
                    )
                }
                $baseTemplate.Add("additionalRoleAssignments", $additionalRoleAssignments)


            }

            if ($useParameterFileForAssignment) {
                if (-not $policySetDefinitionsByName.ContainsKey($assignedPolicySetName)) {
                    throw "Policy set '$assignedPolicySetName' from the parameter file was not found in the ALZ Library."
                }

                $policySetDefinitionParameters = $policySetDefinitionsByName[$assignedPolicySetName].properties.parameters
                $inputParameterNames = [string[]] @($parameterFilePolicySet.Value.PSObject.Properties.Name | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })
                [Array]::Sort($inputParameterNames, [System.StringComparer]::Ordinal)
                foreach ($inputParameterName in $inputParameterNames) {
                    $inputParameter = $parameterFilePolicySet.Value.PSObject.Properties[$inputParameterName]
                    $definitionParameter = $policySetDefinitionParameters.PSObject.Properties | Where-Object { $_.Name -eq $inputParameter.Name } | Select-Object -First 1
                    if ($null -eq $definitionParameter) {
                        Write-ModernStatus -Message "Parameter '$($inputParameter.Name)' from policy set '$assignedPolicySetName' does not exist in the ALZ Library and will be ignored." -Status "warning" -Indent 2
                        continue
                    }

                    $defaultValue = $definitionParameter.Value.defaultValue
                    if (-not (Test-ALZParameterValueEqual -Left $inputParameter.Value -Right $defaultValue)) {
                        $baseTemplate.parameters.Add($inputParameter.Name, $inputParameter.Value)
                    }
                }

                foreach ($key in $structureFile.defaultParameterValues.PSObject.Properties.Name) {
                    foreach ($defaultParameterValue in @($structureFile.defaultParameterValues.$key)) {
                        if (@($defaultParameterValue.policy_assignment_name) -notcontains $fileContent.name) {
                            continue
                        }

                        foreach ($parameter in @($defaultParameterValue.parameters)) {
                            $definitionParameter = $policySetDefinitionParameters.PSObject.Properties | Where-Object { $_.Name -eq $parameter.parameter_name } | Select-Object -First 1
                            if ($null -eq $definitionParameter) {
                                Write-ModernStatus -Message "Default parameter '$($parameter.parameter_name)' for policy assignment '$($fileContent.name)' does not exist in policy set '$assignedPolicySetName' and will be ignored." -Status "warning" -Indent 2
                                continue
                            }

                            if (Test-ALZParameterValueEqual -Left $parameter.value -Right $definitionParameter.Value.defaultValue) {
                                $null = $baseTemplate.parameters.Remove($parameter.parameter_name)
                            }
                            else {
                                $baseTemplate.parameters[$parameter.parameter_name] = $parameter.value
                            }
                        }
                    }
                }

                $sortedParameters = [ordered]@{}
                $parameterNames = [string[]] @($baseTemplate.parameters.Keys)
                [Array]::Sort($parameterNames, [System.StringComparer]::Ordinal)
                foreach ($parameterName in $parameterNames) {
                    $sortedParameters[$parameterName] = $baseTemplate.parameters[$parameterName]
                }
                $baseTemplate.parameters = $sortedParameters
            }

            $category = $structureFile.managementGroupNameMappings.$scopeTrim.management_group_function
            if ([string]::IsNullOrWhiteSpace($category)) {
                $category = $archetype.name
            }
            if ($assignmentFromDefinition) {
                $propertiesToSelect = @("`$schema", "nodeName", "assignment", "definitionEntry", "enforcementMode", "parameters", "scope")
            }
            else {
                $propertiesToSelect = @("`$schema", "nodeName", "assignment", "definitionEntry", "definitionVersion", "enforcementMode", "parameters", "nonComplianceMessages", "scope")
                if ($baseTemplate.Contains("additionalRoleAssignments")) {
                    $propertiesToSelect += "additionalRoleAssignments"
                }
            }
            $propertiesToSelect = @($propertiesToSelect | Where-Object { $baseTemplate.Contains($_) })
            $assignmentContent = ([PSCustomObject]$baseTemplate | Select-Object -Property $propertiesToSelect | ConvertTo-Json -Depth 50) -replace "\[\[", "["
            $assignmentFile = $assignmentContent | New-Item -Path "$DefinitionsRootFolder/policyAssignments/$Type/$PacEnvironmentSelector/$category" -ItemType File -Name "$effectiveAssignmentName.jsonc" -Force -ErrorAction Stop
            $obj = [PSCustomObject]@{
                Path = $assignmentFile.FullName
                Name = $effectiveAssignmentName
            }
            $createdPolicyAssignments += $obj
            if (-not [string]::IsNullOrWhiteSpace($assignmentNameOverride)) {
                $null = $resolvedAssignmentNameOverrideKeys.Add("$($archetype.name)|$requiredAssignment")
            }
        }
    }

    # Warn about any assignment_name overrides that never resolved to a created assignment.
    # This surfaces archetype/policy name mismatches that would otherwise silently produce an
    # assignment under the long library name instead of the requested override name.
    foreach ($archetypeKey in $policyAssignmentNameOverrides.Keys) {
        foreach ($policyKey in $policyAssignmentNameOverrides[$archetypeKey].Keys) {
            if (-not $resolvedAssignmentNameOverrideKeys.Contains("$archetypeKey|$policyKey")) {
                Write-ModernStatus -Message "Assignment name override '$($policyAssignmentNameOverrides[$archetypeKey][$policyKey])' for policy '$policyKey' in archetype '$archetypeKey' did not resolve to any created assignment and was ignored." -Status "warning" -Indent 2
            }
        }
    }

    # Remove any assignments that were not created in this sync to clean up old assignments that are no longer in the library structure
    $assignmentsToRemove = $existingAssignments | Where-Object { $_.Path -notin $createdPolicyAssignments.Path }
    foreach ($assignment in $assignmentsToRemove) {
        Remove-Item -LiteralPath $assignment.Path -Force -ErrorAction Stop
        Write-ModernStatus -Message "Removed assignment '$($assignment.Name)' as it is no longer included in the library structure." -Status "info" -Indent 2
    }

    Write-ModernStatus -Message "ALZ Policy sync completed successfully" -Status "success" -Indent 0
}
catch {
    Write-ModernStatus -Message "Error during sync: $($_.Exception.Message)" -Status "error" -Indent 0
    throw
}
#endregion Create assignment objects
}
finally {
    foreach ($ownedTempPath in $ownedTempPaths) {
        if (Test-Path -LiteralPath $ownedTempPath) {
            Remove-Item -LiteralPath $ownedTempPath -Recurse -Force -ErrorAction Stop
        }
    }
}
}