/**
 * Cursor command - manage Cursor IDE rules installation
 *
 * Usage:
 *   quickback cursor install           # Install to .cursor/rules/quickback.mdc
 *   quickback cursor update            # Update to latest version
 *   quickback cursor remove            # Remove installed rules
 *   quickback cursor status            # Check installation status
 */
import { copyFileSync, mkdirSync, existsSync, rmSync } from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import pc from "picocolors";
import prompts from "prompts";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Paths
const CURSOR_BUNDLE = join(__dirname, "..", "cursor");
const RULES_DIR = join(process.cwd(), ".cursor", "rules");
const RULES_FILE = join(RULES_DIR, "quickback.mdc");
function checkBundledRules() {
    const mdcFile = join(CURSOR_BUNDLE, "quickback.mdc");
    if (!existsSync(mdcFile)) {
        console.log(pc.red("\nCursor rules not found in CLI bundle."));
        console.log(pc.dim("This may indicate an incomplete CLI installation."));
        console.log(pc.dim("Try reinstalling: npm install -g @quickback-dev/cli\n"));
        return false;
    }
    return true;
}
function installRules() {
    mkdirSync(RULES_DIR, { recursive: true });
    const src = join(CURSOR_BUNDLE, "quickback.mdc");
    copyFileSync(src, RULES_FILE);
    console.log(pc.green(`  ✓ quickback.mdc → ${RULES_DIR}`));
    console.log("");
    console.log(pc.green("✓ Quickback rules installed for Cursor IDE"));
    console.log("");
    console.log(pc.bold("The rules will automatically activate in Cursor for quickback/**/*.ts files."));
    console.log(pc.dim("Consider committing .cursor/rules/ to your repository.\n"));
}
async function install(args) {
    if (!checkBundledRules()) {
        process.exit(1);
    }
    // Check for existing installation
    if (existsSync(RULES_FILE)) {
        const response = await prompts({
            type: "confirm",
            name: "overwrite",
            message: "Cursor rules already installed. Overwrite?",
            initial: true,
        });
        if (!response.overwrite) {
            console.log(pc.dim("\nInstallation cancelled.\n"));
            return;
        }
    }
    console.log("");
    console.log(pc.bold("Installing Quickback rules for Cursor IDE"));
    console.log("");
    installRules();
}
async function update(args) {
    if (!checkBundledRules()) {
        process.exit(1);
    }
    if (!existsSync(RULES_FILE)) {
        console.log(pc.yellow("\nNo existing installation found."));
        console.log(pc.dim("Run 'quickback cursor install' to install.\n"));
        return;
    }
    console.log("");
    console.log(pc.bold("Updating Quickback Cursor rules"));
    console.log("");
    installRules();
}
async function remove(args) {
    if (!existsSync(RULES_FILE)) {
        console.log(pc.yellow("\nNo installation found to remove.\n"));
        return;
    }
    console.log("");
    console.log(pc.bold("Remove Quickback Cursor rules"));
    console.log("");
    rmSync(RULES_FILE, { force: true });
    console.log(pc.yellow(`  ✓ Removed ${RULES_FILE}`));
    console.log("");
    console.log(pc.green("✓ Quickback Cursor rules removed.\n"));
}
async function status() {
    console.log("");
    console.log(pc.bold("Quickback Cursor IDE Rules"));
    console.log("");
    if (existsSync(RULES_FILE)) {
        console.log(`  ${pc.green("●")} Installed`);
        console.log(pc.dim(`    ${RULES_FILE}`));
    }
    else {
        console.log(`  ${pc.dim("○")} Not installed`);
    }
    console.log("");
    if (!existsSync(RULES_FILE)) {
        console.log(pc.dim("Run 'quickback cursor install' to install the rules.\n"));
    }
}
function printHelp() {
    console.log(`
${pc.bold("Quickback Cursor IDE Rules")}

${pc.bold("USAGE")}
  quickback cursor <command>

${pc.bold("COMMANDS")}
  ${pc.cyan("install")}    Install Cursor rules to .cursor/rules/
  ${pc.cyan("update")}     Update to the latest version
  ${pc.cyan("remove")}     Remove the installed rules
  ${pc.cyan("status")}     Check installation status

${pc.bold("EXAMPLES")}
  ${pc.gray("# Install rules")}
  quickback cursor install

  ${pc.gray("# Check status")}
  quickback cursor status
`);
}
export async function cursor(args) {
    const subcommand = args[0];
    const subArgs = args.slice(1);
    switch (subcommand) {
        case "install":
            await install(subArgs);
            break;
        case "update":
            await update(subArgs);
            break;
        case "remove":
        case "uninstall":
            await remove(subArgs);
            break;
        case "status":
            await status();
            break;
        case "--help":
        case "-h":
        case undefined:
            printHelp();
            break;
        default:
            console.log(pc.red(`\nUnknown subcommand: ${subcommand}`));
            printHelp();
            process.exit(1);
    }
}
//# sourceMappingURL=cursor.js.map