import { init_agent_scope, resolveAgentProfileDir, resolveDefaultAgentId } from "../../agent/agent-scope.js";
import { WORKSPACE_FILES, init_paths } from "../../config/paths.js";
import { ConfigSchema, init_schema } from "../../config/schema.js";
import { saveConfig } from "../../config/loader.js";
import { resolveGatewayLocalClientHost } from "../../config/gateway-bind.js";
import { isWeixinOnboardConfigured } from "../../../extensions/weixin/src/adapters/onboard-cli.js";
import { formatExamples, register } from "../registry.js";
import "../../config/index.js";
import { seedMainAgentProfileMarkdown } from "../../agent/context/workspace-seed.js";
import { colors } from "../utils/colors.js";
import { initWorkspace } from "../utils/init-workspace.js";
import { setupModel } from "./onboard/model.js";
import { getChannelConfigurators } from "./onboard/channels/registry.js";
import { setupChannels } from "./onboard/channels/index.js";
import { join } from "path";
import { select } from "@inquirer/prompts";
import { Command } from "commander";
//#region src/cli/commands/onboard.ts
init_agent_scope();
init_paths();
init_schema();
function isInteractive() {
	return process.stdin.isTTY && process.stdout.isTTY;
}
async function setupNonInteractive(_configPath, existingConfig) {
	console.log("\n🤖 AI Model Configuration (Non-Interactive Mode)\n");
	console.log("Current config:", JSON.stringify(existingConfig.agents?.defaults?.model, null, 2));
	console.log("\n💡 To configure in interactive mode, run: xopc onboard");
	console.log("💡 Or set up manually in:", _configPath);
	return existingConfig;
}
function createOnboardCommand(ctx) {
	return new Command("onboard").description("Interactive setup wizard for xopc (gateway uses schema defaults)").addHelpText("after", formatExamples([
		"xopc onboard              # Full interactive setup",
		"xopc onboard --model      # Configure LLM model only",
		"xopc onboard --channels   # Configure channels (incl. Weixin QR)",
		"xopc onboard --gateway    # Apply default gateway settings (quiet)"
	])).option("--model", "Configure LLM provider and model").option("--channels", "Configure messaging channels").option("--gateway", "Configure gateway WebUI").option("--all", "Configure everything (default)").action(async (options) => {
		try {
			await runOnboard(options, ctx);
		} catch (error) {
			const err = error;
			if (err?.name === "ExitPromptError" || err?.code === "EXIT_PROMPT") {
				console.log("\n\n👋 Setup cancelled.");
				process.exit(0);
			}
			throw error;
		}
	});
}
async function runOnboard(options, ctx) {
	console.log(colors.cyan("\n🚀 Welcome to xopc setup!\n"));
	console.log("═".repeat(50));
	const workspacePath = ctx.workspacePath;
	const configPath = ctx.configPath;
	let config = (await initWorkspace({
		configPath,
		workspacePath
	})).config;
	const doModel = options.model || options.all || !options.channels && !options.gateway;
	const doChannels = options.channels || options.all || !options.model && !options.gateway;
	const doGateway = options.gateway || options.all || !options.model && !options.channels;
	const runFullWizard = !options.model && !options.channels && !options.gateway;
	/** Any setup step besides the unified launch prompt ran in interactive flow. */
	const didConfigurableSteps = doModel || doChannels || doGateway;
	if (!isInteractive()) {
		if (doModel) config = await setupNonInteractive(configPath, config);
		if (doChannels) {
			console.log("\n💬 Channels Configuration (Non-Interactive Mode)\n");
			console.log("💡 To configure channels, edit the config file manually.");
		}
		if (doGateway) {
			console.log("\n🌐 Gateway Configuration (Non-Interactive Mode)\n");
			console.log("💡 To configure gateway, edit the config file manually.");
		}
	} else {
		if (doModel) config = await setupModel(config, ctx);
		if (doChannels) {
			const channelIds = getChannelConfigurators().map((c) => c.id);
			console.log(colors.gray(`\nChannel onboarding: ${channelIds.join(", ")}\n`));
			config = await setupChannels(config);
		}
		if (doGateway) config = await setupGateway(config);
	}
	await saveConfig(config, configPath);
	seedMainAgentProfileMarkdown(config);
	console.log("\n" + "═".repeat(50));
	console.log("\n🎉 Setup Complete!\n");
	const gatewayAuth = config?.gateway?.auth;
	const gatewayConfigured = gatewayAuth?.mode === "token" && typeof gatewayAuth?.token === "string" && gatewayAuth.token.length > 0;
	const port = config?.gateway?.port ?? 18790;
	const displayHost = resolveGatewayLocalClientHost(config);
	const gwToken = gatewayConfigured ? gatewayAuth.token : void 0;
	if (Boolean(gatewayConfigured && gwToken && (doGateway || runFullWizard)) && gwToken) {
		const webuiUrl = `http://${displayHost}:${port}?token=${gwToken}`;
		console.log("🌐 Web console (browser) — start here");
		console.log(`   Open: http://${displayHost}:${port}`);
		console.log(`   Token: ${gwToken.slice(0, 8)}...${gwToken.slice(-8)}`);
		console.log("   Bookmark link (token is saved in the browser when you open it):");
		console.log(`   ${webuiUrl}`);
		console.log("");
	}
	if (runFullWizard) {
		console.log("🚀 Next steps:");
		if (gatewayConfigured) {
			console.log("  1. Choose how to launch below (gateway or terminal UI)");
			console.log("  2. Or chat with: xopc agent -i");
			console.log("  3. Optional: read BOOTSTRAP.md in your workspace for workspace tips");
		} else {
			console.log("  1. Chat in the terminal: xopc agent -i");
			console.log("  2. Optional: add the Web console: xopc onboard --gateway");
			console.log("  3. Optional: read BOOTSTRAP.md in your workspace");
		}
		console.log("");
	} else if (doGateway && gatewayConfigured) {
		console.log("🚀 Next step:");
		console.log("  Start the gateway if it is not running, then open the Web console URL above.");
		console.log("");
	}
	console.log("📝 Usage:");
	console.log("  xopc agent -m \"Hello\"    # Chat with AI");
	console.log("  xopc agent -i            # Interactive mode");
	console.log("  xopc models list         # List models");
	console.log("  xopc config validate     # Validate xopc.json");
	console.log("  xopc auth list           # View authentication");
	console.log("  xopc init                # Full state dirs (if upgrading or missing data)");
	console.log("\n📁 Files:");
	console.log("  Config:", configPath);
	console.log("  Workspace:", workspacePath);
	if (runFullWizard) console.log("  BOOTSTRAP.md (origin story):", join(resolveAgentProfileDir(config, resolveDefaultAgentId(config)), WORKSPACE_FILES.BOOTSTRAP));
	if (isInteractive() && didConfigurableSteps) await promptLaunchAfterOnboard(config, ctx, { doChannels });
	process.exit(0);
}
async function startGatewayAsService(config, ctx) {
	const { startGatewayNow } = await import("./onboard/gateway.js");
	await startGatewayNow(config, ctx);
}
async function promptLaunchAfterOnboard(config, ctx, flags) {
	console.log("");
	const choice = await select({
		message: "How do you want to launch xopc now?",
		choices: [
			{
				value: "tui",
				name: "Terminal UI (embedded)",
				description: "xopc tui --local — no gateway process required"
			},
			{
				value: "gateway",
				name: "Gateway WebUI (OS service)",
				description: "Install and start the HTTP gateway for the browser console"
			},
			{
				value: "none",
				name: "Exit — I will start manually",
				description: "Finish setup without starting a runtime"
			}
		],
		default: "tui"
	});
	if (choice === "gateway") {
		await startGatewayAsService(config, ctx);
		return;
	}
	if (choice === "tui") {
		if (flags.doChannels && !isWeixinOnboardConfigured(config)) console.log(colors.gray("\n💡 Weixin is not logged in yet. When ready run: xopc channels login --channel weixin\n"));
		const { runTui } = await import("../../tui/tui.js");
		await runTui({ local: true });
		return;
	}
	console.log("\n⏭️  You can start later:");
	console.log("   xopc gateway service install");
	console.log("   xopc gateway");
	console.log("   xopc tui --local");
}
async function setupGateway(config) {
	console.log(colors.cyan("\n🌐 Gateway WebUI\n"));
	console.log(colors.gray("Applying defaults from config schema (127.0.0.1:18790, token auth; token generated if missing).\n"));
	const gw = config.gateway ?? {};
	const { randomBytes } = await import("node:crypto");
	const authMode = gw.auth?.mode === "none" ? "none" : "token";
	const token = authMode === "token" ? typeof gw.auth?.token === "string" && gw.auth.token.length > 0 ? gw.auth.token : randomBytes(24).toString("hex") : void 0;
	const merged = {
		...config,
		gateway: {
			...gw,
			bind: gw.bind ?? "loopback",
			port: gw.port ?? 18790,
			auth: authMode === "none" ? { mode: "none" } : {
				mode: "token",
				token
			}
		}
	};
	const parsed = ConfigSchema.parse(merged);
	console.log("✅ Gateway defaults applied.\n");
	return parsed;
}
register({
	id: "onboard",
	name: "onboard",
	description: "Interactive setup wizard",
	factory: createOnboardCommand,
	metadata: {
		category: "setup",
		examples: [
			"xopc onboard",
			"xopc onboard --model",
			"xopc onboard --channels"
		]
	}
});
//#endregion
export {};

//# sourceMappingURL=onboard.js.map