// SF — Setup catalog (single source of truth for onboarding steps + provider sub-views)
//
// Re-exports filtered views over PROVIDER_REGISTRY (key-manager.ts) and owns the
// canonical ONBOARDING_STEPS list. Consumers (CLI wizard, /setup hub,
// onboarding handler, web alignment) all read from here so adding a step or
// provider lands in one place. Keep this module thin: no behavior beyond
// filters + lookup helpers, so it stays cycle-safe even though it depends on
// key-manager for the provider catalog.
import { PROVIDER_REGISTRY } from "./key-manager.js";
/**
 * Canonical ordered list of onboarding steps.
 *
 * To add a new step:
 *   1. Append here (or insert at the right position).
 *   2. Bump FLOW_VERSION in onboarding-state.ts so existing users get re-prompted.
 *   3. Wire its CLI runner in src/onboarding.ts (and handlers/onboarding.ts for --step).
 */
export const ONBOARDING_STEPS = [
	{
		id: "llm",
		label: "LLM provider & auth",
		required: true,
		hint: "Sign in or paste an API key",
	},
	{
		id: "model",
		label: "Default model",
		required: false,
		hint: "Pick a default model for the chosen provider",
	},
	{
		id: "search",
		label: "Web search provider",
		required: false,
		hint: "Brave, Tavily, or Anthropic built-in",
	},
	{
		id: "remote",
		label: "Remote questions",
		required: false,
		hint: "Discord / Slack / Telegram notifications",
	},
	{
		id: "tool-keys",
		label: "Tool API keys",
		required: false,
		hint: "Context7, Jina, Groq voice, etc.",
	},
	{
		id: "prefs",
		label: "Global preferences",
		required: false,
		hint: "Mode, profile, notifications",
	},
	{
		id: "skills",
		label: "Skills install",
		required: false,
		hint: "Browse and install skill plugins",
	},
	{
		id: "doctor",
		label: "Validate setup",
		required: false,
		hint: "Run provider doctor checks",
	},
	{
		id: "project",
		label: "Project init",
		required: false,
		hint: "Bootstrap .sf/ in this repo",
	},
];
const STEP_INDEX = new Map(ONBOARDING_STEPS.map((s, i) => [s.id, i]));
export function getStep(id) {
	const idx = STEP_INDEX.get(id);
	return idx === undefined ? undefined : ONBOARDING_STEPS[idx];
}
export function isValidStepId(id) {
	return STEP_INDEX.has(id);
}
/**
 * Given a possibly-stale resume point, return the nearest next step that is
 * still defined in the catalog. Falls back to the first step.
 */
export function nearestResumeStep(lastResumePoint, completedSteps) {
	const completed = new Set(completedSteps);
	// First incomplete step at or after the lastResumePoint
	let startIdx = 0;
	if (lastResumePoint && STEP_INDEX.has(lastResumePoint)) {
		startIdx = STEP_INDEX.get(lastResumePoint) ?? 0;
	}
	for (let i = startIdx; i < ONBOARDING_STEPS.length; i++) {
		if (!completed.has(ONBOARDING_STEPS[i].id)) return ONBOARDING_STEPS[i].id;
	}
	// Everything from the resume point is complete — try from the start
	for (const step of ONBOARDING_STEPS) {
		if (!completed.has(step.id)) return step.id;
	}
	return ONBOARDING_STEPS[0].id;
}
// ─── Provider catalog views ───────────────────────────────────────────────────
export function getLlmProviders() {
	return PROVIDER_REGISTRY.filter((p) => p.category === "llm");
}
export function getToolProviders() {
	return PROVIDER_REGISTRY.filter((p) => p.category === "tool");
}
export function getSearchProviders() {
	return PROVIDER_REGISTRY.filter((p) => p.category === "search");
}
export function getRemoteProviders() {
	return PROVIDER_REGISTRY.filter((p) => p.category === "remote");
}
/** Provider IDs that count as "the user has an LLM configured" for shouldRunOnboarding. */
export function getLlmProviderIds() {
	return Array.from(
		new Set([...getLlmProviders().map((p) => p.id), "claude-code"]),
	);
}
// Key-free and meta search provider IDs that are valid as preferences but have
// no PROVIDER_REGISTRY entry (no env-var key required).
const KEYLESS_SEARCH_PROVIDER_IDS = [
	"minimax",
	"ollama",
	"combosearch",
	"native",
	"auto",
];
/**
 * All valid values for the `search_provider` preference.
 * Derived from PROVIDER_REGISTRY (key-backed) plus keyless/meta providers.
 * Single source of truth — import this instead of hardcoding the list.
 */
export const VALID_SEARCH_PROVIDER_PREFS = new Set([
	...getSearchProviders().map((p) => p.id),
	...KEYLESS_SEARCH_PROVIDER_IDS,
]);
