import { readFileSync } from "node:fs";
import path from "node:path";
import { booleanFlag, parseArgs, stringFlag } from "./args.js";
import { formatDoctorResult, runDoctor } from "./doctor.js";
import { formatInstallResult, runInstall } from "./install.js";
import { OPENCODE_MIN_VERSION, OPENCODE_TESTED_VERSION, SEED_DEFAULT_AGENT, SEED_DEFAULT_AGENT_LABEL, SEED_DEFAULT_MODEL, SEED_DEFAULT_VARIANT } from "./constants.js";
import { packageRootFromCli } from "./paths.js";
import { runSeedAgent } from "./run.js";
export async function runCli(argv = process.argv.slice(2)) {
    const parsed = parseArgs(argv);
    const help = booleanFlag(parsed.flags, "help");
    const json = booleanFlag(parsed.flags, "json");
    if (!parsed.command || help || parsed.command === "--help" || parsed.command === "-h") {
        console.log(helpText());
        return 0;
    }
    if (parsed.command === "version" || parsed.command === "--version" || parsed.command === "-v") {
        console.log(`seed-agent v${readPackageVersion()}`);
        return 0;
    }
    if (parsed.command === "install" || parsed.command === "setup") {
        const installModel = stringFlag(parsed.flags, "model");
        const provider = stringFlag(parsed.flags, "provider") ?? providerFromModel(installModel);
        const result = await runInstall({
            local: booleanFlag(parsed.flags, "local"),
            project: booleanFlag(parsed.flags, "project"),
            config: stringFlag(parsed.flags, "config"),
            seedConfig: stringFlag(parsed.flags, "seedConfig"),
            pluginSpec: stringFlag(parsed.flags, "plugin"),
            packageRoot: stringFlag(parsed.flags, "packageRoot"),
            model: installModel,
            variant: stringFlag(parsed.flags, "variant"),
            dryRun: booleanFlag(parsed.flags, "dryRun"),
            skipOpenCodeDeps: booleanFlag(parsed.flags, "skipOpenCodeDeps"),
            provider,
            skipAuth: booleanFlag(parsed.flags, "skipAuth"),
        });
        console.log(json ? JSON.stringify(result, null, 2) : formatInstallResult(result, { provider, skipAuth: booleanFlag(parsed.flags, "skipAuth") }));
        return 0;
    }
    if (parsed.command === "doctor") {
        const result = await runDoctor({
            json,
            project: booleanFlag(parsed.flags, "project"),
            config: stringFlag(parsed.flags, "config"),
            seedConfig: stringFlag(parsed.flags, "seedConfig"),
            includeAgents: booleanFlag(parsed.flags, "agents"),
            cwd: stringFlag(parsed.flags, "directory"),
        });
        console.log(json ? JSON.stringify(result, null, 2) : formatDoctorResult(result));
        return result.exitCode;
    }
    if (parsed.command === "run") {
        const message = parsed.positionals.join(" ").trim();
        if (!message) {
            console.error("seed-agent run requires a message.");
            console.error("Example: seed-agent run --model anthropic/claude-sonnet-4-5 --variant high \"Improve README\"");
            return 1;
        }
        return runSeedAgent({
            message,
            agent: stringFlag(parsed.flags, "agent") ?? SEED_DEFAULT_AGENT,
            model: stringFlag(parsed.flags, "model"),
            variant: stringFlag(parsed.flags, "variant"),
            directory: stringFlag(parsed.flags, "directory"),
            json,
            passthrough: parsed.passthrough,
        });
    }
    console.error(`Unknown command: ${parsed.command}`);
    console.error("");
    console.error(helpText());
    return 1;
}
function helpText() {
    return `SEED-agent OpenCode plugin CLI

Usage:
  seed-agent install [--local] [--project]
  seed-agent doctor [--agents] [--json]
  seed-agent run [options] "professor goal"
  seed-agent version

Commands:
  install   Register the SEED plugin in OpenCode config and create model-neutral seed-agent config.
  doctor    Check OpenCode, plugin registration, seed config, and configured provider visibility.
  run       Execute opencode run --command seed with ${SEED_DEFAULT_AGENT_LABEL} by default.
  version   Show CLI version.

Runtime compatibility:
  OS:       Linux only for this release
  OpenCode: >=${OPENCODE_MIN_VERSION} (tested ${OPENCODE_TESTED_VERSION})
  Node.js:  >=20

Install examples:
  seed-agent install --local --project
  seed-agent install --project --model anthropic/claude-sonnet-4-5
  seed-agent install --local
  seed-agent install --plugin @seed-lab/opencode-seed-agent

Run example:
  seed-agent run --model ${SEED_DEFAULT_MODEL} --variant ${SEED_DEFAULT_VARIANT} "Create a short SEED smoke report."

Provider keys:
  API keys stay in OpenCode. Authenticate the provider you want to use:
  opencode providers login -p openai
  opencode providers login -p anthropic

Defaults:
  agent: ${SEED_DEFAULT_AGENT} (${SEED_DEFAULT_AGENT_LABEL})
  model: OpenCode selected model unless install --model pins one
  run example model: ${SEED_DEFAULT_MODEL}
  run example variant: ${SEED_DEFAULT_VARIANT}
`;
}
function providerFromModel(model) {
    if (!model)
        return undefined;
    const slash = model.indexOf("/");
    return slash > 0 ? model.slice(0, slash) : undefined;
}
function readPackageVersion() {
    try {
        const raw = readFileSync(path.join(packageRootFromCli(), "package.json"), "utf8");
        const parsed = JSON.parse(raw);
        return parsed.version ?? "0.0.0";
    }
    catch {
        return "0.0.0";
    }
}
//# sourceMappingURL=index.js.map