/**
 * Windows prerequisite checks — equivalent of macos-permissions.ts.
 *
 * Windows does not require TCC permissions for SendInput, so this is
 * primarily about checking that required tools are installed and accessible.
 */
import { exec } from "node:child_process";
import { promisify } from "node:util";
import fs from "node:fs";
import path from "node:path";
const execAsync = promisify(exec);
async function commandExists(name) {
    try {
        const { stdout } = await execAsync(`where ${name}`);
        return stdout.trim().length > 0;
    }
    catch {
        return false;
    }
}
export function checkChromeInstalled() {
    const candidates = [
        path.join(process.env.PROGRAMFILES ?? "C:\\Program Files", "Google", "Chrome", "Application", "chrome.exe"),
        path.join(process.env["PROGRAMFILES(X86)"] ?? "C:\\Program Files (x86)", "Google", "Chrome", "Application", "chrome.exe"),
        path.join(process.env.LOCALAPPDATA ?? "", "Google", "Chrome", "Application", "chrome.exe"),
    ];
    for (const p of candidates) {
        if (fs.existsSync(p))
            return true;
    }
    return false;
}
export async function checkFfmpegInstalled() {
    if (await commandExists("ffmpeg"))
        return true;
    const candidates = [
        "C:\\ProgramData\\chocolatey\\bin\\ffmpeg.exe",
        "C:\\ffmpeg\\bin\\ffmpeg.exe",
    ];
    for (const c of candidates) {
        if (fs.existsSync(c))
            return true;
    }
    return false;
}
export async function checkGhInstalled() {
    return commandExists("gh");
}
export async function checkGhAuthenticated() {
    try {
        const result = await execAsync("gh auth status 2>&1");
        return !result.stdout.includes("not logged");
    }
    catch {
        return false;
    }
}
export async function checkGcloudInstalled() {
    return commandExists("gcloud");
}
export async function checkWhisperCliInstalled() {
    // On Windows, npm sometimes doesn't create bin wrappers, so check the module directly
    if (await commandExists("whisper"))
        return true;
    // Fallback: check if the npm module is installed
    try {
        const npmPrefix = process.env.APPDATA
            ? path.join(process.env.APPDATA, "npm", "node_modules", "whisper-cli")
            : "";
        if (npmPrefix && fs.existsSync(npmPrefix))
            return true;
    }
    catch {
        // ignore
    }
    return false;
}
export async function checkPm2Installed() {
    return commandExists("pm2");
}
export async function checkPowerShellExecutionPolicy() {
    try {
        const { stdout } = await execAsync('powershell -NoProfile -Command "Get-ExecutionPolicy"');
        const policy = stdout.trim().toLowerCase();
        return policy !== "restricted" && policy !== "allsigned";
    }
    catch {
        return false;
    }
}
/**
 * Run all Windows prerequisite checks and report results.
 * Returns true if all required prerequisites are met.
 */
export async function checkWindowsPrerequisites() {
    if (process.platform !== "win32") {
        return true;
    }
    console.log("--- Windows Prerequisites Check ---\n");
    let allGood = true;
    // PowerShell execution policy
    process.stdout.write("  PowerShell policy: ");
    const policyOk = await checkPowerShellExecutionPolicy();
    if (policyOk) {
        console.log("OK (scripts allowed)");
    }
    else {
        console.log("RESTRICTED");
        console.log('\n  To fix: Run PowerShell as Admin and execute:');
        console.log('  Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser');
        allGood = false;
    }
    // Chrome
    process.stdout.write("  Chrome:            ");
    const chromeOk = checkChromeInstalled();
    if (chromeOk) {
        console.log("Found");
    }
    else {
        console.log("NOT FOUND");
        console.log("\n  Install Google Chrome from https://www.google.com/chrome/");
        allGood = false;
    }
    // ffmpeg
    process.stdout.write("  ffmpeg:            ");
    const ffmpegOk = await checkFfmpegInstalled();
    if (ffmpegOk) {
        console.log("Installed");
    }
    else {
        console.log("NOT INSTALLED");
        console.log("\n  To install: winget install ffmpeg  OR  choco install ffmpeg");
        allGood = false;
    }
    // gh CLI
    process.stdout.write("  gh (GitHub CLI):   ");
    const ghOk = await checkGhInstalled();
    if (ghOk) {
        const ghAuth = await checkGhAuthenticated();
        if (ghAuth) {
            console.log("Installed & Authenticated");
        }
        else {
            console.log("INSTALLED but NOT AUTHENTICATED");
            console.log("\n  To authenticate: gh auth login");
            allGood = false;
        }
    }
    else {
        console.log("NOT INSTALLED");
        console.log("\n  To install: winget install GitHub.cli");
        console.log("  Then authenticate: gh auth login");
        allGood = false;
    }
    // gcloud (optional but checked)
    process.stdout.write("  gcloud:            ");
    const gcloudOk = await checkGcloudInstalled();
    if (gcloudOk) {
        console.log("Installed");
    }
    else {
        console.log("NOT INSTALLED (optional — needed for automatic Google Cloud setup)");
        console.log("\n  To install: winget install Google.CloudSDK");
    }
    // whisper-cli (optional)
    process.stdout.write("  whisper-cli:       ");
    const whisperOk = await checkWhisperCliInstalled();
    if (whisperOk) {
        console.log("Installed");
    }
    else {
        console.log("NOT INSTALLED (optional — needed for voice message transcription)");
    }
    // pm2
    process.stdout.write("  pm2:               ");
    const pm2Ok = await checkPm2Installed();
    if (pm2Ok) {
        console.log("Installed");
    }
    else {
        console.log("NOT INSTALLED (recommended for production)");
        console.log("\n  To install: npm install -g pm2");
    }
    console.log("");
    if (!allGood) {
        console.log("  Some prerequisites are missing. VisionClaw needs these for full functionality.");
        console.log("  You can continue setup now and install them later, but some features");
        console.log("  will not work until the prerequisites are met.\n");
    }
    else {
        console.log("  All prerequisites met!\n");
    }
    return allGood;
}
//# sourceMappingURL=windows-permissions.js.map