Function Create-UDrive {
    <#
    .SYNOPSIS
        Creates a U: drive home folder for a user on the network share.

    .DESCRIPTION
        Checks whether a U Drive already exists for the user. If not, creates
        the folder under the appropriate organizational path, sets NTFS
        permissions, and updates the user's HomeDrive and HomeDirectory
        attributes in DRA.

        Depends on: Find-UDrive, Set-User, Add-NTFSAccess

    .PARAMETER UserName
        The name of the user for whom the U Drive will be created.

    .PARAMETER Organization
        The organization group folder name configured under UDrive.Roots
        (e.g. "514MSG", "514OG").

    .EXAMPLE
        Create-UDrive -UserName 'Doe.John' -Organization '514MSG'
        Creates a U Drive for John Doe under the 514MSG organization path.
    #>
    [CmdletBinding(SupportsShouldProcess=$true)]
    param(
        [Parameter(Mandatory=$true, Position=0)]
        [string]$UserName,

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

    Process {
        # Resolve the user in AD
        try {
            $adUser = Get-ToolADUser -Identity $UserName -ErrorAction Stop
            $folderName = $adUser.Name
            $adAccount = $adUser.SID.Translate([System.Security.Principal.NTAccount])
            Write-Verbose "Resolved '$UserName' to '$folderName'"
        }
        catch {
            Write-Error "Could not resolve user '$UserName' in Active Directory. Error: $($_.Exception.Message)"
            return
        }

        # Validate the organization root path
        $uDrivePath = Get-ToolUDriveRootForOrganization -OrganizationGroup $Organization
        if (-not $uDrivePath) {
            Write-Error "No U Drive root is configured for organization group: $Organization"
            return
        }
        if (-not (Test-Path $uDrivePath)) {
            Write-Error "Organization U Drive root path does not exist: $uDrivePath"
            return
        }

        # Check if the user already has a U Drive
        $existingDrive = Find-UDrive -UserName $UserName
        if ($existingDrive) {
            Write-Warning "User already has a U Drive located at: $existingDrive"
            return
        }

        # Create the folder and set permissions
        $newFolder = Join-Path $uDrivePath $folderName

        if ($pscmdlet.ShouldProcess($folderName, "Create U Drive at $newFolder")) {
            try {
                $createdFolder = New-Item -Path $uDrivePath -Name $folderName -ItemType Directory -ErrorAction Stop
                Write-Verbose "Created folder: $($createdFolder.FullName)"

                # Set NTFS permissions
                Add-NTFSAccess -Path $createdFolder.FullName `
                    -Account $adAccount `
                    -AccessRights Modify `
                    -AccessType Allow `
                    -ErrorAction Stop
                Write-Verbose "Set Modify permission for $adAccount"

                # Update user attributes in DRA
                Set-User -UserName $UserName -Property 'HomeDrive'    -Value 'U:'              -ErrorAction Stop
                Set-User -UserName $UserName -Property 'HomeDirectory' -Value $createdFolder.FullName -ErrorAction Stop

                Write-Output "$($createdFolder.FullName) has been created for $folderName"
            }
            catch {
                Write-Error "Failed to create U Drive for '$UserName'. Error: $($_.Exception.Message)"
            }
        }
    }
}
