param(
    [string]$Title = "Antigravity",
    [string]$Message = "タスクが完了しました"
)

# Load local Windows SDK and WinRT DLLs
$PSScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$WinRTRuntime = Join-Path $PSScriptDir "WinRT.Runtime.dll"
$WinSDKNet = Join-Path $PSScriptDir "Microsoft.Windows.SDK.NET.dll"

Add-Type -Path $WinRTRuntime
Add-Type -Path $WinSDKNet

# Dynamically resolve PowerShell 7 AppID, fallback to Store version
$AppId = (Get-StartApps | Where-Object { $_.Name -like "PowerShell 7*" -or $_.Name -eq "PowerShell" } | Select-Object -First 1).AppID
if ($null -eq $AppId) {
    $AppId = 'Microsoft.PowerShell_8wekyb3d8bbwe!App'
}

function Escape-Xml ($string) {
    if ([string]::IsNullOrEmpty($string)) { return "" }
    return $string.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace('"', "&quot;").Replace("'", "&apos;")
}

$EscapedTitle = Escape-Xml $Title
$EscapedMessage = Escape-Xml $Message

$ToastXml = @"
<toast>
    <visual>
        <binding template="ToastGeneric">
            <text>$EscapedTitle</text>
            <text>$EscapedMessage</text>
        </binding>
    </visual>
</toast>
"@

$XmlDocument = [Windows.Data.Xml.Dom.XmlDocument]::new()
$XmlDocument.LoadXml($ToastXml)

$Toast = [Windows.UI.Notifications.ToastNotification]::new($XmlDocument)
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppId).Show($Toast)
