#!/usr/bin/env node
import { createRequire } from "node:module";
import { program } from "commander";
import { areSkillsInstalled, getActiveProjectId, getActiveWorkspaceId, getConfigPath, getLocalConfigPath, hasLocalConfig, isConfigured, loadConfig, loadLocalConfig, saveConfig, } from "./config.js";
import { HarmonyMCPServer } from "./server.js";
import { refreshSkills } from "./skills.js";
import { runSetup } from "./tui/setup.js";
const require = createRequire(import.meta.url);
const { version } = require("../package.json");
program
    .name("@gethmy/mcp")
    .description("MCP server for Harmony Kanban board")
    .version(version);
program
    .command("serve")
    .description("Start the MCP server (stdio transport)")
    .action(async () => {
    await refreshSkills();
    const server = new HarmonyMCPServer();
    await server.run();
});
program
    .command("status")
    .description("Show configuration status")
    .action(() => {
    const globalConfig = loadConfig();
    const localConfig = loadLocalConfig();
    const hasLocal = hasLocalConfig();
    const skillsStatus = areSkillsInstalled();
    // Helper to mask email for privacy (u***@example.com)
    const maskEmail = (email) => {
        const [local, domain] = email.split("@");
        if (!domain)
            return email;
        return `${local[0]}***@${domain}`;
    };
    if (isConfigured()) {
        console.log("Status: Configured\n");
        console.log("API:");
        console.log(`  Key: ${globalConfig.apiKey?.slice(0, 8)}...`);
        console.log(`  URL: ${globalConfig.apiUrl}`);
        console.log(`  Email: ${globalConfig.userEmail ? maskEmail(globalConfig.userEmail) : "(not set)"}`);
        console.log("\nSkills:");
        if (skillsStatus.installed) {
            console.log(`  Installed: Yes (${skillsStatus.location})`);
            for (const path of skillsStatus.paths) {
                console.log(`    ${path}`);
            }
        }
        else {
            console.log("  Installed: No");
            console.log("  Run: npx @gethmy/mcp setup");
        }
        console.log("\nContext:");
        if (hasLocal) {
            console.log(`  Local config: ${getLocalConfigPath()}`);
            console.log(`    Workspace: ${localConfig?.workspaceId || "(not set)"}`);
            console.log(`    Project: ${localConfig?.projectId || "(not set)"}`);
        }
        console.log(`  Global config: ${getConfigPath()}`);
        console.log(`    Workspace: ${globalConfig.activeWorkspaceId || "(not set)"}`);
        console.log(`    Project: ${globalConfig.activeProjectId || "(not set)"}`);
        // Show effective (active) context
        const effectiveWorkspace = getActiveWorkspaceId();
        const effectiveProject = getActiveProjectId();
        const wsSource = localConfig?.workspaceId
            ? "local"
            : globalConfig.activeWorkspaceId
                ? "global"
                : "";
        const projSource = localConfig?.projectId
            ? "local"
            : globalConfig.activeProjectId
                ? "global"
                : "";
        console.log("\n  Active (effective):");
        console.log(`    Workspace: ${effectiveWorkspace || "(not set)"}${wsSource ? ` ← ${wsSource}` : ""}`);
        console.log(`    Project: ${effectiveProject || "(not set)"}${projSource ? ` ← ${projSource}` : ""}`);
    }
    else {
        console.log("Status: Not configured\n");
        console.log("Run: npx @gethmy/mcp setup");
        console.log("Get an API key at: https://app.gethmy.com/user/keys");
    }
});
program
    .command("reset")
    .description("Remove stored configuration")
    .action(() => {
    saveConfig({
        apiKey: null,
        activeWorkspaceId: null,
        activeProjectId: null,
        userEmail: null,
    });
    console.log("Configuration reset successfully");
    console.log("\nTo reconfigure, run: npx @gethmy/mcp setup");
});
program
    .command("setup")
    .description("Smart setup wizard for Harmony MCP (recommended)")
    .option("-f, --force", "Overwrite existing configuration files")
    .option("-k, --api-key <key>", "API key (skips prompt)")
    .option("-e, --email <email>", "Your email for auto-assignment")
    .option("-a, --agents <agents...>", "Agents to configure: claude, codex, cursor, windsurf")
    .option("-l, --local", "Install skills locally in project directory")
    .option("-g, --global", "Install skills globally (recommended)")
    .option("-w, --workspace <id>", "Set workspace context")
    .option("-p, --project <id>", "Set project context")
    .option("--skip-context", "Skip workspace/project selection")
    .option("--skip-docs", "Skip project docs scaffold/verification")
    .action(async (options) => {
    await runSetup({
        force: options.force,
        apiKey: options.apiKey,
        userEmail: options.email,
        agents: options.agents,
        installMode: options.global
            ? "global"
            : options.local
                ? "local"
                : undefined,
        workspaceId: options.workspace,
        projectId: options.project,
        skipContext: options.skipContext,
        skipDocs: options.skipDocs,
    });
});
program.parse();
