// #region RoutesRegistry [C:3] [TYPE Module] [SEMANTICS routes,navigation,registry,ssot]
// @BRIEF Single Source of Truth for all frontend route paths. Every goto() and href= must use these functions.
// @INVARIANT Every function returns a valid path that maps to a file in src/routes/.
// @INVARIANT Adding a new route requires adding a function here AND the corresponding file in src/routes/.
// @RELATION DEPENDS_ON -> [FileSystem:src/routes/]
// @RATIONALE Centralized route registry prevents dead links (href/goto pointing to non-existent routes).
//   Before this registry, routes were hardcoded as strings in 40+ locations across the codebase,
//   causing 404 errors when routes were renamed or not yet created. TypeScript catches typos in
//   function names and parameter types at compile time. The link-integrity test scans all href/goto
//   usages and validates them against this registry's output patterns.
// @REJECTED String constants without functions rejected — dynamic segments (ids, envIds) need
//   parameterized builders. Class-based registry rejected — plain object with arrow functions is
//   tree-shakeable and works in both Svelte templates and TypeScript.
// @REJECTED Enum-based paths rejected — does not support dynamic parameters.
//

/**
 * ROUTES — typed route builders for the entire application.
 *
 * Usage:
 *   import { ROUTES } from '$lib/routes';
 *   goto(ROUTES.dashboards.detail(id, envId));
 *   <a href={ROUTES.dashboards.detail(id, envId)}>...
 */
export const ROUTES = {
	// ── Home ─────────────────────────────────────────────────
	home: () => '/',

	// ── Auth ─────────────────────────────────────────────────
	login: () => '/login',

	// ── Dashboards ───────────────────────────────────────────
	dashboards: {
		list: (envId?: string) =>
			envId ? `/dashboards?env_id=${encodeURIComponent(envId)}` : '/dashboards',
		detail: (id: string | number, envId: string) =>
			`/dashboards/${encodeURIComponent(String(id))}?env_id=${encodeURIComponent(envId)}`,
		validation: (id: string | number, envId: string) =>
			`/dashboards/${encodeURIComponent(String(id))}/validation?env_id=${encodeURIComponent(envId)}`,
		health: () => '/dashboards/health',
	},

	// ── Datasets ─────────────────────────────────────────────
	datasets: {
		list: (envId?: string) =>
			envId ? `/datasets?env_id=${encodeURIComponent(envId)}` : '/datasets',
		detail: (id: string | number, envId: string) =>
			`/datasets/${encodeURIComponent(String(id))}?env_id=${encodeURIComponent(envId)}`,
		review: {
			list: () => '/datasets/review',
			workspace: (sessionId: string) => `/datasets/review/${encodeURIComponent(sessionId)}`,
		},
	},

	// ── Migration ────────────────────────────────────────────
	migration: {
		overview: () => '/migration',
		mappings: () => '/migration/mappings',
	},

	// ── Git ──────────────────────────────────────────────────
	git: {
		status: () => '/git',
	},

	// ── Translation ──────────────────────────────────────────
	translate: {
		list: () => '/translate',
		new: () => '/translate/new',
		detail: (id: string | number) => `/translate/${encodeURIComponent(String(id))}`,
		dictionaries: {
			list: () => '/translate/dictionaries',
			detail: (id: string | number) =>
				`/translate/dictionaries/${encodeURIComponent(String(id))}`,
		},
		history: () => '/translate/history',
	},

	// ── Validation Tasks ─────────────────────────────────────
	validationTasks: {
		list: (search?: string) =>
			search
				? `/validation-tasks?search=${encodeURIComponent(search)}`
				: '/validation-tasks',
		new: (dashboardIds?: number[]) =>
			dashboardIds?.length
				? `/validation-tasks/new?ids=${dashboardIds.join(',')}`
				: '/validation-tasks/new',
		detail: (policyId: string) =>
			`/validation-tasks/${encodeURIComponent(policyId)}`,
		edit: (policyId: string) =>
			`/validation-tasks/${encodeURIComponent(policyId)}/edit`,
		runReport: (policyId: string, runId: string) =>
			`/validation-tasks/${encodeURIComponent(policyId)}/runs/${encodeURIComponent(runId)}`,
		history: () => '/validation-tasks/history',
	},

	// ── Reports ──────────────────────────────────────────────
	reports: {
		list: () => '/reports',
		llmReport: (taskId: string) => `/reports/llm/${encodeURIComponent(taskId)}`,
	},

	// ── Settings ─────────────────────────────────────────────
	settings: {
		general: () => '/settings',
		environmentsTab: () => '/settings?tab=environments#environments',
		connections: () => '/settings#/settings/connections',
		git: () => '/settings/git',
		automation: () => '/settings/automation',
		notifications: () => '/settings/notifications',
	},

	// ── Admin ────────────────────────────────────────────────
	admin: {
		overview: () => '/admin',
		users: () => '/admin/users',
		roles: () => '/admin/roles',
		settings: () => '/admin/settings',
		llmSettings: () => '/admin/settings/llm',
	},

	// ── Storage ──────────────────────────────────────────────
	storage: {
		overview: () => '/storage',
		backups: () => '/storage/backups',
	},

	// ── Tools ────────────────────────────────────────────────
	tools: {
		overview: () => '/tools',
		mapper: () => '/tools/mapper',
		debug: () => '/tools/debug',
		storage: () => '/tools/storage',
		backups: () => '/tools/backups',
	},

	// ── Profile ──────────────────────────────────────────────
	profile: () => '/profile',

	// ── Maintenance ──────────────────────────────────────────
	maintenance: () => '/maintenance',
} as const;

/**
 * RouteParam — extract parameter types from ROUTES function signatures.
 * Used by the link-integrity test to verify route patterns.
 */
export type RouteParam = string | number | undefined;

// #endregion RoutesRegistry
