#!/usr/bin/env node
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { readOntoVersion } from "./core-runtime/release-channel/release-channel.js";
/**
 * Public binary surface for the MCP-native product.
 *
 * Product tools are exposed through MCP (`onto mcp`). Review and other
 * runtime flows are no longer public `onto <activity>` CLI commands. The
 * `npm run review:*` scripts remain internal development harnesses.
 */
function loadOntoEnvFile(filePath) {
    if (!fs.existsSync(filePath))
        return;
    let contents;
    try {
        contents = fs.readFileSync(filePath, "utf8");
    }
    catch {
        return;
    }
    for (const rawLine of contents.split(/\r?\n/)) {
        const line = rawLine.trim();
        if (!line || line.startsWith("#"))
            continue;
        const eq = line.indexOf("=");
        if (eq === -1)
            continue;
        const key = line.slice(0, eq).trim();
        if (!key || process.env[key] !== undefined)
            continue;
        process.env[key] = line.slice(eq + 1);
    }
}
function printHelp() {
    console.log([
        "Usage: onto mcp",
        "",
        "Active interface:",
        "  mcp            Start the MCP stdio tool server",
        "",
        "Available MCP tools:",
        "  onto.review",
        "  onto.prepare_review",
        "  onto.review_status",
        "  onto.review_result",
        "  onto.list_lenses",
        "  onto.list_domains",
        "  onto.list_source_profiles",
        "  onto.observe_source",
        "  onto.validate_reconstruct_directive",
        "  onto.reconstruct",
        "  onto.reconstruct_status",
        "  onto.reconstruct_result",
        "",
        "Development harness:",
        "  npm run review:invoke -- <target> \"<intent>\"",
        "",
        "Options:",
        "  --version, -v  Show version",
        "  --help, -h     Show this help",
    ].join("\n"));
}
function unsupportedCommandMessage(subcommand) {
    return [
        `[onto] Unsupported public CLI subcommand: ${subcommand}`,
        "Active public command: onto mcp",
        "Development harness: npm run review:invoke -- <target> \"<intent>\"",
    ].join("\n");
}
async function main() {
    const argv = process.argv.slice(2);
    const subcommand = argv[0];
    loadOntoEnvFile(path.join(os.homedir(), ".onto", ".env"));
    loadOntoEnvFile(path.join(process.cwd(), ".onto", ".env"));
    switch (subcommand) {
        case "mcp": {
            const { startMcpServer } = await import("./mcp/server.js");
            return startMcpServer();
        }
        case "--version":
        case "-v": {
            const version = await readOntoVersion();
            console.log(`onto-mcp ${version}`);
            return 0;
        }
        case "--help":
        case "-h":
        case undefined:
            printHelp();
            return 0;
        default:
            console.error(unsupportedCommandMessage(subcommand));
            return 1;
    }
}
main().then((exitCode) => process.exit(exitCode), (error) => {
    console.error(error instanceof Error ? error.message : String(error));
    process.exit(1);
});
