[CmdletBinding()]
param(
    [Parameter(Mandatory=$true, HelpMessage="Path to the local zip file to transfer")]
    [string]$LocalZipPath,
    
    [Parameter(HelpMessage="Remote destination path for the zip file")]
    [string]$RemoteZipPath,
    
    [Parameter(HelpMessage="Remote machine name or IP")]
    [string]$RemoteMachine = 'macmini2012',
    
    [Parameter(HelpMessage="Remote username")]
    [string]$Username = 'administrator',
    
    [Parameter(HelpMessage="Remote password")]
    [string]$Password = 'Woslaodaha0909',
    
    [Parameter(HelpMessage="Enable beep notification on completion")]
    [switch]$EnableBeep = $true,
    
    [Parameter(HelpMessage="Beep frequency in Hz")]
    [int]$BeepFrequency = 1000,
    
    [Parameter(HelpMessage="Beep duration in milliseconds")]
    [int]$BeepDuration = 500
)

# Set default RemoteZipPath if not provided
if (-not $RemoteZipPath) {
    $fileName = Split-Path -Leaf $LocalZipPath
    $RemoteZipPath = "C:\temp\$fileName"
}

# Messages
$msgValidatingZip = "Validating local zip file..."
$msgZipNotFound = "Local zip file not found: {0}"
$msgZipFound = "Local zip file found: {0}"
$msgTransferring = "Transferring zip file to remote machine..."
$msgTransferComplete = "Transfer complete!"
$msgCreatingSession = "Creating remote session..."
$msgCheckingRemoteDir = "Checking remote directory..."
$msgCreatingRemoteDir = "Creating remote directory..."
$msgRemotePathReady = "Remote path is ready."
$msgFileSize = "File size: {0:N2} MB"

# Convert password to secure string
$securePassword = $Password | ConvertTo-SecureString -AsPlainText -Force

try {
    # Validate local zip file exists
    Write-Host $msgValidatingZip
    if (!(Test-Path $LocalZipPath)) {
        Write-Error ($msgZipNotFound -f $LocalZipPath)
        exit 1
    }
    
    Write-Host ($msgZipFound -f $LocalZipPath) -ForegroundColor Green
    
    # Get file size
    $fileInfo = Get-Item $LocalZipPath
    $fileSizeMB = $fileInfo.Length / 1MB
    Write-Host ($msgFileSize -f $fileSizeMB)
    
    # Prepare credentials
    $cred = New-Object System.Management.Automation.PSCredential($Username, $securePassword)
    
    # Create a session
    Write-Host $msgCreatingSession
    $session = New-PSSession -ComputerName $RemoteMachine -Credential $cred -ErrorAction Stop
    
    # Check if remote directory exists and create if needed
    Write-Host $msgCheckingRemoteDir
    $remoteDir = Split-Path -Parent $RemoteZipPath
    
    Invoke-Command -Session $session -ScriptBlock {
        param($dir, $msgCreatingRemoteDir, $msgRemotePathReady)
        if (!(Test-Path $dir)) {
            Write-Host $msgCreatingRemoteDir
            New-Item -Path $dir -ItemType Directory -Force | Out-Null
        }
        Write-Host $msgRemotePathReady
    } -ArgumentList $remoteDir, $msgCreatingRemoteDir, $msgRemotePathReady
    
    # Transfer the zip file
    Write-Host $msgTransferring -ForegroundColor Yellow
    if ($Verbose) {
        Copy-Item -Path $LocalZipPath -Destination $RemoteZipPath -ToSession $session -Force -Verbose
    } else {
        Copy-Item -Path $LocalZipPath -Destination $RemoteZipPath -ToSession $session -Force
    }
    
    # Verify the file was copied
    $remoteFileExists = Invoke-Command -Session $session -ScriptBlock {
        param($path)
        Test-Path $path
    } -ArgumentList $RemoteZipPath
    
    if ($remoteFileExists) {
        Write-Host $msgTransferComplete -ForegroundColor Green
        
        # Get remote file size for verification
        $remoteFileSize = Invoke-Command -Session $session -ScriptBlock {
            param($path)
            (Get-Item $path).Length
        } -ArgumentList $RemoteZipPath
        
        $remoteFileSizeMB = $remoteFileSize / 1MB
        Write-Host "Remote file size: $([math]::Round($remoteFileSizeMB, 2)) MB" -ForegroundColor Green
        
        if ([math]::Abs($fileSizeMB - $remoteFileSizeMB) -gt 0.01) {
            Write-Warning "File sizes do not match! Local: $([math]::Round($fileSizeMB, 2)) MB, Remote: $([math]::Round($remoteFileSizeMB, 2)) MB"
        }
    } else {
        Write-Error "Failed to verify the transferred file on remote machine"
        exit 1
    }
    
    # Clean up session
    Remove-PSSession $session
    
    # Play a beep sound if enabled
    if ($EnableBeep) {
        [console]::beep($BeepFrequency, $BeepDuration)
    }
    
    Write-Host "Successfully transferred $LocalZipPath to $RemoteMachine`:$RemoteZipPath" -ForegroundColor Green
    
} catch {
    Write-Error "An error occurred: $_"
    if ($session) {
        Remove-PSSession $session -ErrorAction SilentlyContinue
    }
    exit 1
}