import fs from 'fs';
import path from 'path';

export default function sigproRouter() {
  const virtualModuleId = 'virtual:sigpro-routes';
  const resolvedVirtualModuleId = '\0' + virtualModuleId;

  // ... (tus funciones getFiles y pathToUrl se quedan igual)

  return {
    name: 'sigpro-router',
    resolveId(id) {
      if (id === virtualModuleId) return resolvedVirtualModuleId;
    },
    load(id) {
      if (id !== resolvedVirtualModuleId) return;

      const pagesDir = path.resolve(process.cwd(), 'src/pages');
      const files = getFiles(pagesDir).sort((a, b) => b.length - a.length);

      let routeEntries = '';

      files.forEach((fullPath, i) => {
        const urlPath = pathToUrl(pagesDir, fullPath);
        const importPath = fullPath.replace(/\\/g, '/');

        routeEntries += `  { path: '${urlPath}', component: () => import('${importPath}') },\n`;
      });

      if (!routeEntries.includes("path: '*'")) {
        routeEntries += `  { path: '*', component: () => h1('404 - Not Found') },\n`;
      }

      return `export const routes = [\n${routeEntries}];`;
    }
  };
}