Function Add-MemberToGroup {
    <#
    .SYNOPSIS
        Adds one or more users to one or more DRA security groups.

    .DESCRIPTION
        Resolves each user via Active Directory then adds them to each specified
        group in the DRA environment.

    .PARAMETER UserName
        The name(s) of the user(s) to add to groups. Accepts an array or comma-
        separated string.

    .PARAMETER GroupName
        The name(s) of the group(s) to add the users to. Accepts an array or
        comma-separated string.

    .PARAMETER DraServer
        The FQDN of the DRA server. Defaults to "mcguire.dra.us.af.mil".

    .EXAMPLE
        Add-MemberToGroup -UserName 'Doe.John' -GroupName 'SAShare Admins', 'VPN Users'
        Adds John Doe to two groups.

    .EXAMPLE
        Add-MemberToGroup -UserName 'Doe.John', 'Smith.Jane' -GroupName 'SAShare Admins' -Verbose
        Adds two users to one group and shows verbose output.
    #>
    [CmdletBinding(SupportsShouldProcess=$true)]
    param(
        [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
        [string[]]$UserName,

        [Parameter(Mandatory=$true, Position=1)]
        [string[]]$GroupName,

        [Parameter(Position=2)]
        [string]$DraServer = "mcguire.dra.us.af.mil"
    )

    Begin {
        $UserName = @(Split-ToolArgumentList -Value $UserName)
        $GroupName = @(Split-ToolArgumentList -Value $GroupName)
        $domain = "AREA52.afnoapps.usaf.mil"
    }

    Process {
        foreach ($user in $UserName) {
            try {
                $adUser = Get-ToolADUser -Identity $user -ErrorAction Stop
                $displayName = $adUser.Name
                Write-Verbose "Resolved '$user' to '$displayName'"
            }
            catch {
                Write-Warning "Could not resolve user '$user' in Active Directory. Error: $($_.Exception.Message)"
                continue
            }

            foreach ($group in $GroupName) {
                $trimmedGroup = $group.Trim()

                if ($pscmdlet.ShouldProcess($displayName, "Add to group '$trimmedGroup'")) {
                    try {
                        if ($script:SimulationMode) {
                            Add-ToolADGroupMember -UserIdentity $user -GroupIdentity $trimmedGroup -ErrorAction Stop | Out-Null
                            Write-Verbose "Simulation: added '$displayName' to group '$trimmedGroup'"
                        } else {
                            Add-DRAGroupMembers `
                                -Domain $domain `
                                -DRARestServer $DraServer `
                                -DRARestPort 8755 `
                                -DRAHostServer $DraServer `
                                -DRAHostPort 11192 `
                                -Identifier $trimmedGroup `
                                -User $displayName `
                                -ErrorAction Stop

                            Write-Verbose "Added '$displayName' to group '$trimmedGroup'"
                        }
                    }
                    catch {
                        Write-Warning "Could not add '$displayName' to group '$trimmedGroup'. Error: $($_.Exception.Message)"
                    }
                }
            }
        }
    }
}
