import crypto from "node:crypto";
import { configExists, ensureConfigDir, saveOwnerConfig, ownerConfigExists, } from "../config/index.js";
import { DEFAULT_HEARTBEAT_INTERVAL_MS } from "../config/types.js";
export function generateVerificationCode() {
    const chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
    let code = "";
    const bytes = crypto.randomBytes(6);
    for (let i = 0; i < 6; i++) {
        code += chars[bytes[i] % chars.length];
    }
    return code;
}
/**
 * CLI command: visionclaw set-owner --email <email> --name <name> [--dual-session]
 *
 * Writes a partial owner.json with the owner's email, name, and a verification code.
 * The agent will send an onboarding invitation email on next startup.
 */
export function runSetOwner(args) {
    if (!configExists()) {
        console.error("Error: No manufacturer config found. Run the agent first to complete manufacturer setup.");
        process.exit(1);
    }
    const emailIdx = args.indexOf("--email");
    const email = emailIdx !== -1 && emailIdx + 1 < args.length
        ? args[emailIdx + 1]
        : undefined;
    if (!email?.includes("@")) {
        console.error("Usage: visionclaw set-owner --email <owner-email> --name <owner-name> [--dual-session]");
        process.exit(1);
    }
    const nameIdx = args.indexOf("--name");
    const ownerName = nameIdx !== -1 && nameIdx + 1 < args.length
        ? args[nameIdx + 1]
        : undefined;
    if (!ownerName?.trim()) {
        console.error("Usage: visionclaw set-owner --email <owner-email> --name <owner-name> [--dual-session]");
        process.exit(1);
    }
    const dualSession = args.includes("--dual-session");
    if (ownerConfigExists()) {
        console.error("Error: owner.json already exists for this profile. Owner has already been set.");
        process.exit(1);
    }
    ensureConfigDir();
    const code = generateVerificationCode();
    const ownerConfig = {
        ownerName: ownerName.trim(),
        ownerEmail: email,
        allowedDiscordChannelIds: [],
        heartbeatIntervalMs: DEFAULT_HEARTBEAT_INTERVAL_MS,
        dualSessionEnabled: dualSession,
        androidUseEnabled: false,
        chatMonitorEnabled: true,
        onboardingComplete: false,
        onboardingCodeVerified: false,
        onboardingCode: code,
        silentCompaction: true,
        language: "en",
    };
    saveOwnerConfig(ownerConfig);
    console.log(`\nOwner name set to: ${ownerName.trim()}`);
    console.log(`Owner email set to: ${email}`);
    console.log(`Dual session: ${dualSession ? "enabled" : "disabled"}`);
    console.log(`Verification code: ${code}`);
    console.log("\nStart the agent to send the onboarding invitation email to the owner.");
}
//# sourceMappingURL=set-owner.js.map