# QwenClaw Windows Startup Installer (Startup Folder Method)
# This method does NOT require admin privileges
# Run this ONCE to register QwenClaw to start automatically with Windows

$ErrorActionPreference = "Stop"

$SCRIPT_NAME = "QwenClaw Daemon"
$SCRIPT_PATH = Join-Path $PSScriptRoot "autostart.ps1"
$LOG_DIR = Join-Path $env:USERPROFILE ".qwen\qwenclaw"
$STARTUP_FOLDER = [Environment]::GetFolderPath("Startup")

Write-Host "QwenClaw Windows Startup Installer" -ForegroundColor Cyan
Write-Host "===================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Using Windows Startup Folder method (no admin required)" -ForegroundColor Yellow
Write-Host ""

# Ensure log directory exists
if (-not (Test-Path $LOG_DIR)) {
    New-Item -ItemType Directory -Path $LOG_DIR -Force | Out-Null
    Write-Host "[OK] Created log directory: $LOG_DIR" -ForegroundColor Green
}

# Create a shortcut in the Startup folder
$shortcutPath = Join-Path $STARTUP_FOLDER "$SCRIPT_NAME.lnk"

try {
    $WScript = New-Object -ComObject WScript.Shell
    $shortcut = $WScript.CreateShortcut($shortcutPath)
    $shortcut.TargetPath = "PowerShell.exe"
    $shortcut.Arguments = "-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$SCRIPT_PATH`""
    $shortcut.WorkingDirectory = Split-Path $SCRIPT_PATH -Parent
    $shortcut.Description = "Automatically starts QwenClaw daemon when user logs on"
    $shortcut.IconLocation = "powershell.exe,0"
    $shortcut.Save()
    
    Write-Host "[OK] Startup shortcut created: $shortcutPath" -ForegroundColor Green
    Write-Host ""
    Write-Host "QwenClaw will now start automatically when you log in to Windows." -ForegroundColor Green
    Write-Host ""
    Write-Host "To verify: Open Shell:Startup folder and look for '$SCRIPT_NAME'" -ForegroundColor Cyan
    Write-Host "To disable: Delete the shortcut from the Startup folder" -ForegroundColor Cyan
} catch {
    Write-Host "[ERROR] Failed to create startup shortcut: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}
