import Dashboard from '@pages/Dashboard';
import CrudPage from '@pages/CrudPage';

const kebab = (s) =>
  String(s)
    .replace(/([a-z0-9])([A-Z])/g, '$1-$2')
    .toLowerCase();

/**
 * Build the protected route table from the module registry (pass the live
 * registry from useModules()). Each model gets a generic list page; nav items +
 * route guards key off (module, action='view').
 */
export function buildRoutes(modules = {}) {
  const routes = [{ path: '/', element: <Dashboard />, label: 'Dashboard', icon: 'layout-dashboard', module: 'core', action: 'view' }];
  for (const mod of Object.values(modules)) {
    for (const model of mod.models || []) {
      routes.push({
        path: `/${kebab(model)}`,
        element: <CrudPage model={model} module={mod.key} title={model} />,
        label: model,
        icon: mod.icon,
        module: mod.key,
        action: 'view',
      });
    }
  }
  return routes;
}
