#!/usr/bin/env node
/**
 * Quickback CLI
 *
 * A thin client that calls the remote compiler API.
 * All compilation logic lives on the server - this CLI just handles:
 * - File I/O (reading config, writing output)
 * - User interaction (prompts, spinners)
 * - Authentication (login, logout)
 */
import pc from "picocolors";
// Version injected at build time by scripts/inject-version.ts
const CLI_VERSION = "0.34.1";
function getPackageVersion() {
    return CLI_VERSION;
}
// Print version information
function printVersion() {
    const version = getPackageVersion();
    console.log(`@quickback/cli v${version}`);
}
// ASCII logo
const logo = `
     ..######
   .########.
  ##########
 ###########
###########
###########
       ###########
       ###########
      ###########
      ##########
     .########.
     ######..
`;
// Print help information
function printHelp() {
    const version = getPackageVersion();
    const help = `${pc.cyan(logo)}
  ${pc.bold("quickback.dev")}

${pc.bold("Quickback CLI")} v${version}

${pc.bold("USAGE")}
  quickback <command> [options]

${pc.bold("COMMANDS")}
  ${pc.cyan("start")}                      Interactive onboarding — scaffold + compile in one shot
  ${pc.cyan("create")} <template> <name>   Scaffold a new project from a template (scriptable)
  ${pc.cyan("example")} <name> [dir]       Extract a pre-compiled example project
  ${pc.cyan("compile")}                    Compile definitions via remote API
  ${pc.cyan("migrations")} <subcommand>    Inspect/repair drizzle migration state (try: doctor)
  ${pc.cyan("docs")} [topic]               Show built-in documentation
  ${pc.cyan("claude")} <command>           Manage Claude Code skill
  ${pc.cyan("cursor")} <command>           Manage Cursor IDE rules
  ${pc.cyan("mcp")}                       Start MCP server for AI tools
  ${pc.cyan("login")}                      Authenticate for Pro templates
  ${pc.cyan("logout")}                     Clear stored credentials
  ${pc.cyan("whoami")}                     Show current auth status

${pc.bold("OPTIONS")}
  ${pc.cyan("-v, --version")}              Show version number
  ${pc.cyan("-h, --help")}                 Show this help message

${pc.bold("EXAMPLES")}
  ${pc.gray("# Brand new? One command in an empty folder.")}
  npx @quickback-dev/cli start

  ${pc.gray("# Or scriptable:")}
  quickback create cloudflare my-app

  ${pc.gray("# Single-tenant blog")}
  quickback create blog my-site

  ${pc.gray("# Try a pre-compiled example (no cloud compile needed)")}
  quickback example recruitment

  ${pc.gray("# Compile existing project")}
  cd my-app && quickback compile

  ${pc.gray("# List available templates")}
  quickback create --list

  ${pc.gray("# View documentation")}
  quickback docs firewall

  ${pc.gray("# Install Claude Code skill")}
  quickback claude install

  ${pc.gray("# Install Cursor IDE rules")}
  quickback cursor install

  ${pc.gray("# Start MCP server")}
  quickback mcp

${pc.bold("TEMPLATES")}
  ${pc.cyan("cloudflare")}   Cloudflare Workers + D1 + Better Auth (free, bare scaffold)
  ${pc.cyan("todos")}        Working todos example with masking + actions (free)
  ${pc.cyan("blog")}         Single-tenant blog — PUBLIC reads, admin writes (free)
  ${pc.cyan("saas")}         Full B2B SaaS: orgs, R2 file storage, webhooks (pro)
  ${pc.cyan("empty")}        Empty scaffold — config only, no features (free)

${pc.bold("DOCUMENTATION")}
  https://quickback.dev/docs
`;
    console.log(help);
}
// Fatal error handler
function fatal(message) {
    console.error(pc.red(`\n✖ ${message}`));
    process.exit(1);
}
// Main command routing
const cmd = process.argv[2];
const args = process.argv.slice(3);
// Check for version flag
if (cmd === "version" || cmd === "--version" || cmd === "-v") {
    printVersion();
}
else if (cmd === "help" || cmd === "--help" || cmd === "-h" || !cmd) {
    printHelp();
}
else if (cmd === "init") {
    // `init` was removed in v0.17 — its legacy `definitions/features/<name>/`
    // layout no longer matches the compiler's `features/<name>/<name>.ts`
    // (defineTable) expectation, and the message it printed pointed at a
    // command that doesn't exist. `start` and `create` cover the same path
    // with a working scaffold.
    console.log(pc.yellow("\n'quickback init' has been removed."));
    console.log(pc.gray("\nUse one of these instead:\n"));
    console.log(pc.cyan("  quickback start           ") + pc.gray("Interactive — scaffold + compile in one shot"));
    console.log(pc.cyan("  quickback create cloudflare <name>") + pc.gray("  Scriptable scaffold"));
    console.log("");
    process.exit(0);
}
else if (cmd === "start") {
    import("./commands/start.js").then(({ start }) => start(args)).catch((err) => {
        fatal(String(err?.message ?? err));
    });
}
else if (cmd === "create") {
    import("./commands/create.js").then(({ create }) => create(args)).catch((err) => {
        fatal(String(err?.message ?? err));
    });
}
else if (cmd === "example") {
    import("./commands/example.js").then(({ example }) => example(args)).catch((err) => {
        fatal(String(err?.message ?? err));
    });
}
else if (cmd === "compile") {
    const noSyncConfig = args.includes("--no-sync-config");
    import("./commands/compile.js").then(({ compile }) => compile({ syncConfig: !noSyncConfig })).catch((err) => {
        fatal(String(err?.message ?? err));
    });
}
else if (cmd === "migrations") {
    import("./commands/migrations.js").then(({ migrations }) => migrations(args)).catch((err) => {
        fatal(String(err?.message ?? err));
    });
}
else if (cmd === "login") {
    import("./commands/login.js").then(({ login }) => login()).catch((err) => {
        fatal(String(err?.message ?? err));
    });
}
else if (cmd === "logout") {
    import("./commands/logout.js").then(({ logout }) => logout()).catch((err) => {
        fatal(String(err?.message ?? err));
    });
}
else if (cmd === "whoami") {
    import("./commands/whoami.js").then(({ whoami }) => whoami()).catch((err) => {
        fatal(String(err?.message ?? err));
    });
}
else if (cmd === "docs") {
    import("./commands/docs.js").then(({ docs }) => docs(args)).catch((err) => {
        fatal(String(err?.message ?? err));
    });
}
else if (cmd === "claude") {
    import("./commands/claude.js").then(({ claude }) => claude(args)).catch((err) => {
        fatal(String(err?.message ?? err));
    });
}
else if (cmd === "cursor") {
    import("./commands/cursor.js").then(({ cursor }) => cursor(args)).catch((err) => {
        fatal(String(err?.message ?? err));
    });
}
else if (cmd === "mcp") {
    import("./commands/mcp.js").then(({ mcp }) => mcp()).catch((err) => {
        fatal(String(err?.message ?? err));
    });
}
else if (cmd === "db:generate" || cmd === "db:migrate" || cmd === "db:studio" || cmd === "migrate") {
    // Legacy database commands - now handled via drizzle-kit directly
    console.log(pc.yellow(`\nThe '${cmd}' command has been deprecated.`));
    console.log(pc.gray("\nUse drizzle-kit directly in your project:\n"));
    console.log(pc.cyan("  npx drizzle-kit generate    # Generate migrations"));
    console.log(pc.cyan("  npx drizzle-kit migrate     # Apply migrations"));
    console.log(pc.cyan("  npx drizzle-kit studio      # Open database studio"));
    console.log("");
    process.exit(0);
}
else {
    console.error(pc.red(`Unknown command: ${cmd}`));
    console.log(pc.gray("\nRun 'quickback --help' for available commands."));
    process.exit(1);
}
//# sourceMappingURL=index.js.map