{{!-- SmartStack NavRoute Registry Template --}}
{{!-- Generates centralized route registry for frontend --}}

/**
 * NavRoute Registry
 *
 * Auto-generated by SmartStack MCP - DO NOT EDIT MANUALLY
 * Generated: {{generatedAt}}
 * Source: {{source}}
 * Routes: {{routeCount}}
 */

export interface NavRoute {
  /** NavRoute path (e.g., "administration.users") */
  navRoute: string;
  /** API endpoint path (e.g., "/api/administration/users") */
  api: string;
  /** Frontend route path (e.g., "/administration/users") */
  web: string;
  /** Required permissions to access this route */
  permissions: string[];
  /** Backend controller name */
  controller?: string;
  /** Supported HTTP methods */
  methods: string[];
}

/**
 * All registered NavRoutes
 */
export const ROUTES: Record<string, NavRoute> = {
{{#each routes}}
  '{{this.navRoute}}': {
    navRoute: '{{this.navRoute}}',
    api: '{{this.apiPath}}',
    web: '{{this.webPath}}',
    permissions: [{{#each this.permissions}}'{{this}}'{{#unless @last}}, {{/unless}}{{/each}}],
{{#if this.controller}}
    controller: '{{this.controller}}',
{{/if}}
    methods: [{{#each this.methods}}'{{this}}'{{#unless @last}}, {{/unless}}{{/each}}],
  },
{{/each}}
};

/**
 * Get route configuration by NavRoute path
 * @throws Error if route not found
 */
export function getRoute(navRoute: string): NavRoute {
  const route = ROUTES[navRoute];
  if (!route) {
    throw new Error(`Route not found: ${navRoute}. Run scaffold_routes to regenerate.`);
  }
  return route;
}

/**
 * Safely get route configuration, returns undefined if not found
 */
export function tryGetRoute(navRoute: string): NavRoute | undefined {
  return ROUTES[navRoute];
}

/**
 * Check if user has permission for route
 */
export function hasRoutePermission(navRoute: string, userPermissions: string[]): boolean {
  const route = ROUTES[navRoute];
  if (!route || route.permissions.length === 0) return true;
  return route.permissions.some(p => userPermissions.includes(p));
}

/**
 * Get all routes for an application (e.g., "administration")
 */
export function getRoutesByApplication(application: string): NavRoute[] {
  return Object.values(ROUTES).filter(r => r.navRoute.startsWith(`${application}.`));
}

/**
 * Get all routes for an application and module (e.g., "administration.users")
 */
export function getRoutesByModule(application: string, module: string): NavRoute[] {
  const prefix = `${application}.${module}`;
  return Object.values(ROUTES).filter(r => r.navRoute.startsWith(`${prefix}.`) || r.navRoute === prefix);
}

/**
 * Get all unique applications
 */
export function getApplications(): string[] {
  const applications = new Set<string>();
  for (const route of Object.values(ROUTES)) {
    applications.add(route.navRoute.split('.')[0]);
  }
  return Array.from(applications);
}

/**
 * Get all unique modules within an application
 */
export function getModules(application: string): string[] {
  const modules = new Set<string>();
  for (const route of Object.values(ROUTES)) {
    const parts = route.navRoute.split('.');
    if (parts[0] === application && parts.length >= 2) {
      modules.add(parts[1]);
    }
  }
  return Array.from(modules);
}

/**
 * Build API URL from NavRoute with optional path segments
 */
export function buildApiUrl(navRoute: string, ...segments: string[]): string {
  const route = getRoute(navRoute);
  if (segments.length === 0) return route.api;
  return `${route.api}/${segments.join('/')}`;
}

/**
 * Build frontend URL from NavRoute with optional path segments
 */
export function buildWebUrl(navRoute: string, ...segments: string[]): string {
  const route = getRoute(navRoute);
  if (segments.length === 0) return route.web;
  return `${route.web}/${segments.join('/')}`;
}

// Route count for debugging
export const ROUTE_COUNT = {{routeCount}};
