Function Unlock-User {
    <#
    .SYNOPSIS
        Unlocks a locked user account in DRA.

    .DESCRIPTION
        Checks whether one or more user accounts are locked out in Active Directory.
        If an account is locked, it is unlocked via DRA. If the account is not locked,
        a warning is emitted.

    .PARAMETER UserName
        The name(s) of the user(s) to unlock.

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

    .EXAMPLE
        Unlock-User -UserName 'Doe.John', 'Smith.Jane'
        Unlocks the specified user accounts if they are currently locked.

    .EXAMPLE
        'Doe.John' | Unlock-User -Verbose
        Unlocks a user passed via the pipeline and shows verbose output.
    #>
    [CmdletBinding(SupportsShouldProcess=$true)]
    param(
        [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
        [string[]]$UserName,

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

    Begin {
        $UserName = @(Split-ToolArgumentList -Value $UserName)
        $i = 1
        $total = $UserName.Count
    }

    Process {
        foreach ($user in $UserName) {
            $percentComplete = ($i / $total) * 100
            Show-ProgressBar -Activity "Unlocking $user" -PercentComplete $percentComplete

            try {
                # Resolve the user's canonical name via AD
                $adUser = Get-ToolADUser -Identity $user -Properties LockedOut -ErrorAction Stop
                $displayName = $adUser.Name
                $isLocked = $adUser.LockedOut

                Write-Verbose "Resolved '$user' to '$displayName'. LockedOut = $isLocked"

                if ($isLocked) {
                    if ($pscmdlet.ShouldProcess($displayName, "Unlock User Account")) {
                        if ($script:SimulationMode) {
                            Set-ToolADUserProperty -Identity $user -Property 'LockedOut' -Value $false -ErrorAction Stop | Out-Null
                            Write-Verbose "Simulation: unlocked user: $displayName"
                        } else {
                            Unlock-DRAUser `
                                -Domain "AREA52.afnoapps.usaf.mil" `
                                -DRARestServer $DraServer `
                                -DRARestPort 8755 `
                                -DRAHostServer $DraServer `
                                -DRAHostPort 11192 `
                                -Identifier $displayName

                            Write-Verbose "Successfully unlocked user: $displayName"
                        }
                    }
                }
                else {
                    Write-Warning "User '$user' is not currently locked out."
                }
            }
            catch {
                Write-Warning "Could not unlock user '$user'. Error: $($_.Exception.Message)"
            }
            $i++
        }
    }

    End {
        Show-ProgressBar -Activity "Operation Complete" -PercentComplete 100
    }
}
