/**
 * Prepare a fresh Windows machine for VisionClaw.
 *
 * Installs dependencies via winget/choco, checks PowerShell execution policy,
 * and verifies Chrome is installed. Much simpler than prepare-mac.ts since
 * Windows doesn't need TCC permissions, dock management, or pmset config.
 */
import { exec, spawnSync } from "node:child_process";
import { promisify } from "node:util";
import prompts from "prompts";
import { checkChromeInstalled, checkFfmpegInstalled, checkGhInstalled, checkGcloudInstalled, checkPowerShellExecutionPolicy, checkPm2Installed, checkWhisperCliInstalled, } from "./windows-permissions.js";
const execAsync = promisify(exec);
function hasWinget() {
    return spawnSync("winget", ["--version"], { stdio: "ignore" }).status === 0;
}
function hasChoco() {
    return spawnSync("choco", ["--version"], { stdio: "ignore" }).status === 0;
}
async function installWithWinget(packageId, name) {
    console.log(`  Installing ${name} via winget...`);
    try {
        await execAsync(`winget install --id ${packageId} --accept-source-agreements --accept-package-agreements`, {
            timeout: 300_000,
        });
        console.log(`  ${name} installed successfully.`);
        return true;
    }
    catch (e) {
        console.log(`  Failed to install ${name}: ${e instanceof Error ? e.message : String(e)}`);
        return false;
    }
}
async function installWithChoco(packageName, name) {
    console.log(`  Installing ${name} via Chocolatey...`);
    try {
        await execAsync(`choco install ${packageName} -y`, { timeout: 300_000 });
        console.log(`  ${name} installed successfully.`);
        return true;
    }
    catch (e) {
        console.log(`  Failed to install ${name}: ${e instanceof Error ? e.message : String(e)}`);
        return false;
    }
}
async function installPackage(wingetId, chocoName, displayName, useWinget, useChoco) {
    if (useWinget) {
        return installWithWinget(wingetId, displayName);
    }
    if (useChoco) {
        return installWithChoco(chocoName, displayName);
    }
    console.log(`  Cannot install ${displayName} — no package manager available.`);
    console.log(`  Please install manually: winget install ${wingetId}`);
    return false;
}
export async function runPrepareWindows() {
    console.log("=== Prepare Windows for VisionClaw ===\n");
    if (process.platform !== "win32") {
        console.log("This command is for Windows only. Use 'prepare-mac' on macOS.");
        return;
    }
    const useWinget = hasWinget();
    const useChoco = hasChoco();
    if (!useWinget && !useChoco) {
        console.log("Warning: Neither winget nor Chocolatey found.");
        console.log("winget comes with Windows 10 1709+ and Windows 11.");
        console.log("To install Chocolatey: https://chocolatey.org/install\n");
        console.log("Continuing with checks only (no automatic installation)...\n");
    }
    else {
        const pm = useWinget ? "winget" : "Chocolatey";
        console.log(`Package manager: ${pm}\n`);
    }
    // 1. PowerShell execution policy
    console.log("--- PowerShell Execution Policy ---");
    const policyOk = await checkPowerShellExecutionPolicy();
    if (policyOk) {
        console.log("  Execution policy allows scripts.\n");
    }
    else {
        console.log("  Execution policy is too restrictive.");
        const fixResult = (await prompts({
            type: "confirm",
            name: "fix",
            message: "Set execution policy to RemoteSigned for current user?",
            initial: true,
        }));
        if (fixResult.fix) {
            try {
                await execAsync('powershell -NoProfile -Command "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force"');
                console.log("  Execution policy updated.\n");
            }
            catch (e) {
                console.log(`  Failed: ${e instanceof Error ? e.message : String(e)}`);
                console.log("  Please run manually in an elevated PowerShell:\n");
                console.log("  Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser\n");
            }
        }
    }
    // 2. Chrome
    console.log("--- Google Chrome ---");
    const chromeOk = checkChromeInstalled();
    if (chromeOk) {
        console.log("  Chrome is installed.\n");
    }
    else {
        console.log("  Chrome not found.");
        const installResult = (await prompts({
            type: "confirm",
            name: "install",
            message: "Install Google Chrome?",
            initial: true,
        }));
        if (installResult.install) {
            await installPackage("Google.Chrome", "googlechrome", "Google Chrome", useWinget, useChoco);
        }
        console.log("");
    }
    // 3. ffmpeg
    console.log("--- ffmpeg ---");
    const ffmpegOk = await checkFfmpegInstalled();
    if (ffmpegOk) {
        console.log("  ffmpeg is installed.\n");
    }
    else {
        console.log("  ffmpeg not found.");
        const installResult = (await prompts({
            type: "confirm",
            name: "install",
            message: "Install ffmpeg?",
            initial: true,
        }));
        if (installResult.install) {
            await installPackage("Gyan.FFmpeg", "ffmpeg", "ffmpeg", useWinget, useChoco);
        }
        console.log("");
    }
    // 4. gh CLI
    console.log("--- GitHub CLI ---");
    const ghOk = await checkGhInstalled();
    if (ghOk) {
        console.log("  gh is installed.\n");
    }
    else {
        console.log("  gh not found.");
        const installResult = (await prompts({
            type: "confirm",
            name: "install",
            message: "Install GitHub CLI?",
            initial: true,
        }));
        if (installResult.install) {
            await installPackage("GitHub.cli", "gh", "GitHub CLI", useWinget, useChoco);
        }
        console.log("");
    }
    // 5. gcloud
    console.log("--- Google Cloud SDK ---");
    const gcloudOk = await checkGcloudInstalled();
    if (gcloudOk) {
        console.log("  gcloud is installed.\n");
    }
    else {
        console.log("  gcloud not found (optional — needed for automatic Google Cloud setup).");
        const installResult = (await prompts({
            type: "confirm",
            name: "install",
            message: "Install Google Cloud SDK?",
            initial: false,
        }));
        if (installResult.install) {
            await installPackage("Google.CloudSDK", "gcloudsdk", "Google Cloud SDK", useWinget, useChoco);
        }
        console.log("");
    }
    // 6. whisper-cli
    console.log("--- whisper-cli ---");
    const whisperOk = await checkWhisperCliInstalled();
    if (whisperOk) {
        console.log("  whisper-cli is installed.\n");
    }
    else {
        console.log("  whisper-cli not found (optional — needed for voice message transcription).");
        const installResult = (await prompts({
            type: "confirm",
            name: "install",
            message: "Install whisper-cli via npm?",
            initial: false,
        }));
        if (installResult.install) {
            try {
                await execAsync("npm install -g whisper-cli", { timeout: 120_000 });
                console.log("  whisper-cli installed successfully.");
            }
            catch (e) {
                console.log(`  Failed: ${e instanceof Error ? e.message : String(e)}`);
                console.log("  Install manually: npm install -g whisper-cli");
            }
        }
        console.log("");
    }
    // 7. cloudflared
    console.log("--- Cloudflared ---");
    const hasCf = spawnSync("cloudflared", ["--version"], { stdio: "ignore" }).status === 0;
    if (hasCf) {
        console.log("  cloudflared is installed.\n");
    }
    else {
        console.log("  cloudflared not found (optional — needed for remote OBS access).");
        const installResult = (await prompts({
            type: "confirm",
            name: "install",
            message: "Install cloudflared?",
            initial: false,
        }));
        if (installResult.install) {
            await installPackage("Cloudflare.cloudflared", "cloudflared", "cloudflared", useWinget, useChoco);
        }
        console.log("");
    }
    // 8. pm2
    console.log("--- pm2 (Process Manager) ---");
    const pm2Ok = await checkPm2Installed();
    if (pm2Ok) {
        console.log("  pm2 is installed.\n");
    }
    else {
        console.log("  pm2 not found (recommended for production).");
        const installResult = (await prompts({
            type: "confirm",
            name: "install",
            message: "Install pm2 globally via npm?",
            initial: true,
        }));
        if (installResult.install) {
            try {
                await execAsync("npm install -g pm2", { timeout: 120_000 });
                console.log("  pm2 installed successfully.");
            }
            catch (e) {
                console.log(`  Failed: ${e instanceof Error ? e.message : String(e)}`);
                console.log("  Install manually: npm install -g pm2");
            }
        }
        console.log("");
    }
    console.log("=== Windows preparation complete ===");
    console.log('Run "visionclaw setup" to start manufacturer setup.\n');
}
//# sourceMappingURL=prepare-windows.js.map