import { fileURLToPath as __fileURLToPath } from 'url';
import { dirname as __pathDirname } from 'path';
const __filename = __fileURLToPath(import.meta.url);
const __dirname = __pathDirname(__filename);
import { Command } from "commander";
import chalk from "chalk";
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "fs";
import { join } from "path";
import { homedir } from "os";
import { execSync } from "child_process";
const CLAUDE_DIR = join(homedir(), ".claude");
const CLAUDE_CONFIG_FILE = join(CLAUDE_DIR, "config.json");
const MCP_CONFIG_FILE = join(CLAUDE_DIR, "stackmemory-mcp.json");
const HOOKS_JSON = join(CLAUDE_DIR, "hooks.json");
function createSetupMCPCommand() {
  return new Command("setup-mcp").description("Auto-configure Claude Code MCP integration").option("--dry-run", "Show what would be configured without making changes").option("--reset", "Reset MCP configuration to defaults").action(async (options) => {
    console.log(chalk.cyan("\nStackMemory MCP Setup\n"));
    if (options.dryRun) {
      console.log(chalk.yellow("[DRY RUN] No changes will be made.\n"));
    }
    if (!existsSync(CLAUDE_DIR)) {
      if (options.dryRun) {
        console.log(chalk.gray(`Would create: ${CLAUDE_DIR}`));
      } else {
        mkdirSync(CLAUDE_DIR, { recursive: true });
        console.log(chalk.green("[OK]") + " Created ~/.claude directory");
      }
    }
    const mcpConfig = {
      mcpServers: {
        stackmemory: {
          command: "stackmemory",
          args: ["mcp-server"],
          env: {
            NODE_ENV: "production"
          }
        }
      }
    };
    if (options.dryRun) {
      console.log(
        chalk.gray(`Would write MCP config to: ${MCP_CONFIG_FILE}`)
      );
      console.log(chalk.gray(JSON.stringify(mcpConfig, null, 2)));
    } else {
      writeFileSync(MCP_CONFIG_FILE, JSON.stringify(mcpConfig, null, 2));
      console.log(chalk.green("[OK]") + " Created MCP server configuration");
    }
    let claudeConfig = {};
    if (existsSync(CLAUDE_CONFIG_FILE)) {
      try {
        claudeConfig = JSON.parse(readFileSync(CLAUDE_CONFIG_FILE, "utf8"));
      } catch {
        console.log(
          chalk.yellow("[WARN]") + " Could not parse existing config.json, creating new"
        );
      }
    }
    if (!claudeConfig.mcp) {
      claudeConfig.mcp = {};
    }
    const mcp = claudeConfig.mcp;
    if (!mcp.configFiles) {
      mcp.configFiles = [];
    }
    const configFiles = mcp.configFiles;
    if (!configFiles.includes(MCP_CONFIG_FILE)) {
      configFiles.push(MCP_CONFIG_FILE);
    }
    if (options.dryRun) {
      console.log(chalk.gray(`Would update: ${CLAUDE_CONFIG_FILE}`));
    } else {
      writeFileSync(
        CLAUDE_CONFIG_FILE,
        JSON.stringify(claudeConfig, null, 2)
      );
      console.log(chalk.green("[OK]") + " Updated Claude config.json");
    }
    console.log(chalk.cyan("\nValidating configuration..."));
    try {
      execSync("stackmemory --version", { stdio: "pipe" });
      console.log(chalk.green("[OK]") + " stackmemory CLI is installed");
    } catch {
      console.log(chalk.yellow("[WARN]") + " stackmemory CLI not in PATH");
      console.log(chalk.gray("  You may need to restart your terminal"));
    }
    try {
      execSync("claude --version", { stdio: "pipe" });
      console.log(chalk.green("[OK]") + " Claude Code is installed");
    } catch {
      console.log(chalk.yellow("[WARN]") + " Claude Code not found");
      console.log(chalk.gray("  Install from: https://claude.ai/code"));
    }
    if (!options.dryRun) {
      console.log(chalk.green("\nMCP setup complete!"));
      console.log(chalk.cyan("\nNext steps:"));
      console.log(chalk.white("  1. Restart Claude Code"));
      console.log(
        chalk.white(
          "  2. The StackMemory MCP tools will be available automatically"
        )
      );
      console.log(
        chalk.gray(
          '\nTo verify: Run "stackmemory doctor" to check all integrations'
        )
      );
    }
  });
}
function createDoctorCommand() {
  return new Command("doctor").description("Diagnose StackMemory configuration and common issues").option("--fix", "Attempt to automatically fix issues").action(async (options) => {
    console.log(chalk.cyan("\nStackMemory Doctor\n"));
    console.log(chalk.gray("Checking configuration and dependencies...\n"));
    const results = [];
    const projectDir = join(process.cwd(), ".stackmemory");
    const dbPath = join(projectDir, "context.db");
    if (existsSync(dbPath)) {
      results.push({
        name: "Project Initialization",
        status: "ok",
        message: "StackMemory is initialized in this project"
      });
    } else if (existsSync(projectDir)) {
      results.push({
        name: "Project Initialization",
        status: "warn",
        message: ".stackmemory directory exists but database not found",
        fix: "Run: stackmemory init"
      });
    } else {
      results.push({
        name: "Project Initialization",
        status: "error",
        message: "StackMemory not initialized in this project",
        fix: "Run: stackmemory init"
      });
    }
    if (existsSync(dbPath)) {
      try {
        const Database = (await import("better-sqlite3")).default;
        const db = new Database(dbPath, { readonly: true });
        const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table'").all();
        db.close();
        const hasFrames = tables.some((t) => t.name === "frames");
        if (hasFrames) {
          results.push({
            name: "Database Integrity",
            status: "ok",
            message: `Database has ${tables.length} tables`
          });
        } else {
          results.push({
            name: "Database Integrity",
            status: "warn",
            message: "Database exists but missing expected tables",
            fix: "Run: stackmemory init --interactive"
          });
        }
      } catch (error) {
        results.push({
          name: "Database Integrity",
          status: "error",
          message: `Database error: ${error.message}`,
          fix: "Remove .stackmemory/context.db and run: stackmemory init"
        });
      }
    }
    if (existsSync(MCP_CONFIG_FILE)) {
      try {
        const config = JSON.parse(readFileSync(MCP_CONFIG_FILE, "utf8"));
        if (config.mcpServers?.stackmemory) {
          results.push({
            name: "MCP Configuration",
            status: "ok",
            message: "MCP server configured"
          });
        } else {
          results.push({
            name: "MCP Configuration",
            status: "warn",
            message: "MCP config file exists but stackmemory server not configured",
            fix: "Run: stackmemory setup-mcp"
          });
        }
      } catch {
        results.push({
          name: "MCP Configuration",
          status: "error",
          message: "Invalid MCP configuration file",
          fix: "Run: stackmemory setup-mcp --reset"
        });
      }
    } else {
      results.push({
        name: "MCP Configuration",
        status: "warn",
        message: "MCP not configured for Claude Code",
        fix: "Run: stackmemory setup-mcp"
      });
    }
    if (existsSync(HOOKS_JSON)) {
      try {
        const hooks = JSON.parse(readFileSync(HOOKS_JSON, "utf8"));
        const hasTraceHook = !!hooks["tool-use-approval"];
        if (hasTraceHook) {
          results.push({
            name: "Claude Hooks",
            status: "ok",
            message: "Tool tracing hook installed"
          });
        } else {
          results.push({
            name: "Claude Hooks",
            status: "warn",
            message: "Hooks file exists but tracing not configured",
            fix: "Run: stackmemory hooks install"
          });
        }
      } catch {
        results.push({
          name: "Claude Hooks",
          status: "warn",
          message: "Could not read hooks.json"
        });
      }
    } else {
      results.push({
        name: "Claude Hooks",
        status: "warn",
        message: "Claude hooks not installed (optional)",
        fix: "Run: stackmemory hooks install"
      });
    }
    const envChecks = [
      { key: "LINEAR_API_KEY", name: "Linear API Key", optional: true },
      { key: "TWILIO_ACCOUNT_SID", name: "Twilio Account", optional: true }
    ];
    for (const check of envChecks) {
      const value = process.env[check.key];
      if (value) {
        results.push({
          name: check.name,
          status: "ok",
          message: "Environment variable set"
        });
      } else if (!check.optional) {
        results.push({
          name: check.name,
          status: "error",
          message: "Required environment variable not set",
          fix: `Set ${check.key} in your .env file`
        });
      }
    }
    const homeStackmemory = join(homedir(), ".stackmemory");
    if (existsSync(homeStackmemory)) {
      try {
        const testFile = join(homeStackmemory, ".write-test");
        writeFileSync(testFile, "test");
        const { unlinkSync } = await import("fs");
        unlinkSync(testFile);
        results.push({
          name: "File Permissions",
          status: "ok",
          message: "~/.stackmemory is writable"
        });
      } catch {
        results.push({
          name: "File Permissions",
          status: "error",
          message: "~/.stackmemory is not writable",
          fix: "Run: chmod 700 ~/.stackmemory"
        });
      }
    }
    let hasErrors = false;
    let hasWarnings = false;
    for (const result of results) {
      const icon = result.status === "ok" ? chalk.green("[OK]") : result.status === "warn" ? chalk.yellow("[WARN]") : chalk.red("[ERROR]");
      console.log(`${icon} ${result.name}`);
      console.log(chalk.gray(`    ${result.message}`));
      if (result.fix) {
        console.log(chalk.cyan(`    Fix: ${result.fix}`));
        if (options.fix && result.status !== "ok") {
          if (result.fix.includes("stackmemory setup-mcp")) {
            console.log(chalk.gray("    Attempting auto-fix..."));
            try {
              execSync("stackmemory setup-mcp", { stdio: "inherit" });
            } catch {
              console.log(chalk.red("    Auto-fix failed"));
            }
          }
        }
      }
      if (result.status === "error") hasErrors = true;
      if (result.status === "warn") hasWarnings = true;
    }
    console.log("");
    if (hasErrors) {
      console.log(
        chalk.red("Some issues need attention. Run suggested fixes above.")
      );
      process.exit(1);
    } else if (hasWarnings) {
      console.log(
        chalk.yellow(
          "StackMemory is working but some optional features are not configured."
        )
      );
    } else {
      console.log(
        chalk.green("All checks passed! StackMemory is properly configured.")
      );
    }
  });
}
function registerSetupCommands(program) {
  program.addCommand(createSetupMCPCommand());
  program.addCommand(createDoctorCommand());
}
export {
  createDoctorCommand,
  createSetupMCPCommand,
  registerSetupCommands
};
//# sourceMappingURL=setup.js.map
