# QwenClaw Setup Script
# Run this ONCE to set up QwenClaw with persistent auto-start

$ErrorActionPreference = "Stop"

Write-Host ""
Write-Host "  ____                _      __ _ _           " -ForegroundColor Cyan
Write-Host " / ___|_ __ __ _  ___| | __ / _(_) | ___      " -ForegroundColor Cyan
Write-Host "| |   | '__/ _` |/ __| |/ /| |_| | |/ _ \     " -ForegroundColor Cyan
Write-Host "| |___| | | (_| | (__|   < |  _| | |  __/     " -ForegroundColor Cyan
Write-Host " \____|_|  \__,_|\___|_|\_\|_| |_|_|\___|     " -ForegroundColor Cyan
Write-Host ""
Write-Host "Persistent Daemon Setup" -ForegroundColor Cyan
Write-Host "=======================" -ForegroundColor Cyan
Write-Host ""

$QWENCLAW_DIR = Join-Path $env:USERPROFILE "qwenclaw"
$QWEN_DIR = Join-Path $env:USERPROFILE ".qwen"

# Step 1: Check prerequisites
Write-Host "[1/4] Checking prerequisites..." -ForegroundColor Yellow

try {
    $bunVersion = bun --version 2>&1
    Write-Host "      Bun detected: v$bunVersion" -ForegroundColor Green
} catch {
    Write-Host "      [ERROR] Bun is not installed. Install from https://bun.sh" -ForegroundColor Red
    exit 1
}

# Step 2: Install dependencies
Write-Host ""
Write-Host "[2/4] Installing dependencies..." -ForegroundColor Yellow
Set-Location $QWENCLAW_DIR
bun install

# Step 3: Create directories
Write-Host ""
Write-Host "[3/4] Creating directories..." -ForegroundColor Yellow
$dirs = @(
    (Join-Path $QWEN_DIR "qwenclaw"),
    (Join-Path $QWEN_DIR "qwenclaw\jobs"),
    (Join-Path $QWEN_DIR "qwenclaw\logs"),
    (Join-Path $QWEN_DIR "qwenclaw\inbox")
)

foreach ($dir in $dirs) {
    if (-not (Test-Path $dir)) {
        New-Item -ItemType Directory -Path $dir -Force | Out-Null
        Write-Host "      Created: $dir" -ForegroundColor Green
    }
}

# Step 4: Set up Windows startup
Write-Host ""
Write-Host "[4/4] Setting up Windows auto-start..." -ForegroundColor Yellow

$installScript = Join-Path $QWENCLAW_DIR "scripts\install-startup.ps1"
& $installScript

# Create initial settings
Write-Host ""
Write-Host "Creating default settings..." -ForegroundColor Yellow

$settingsPath = Join-Path $QWEN_DIR "qwenclaw\settings.json"
if (-not (Test-Path $settingsPath)) {
    $settings = @{
        model = ""
        api = ""
        autoStart = $true
        fallback = @{
            model = ""
            api = ""
        }
        timezone = "UTC"
        timezoneOffsetMinutes = 0
        heartbeat = @{
            enabled = $false
            interval = 15
            prompt = ""
            excludeWindows = @()
        }
        telegram = @{
            token = ""
            allowedUserIds = @()
        }
        security = @{
            level = "moderate"
            allowedTools = @()
            disallowedTools = @()
        }
        web = @{
            enabled = $true
            host = "127.0.0.1"
            port = 4632
        }
    }
    
    $settings | ConvertTo-Json -Depth 10 | Out-File -FilePath $settingsPath -Encoding utf8
    Write-Host "      Created: $settingsPath" -ForegroundColor Green
}

# Summary
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "  QwenClaw Setup Complete!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "What's configured:" -ForegroundColor Cyan
Write-Host "  [OK] Dependencies installed" -ForegroundColor Green
Write-Host "  [OK] Directories created" -ForegroundColor Green
Write-Host "  [OK] Windows auto-start enabled" -ForegroundColor Green
Write-Host "  [OK] Default settings created" -ForegroundColor Green
Write-Host ""
Write-Host "Quick Commands:" -ForegroundColor Cyan
Write-Host "  bun run start     - Start daemon manually" -ForegroundColor White
Write-Host "  bun run status    - Check daemon status" -ForegroundColor White
Write-Host "  bun run stop      - Stop daemon" -ForegroundColor White
Write-Host ""
Write-Host "Web Dashboard:" -ForegroundColor Cyan
Write-Host "  http://127.0.0.1:4632" -ForegroundColor White
Write-Host ""
Write-Host "The daemon will start automatically when you log in to Windows." -ForegroundColor Green
Write-Host ""
