import { registerSFCommands } from "./commands/index.js";
import { workflowTemplateCommandDefinitions } from "./workflow-templates.js";

const TOP_LEVEL_SUBCOMMANDS = [
	{ cmd: "help", desc: "Categorized command reference with descriptions" },
	{ cmd: "next", desc: "Assisted mode — execute one unit, then pause" },
	{
		cmd: "autonomous",
		desc: "Autonomous mode — research, plan, execute, commit, repeat",
	},
	{ cmd: "stop", desc: "Stop autonomous mode gracefully" },
	{
		cmd: "pause",
		desc: "Pause autonomous mode (preserves state, /autonomous to resume)",
	},
	{ cmd: "status", desc: "Progress dashboard" },
	{ cmd: "visualize", desc: "Open workflow visualizer" },
	{ cmd: "queue", desc: "Queue and reorder future milestones" },
	{ cmd: "quick", desc: "Execute a quick task without full planning overhead" },
	{ cmd: "discuss", desc: "Discuss architecture and decisions" },
	{ cmd: "capture", desc: "Fire-and-forget thought capture" },
	{ cmd: "triage", desc: "Manually trigger triage of pending captures" },
	{ cmd: "history", desc: "View execution history" },
	{ cmd: "undo", desc: "Revert last completed unit" },
	{ cmd: "skip", desc: "Prevent a unit from autonomous dispatch" },
	{ cmd: "park", desc: "Park a milestone" },
	{ cmd: "unpark", desc: "Reactivate a parked milestone" },
	{ cmd: "prefs", desc: "Manage preferences" },
	{ cmd: "config", desc: "Set API keys for external tools" },
	{ cmd: "keys", desc: "API key manager" },
	{ cmd: "model", desc: "Switch the active session model or open a picker" },
	{ cmd: "doctor", desc: "Runtime health checks with auto-fix" },
	{ cmd: "repair", desc: "Repair runtime state and generated artifacts" },
	{ cmd: "logs", desc: "Browse activity logs, debug logs, and metrics" },
	{ cmd: "forensics", desc: "Examine execution logs" },
	{ cmd: "init", desc: "Project init wizard" },
	{ cmd: "setup", desc: "Global setup status and configuration" },
	{ cmd: "remote", desc: "Configure remote question delivery" },
	{ cmd: "knowledge", desc: "Add persistent project knowledge" },
	{ cmd: "update", desc: "Update SF to the latest version" },
	{ cmd: "start", desc: "Start a workflow template" },
	{ cmd: "templates", desc: "List available workflow templates" },
	{ cmd: "workflow", desc: "Inspect or continue workflow state" },
	{ cmd: "ship", desc: "Prepare changes for review and delivery" },
	{ cmd: "schedule", desc: "Manage time-bound reminders" },
	{ cmd: "backlog", desc: "Inspect priority-ordered work" },
];
function filterStartsWith(partial, options, prefix = "") {
	const normalizedPrefix = prefix.length > 0 ? `${prefix} ` : "";
	return options
		.filter((option) => option.cmd.startsWith(partial))
		.map((option) => ({
			value: `${normalizedPrefix}${option.cmd}`,
			label: option.cmd,
			description: option.desc,
		}));
}
function _getSfArgumentCompletions(prefix) {
	const parts = prefix.trim().split(/\s+/);
	if (parts.length <= 1) {
		return filterStartsWith(parts[0] ?? "", TOP_LEVEL_SUBCOMMANDS);
	}
	const partial = parts[1] ?? "";
	if (parts[0] === "autonomous" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "--verbose", desc: "Show detailed execution output" },
				{ cmd: "--debug", desc: "Enable debug logging" },
			],
			parts[0],
		);
	}
	if (parts[0] === "next" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "--verbose", desc: "Show detailed step output" },
				{ cmd: "--dry-run", desc: "Preview next step without executing" },
			],
			"next",
		);
	}
	if (parts[0] === "mode" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "global", desc: "Edit global workflow mode" },
				{ cmd: "project", desc: "Edit project-specific workflow mode" },
			],
			"mode",
		);
	}
	if (parts[0] === "parallel" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "start", desc: "Start parallel milestone orchestration" },
				{ cmd: "status", desc: "Show parallel worker statuses" },
				{ cmd: "stop", desc: "Stop all parallel workers" },
				{ cmd: "pause", desc: "Pause a specific worker" },
				{ cmd: "resume", desc: "Resume a paused worker" },
				{ cmd: "merge", desc: "Merge completed milestone branches" },
			],
			"parallel",
		);
	}
	if (parts[0] === "setup" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "llm", desc: "Configure LLM provider settings" },
				{ cmd: "search", desc: "Configure web search provider" },
				{ cmd: "remote", desc: "Configure remote integrations" },
				{ cmd: "keys", desc: "Manage API keys" },
				{ cmd: "prefs", desc: "Configure global preferences" },
			],
			"setup",
		);
	}
	if (parts[0] === "logs" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "debug", desc: "List or view debug log files" },
				{ cmd: "tail", desc: "Show last N activity log summaries" },
				{ cmd: "clear", desc: "Remove old activity and debug logs" },
			],
			"logs",
		);
	}
	if (parts[0] === "keys" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "list", desc: "Show key status dashboard" },
				{ cmd: "add", desc: "Add a key for a provider" },
				{ cmd: "remove", desc: "Remove a key" },
				{ cmd: "test", desc: "Validate key(s) with API call" },
				{ cmd: "rotate", desc: "Replace an existing key" },
				{ cmd: "doctor", desc: "Health check all keys" },
			],
			"keys",
		);
	}
	if (parts[0] === "prefs" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "global", desc: "Edit global preferences file" },
				{ cmd: "project", desc: "Edit project preferences file" },
				{ cmd: "status", desc: "Show effective preferences" },
				{ cmd: "wizard", desc: "Interactive preferences wizard" },
				{ cmd: "setup", desc: "First-time preferences setup" },
				{ cmd: "import-claude", desc: "Import settings from Claude Code" },
			],
			"prefs",
		);
	}
	if (parts[0] === "remote" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "slack", desc: "Configure Slack integration" },
				{ cmd: "discord", desc: "Configure Discord integration" },
				{ cmd: "status", desc: "Show remote connection status" },
				{ cmd: "disconnect", desc: "Disconnect remote integrations" },
			],
			"remote",
		);
	}
	if (parts[0] === "history" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "--cost", desc: "Show cost breakdown per entry" },
				{ cmd: "--phase", desc: "Filter by phase type" },
				{ cmd: "--model", desc: "Filter by model used" },
				{ cmd: "10", desc: "Show last 10 entries" },
				{ cmd: "20", desc: "Show last 20 entries" },
				{ cmd: "50", desc: "Show last 50 entries" },
			],
			"history",
		);
	}
	if (parts[0] === "export" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "--json", desc: "Export as JSON" },
				{ cmd: "--markdown", desc: "Export as Markdown" },
				{ cmd: "--html", desc: "Export as HTML" },
				{ cmd: "--html --all", desc: "Export all milestones as HTML" },
			],
			"export",
		);
	}
	if (parts[0] === "cleanup" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "branches", desc: "Remove merged milestone branches" },
				{ cmd: "snapshots", desc: "Remove old execution snapshots" },
			],
			"cleanup",
		);
	}
	if (parts[0] === "knowledge" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "rule", desc: "Add a project rule" },
				{ cmd: "pattern", desc: "Add a code pattern" },
				{ cmd: "lesson", desc: "Record a lesson learned" },
			],
			"knowledge",
		);
	}
	if (parts[0] === "start" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				...workflowTemplateCommandDefinitions(),
				{ cmd: "resume", desc: "Resume an in-progress workflow" },
				{ cmd: "--list", desc: "List all available templates" },
				{ cmd: "--dry-run", desc: "Preview workflow without executing" },
			],
			"start",
		);
	}
	if (parts[0] === "templates" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[{ cmd: "info", desc: "Show detailed template info" }],
			"templates",
		);
	}
	if (parts[0] === "extensions" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "list", desc: "List all extensions and their status" },
				{ cmd: "enable", desc: "Enable a disabled extension" },
				{ cmd: "disable", desc: "Disable an extension" },
				{ cmd: "info", desc: "Show extension details" },
			],
			"extensions",
		);
	}
	if (parts[0] === "codebase" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "generate", desc: "Generate or regenerate CODEBASE.md" },
				{ cmd: "update", desc: "Refresh the CODEBASE.md cache immediately" },
				{
					cmd: "stats",
					desc: "Show codebase-map coverage and generation time",
				},
				{
					cmd: "indexer",
					desc: "Inspect Sift code search backend",
				},
				{ cmd: "help", desc: "Show usage and subcommands" },
			],
			"codebase",
		);
	}
	if (parts[0] === "triage" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[{ cmd: "--source", desc: "Triage source (captures|todo)" }],
			"triage",
		);
	}
	if (parts[0] === "triage" && parts[1] === "--source" && parts.length <= 3) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "captures", desc: "Triage pending captures (default)" },
				{ cmd: "todo", desc: "Triage repo-root TODO.md" },
			],
			"triage --source",
		);
	}
	if (parts[0] === "doctor" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "fix", desc: "Auto-fix detected issues" },
				{ cmd: "heal", desc: "AI-driven deep healing" },
				{ cmd: "audit", desc: "Run health audit without fixing" },
			],
			"doctor",
		);
	}
	if (parts[0] === "scaffold" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{
					cmd: "sync",
					desc: "Refresh ADR-021 scaffold docs (drift report + apply pending upgrades)",
				},
			],
			"scaffold",
		);
	}
	if (parts[0] === "dispatch" && parts.length <= 2) {
		return filterStartsWith(
			partial,
			[
				{ cmd: "research", desc: "Run research phase" },
				{ cmd: "plan", desc: "Run planning phase" },
				{ cmd: "execute", desc: "Run execution phase" },
				{ cmd: "complete", desc: "Run completion phase" },
				{ cmd: "reassess", desc: "Reassess current progress" },
				{ cmd: "uat", desc: "Run user acceptance testing" },
				{ cmd: "replan", desc: "Replan the current slice" },
			],
			"dispatch",
		);
	}
	return null;
}
export function registerLazySFCommand(pi) {
	registerSFCommands(pi);
}
