import { CommonModule } from "@angular/common";
import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";
import { HeaderModule } from "src/app/components/header/header.module";
import { LayoutModule } from "src/app/components/layout/layout.module";
import { AuthGuard } from "src/app/services/authguard.service";
import { MenuDataGuard } from "src/app/services/menu-data.guard";
import { HomePageComponent } from "./home.page";
import { getAllElements } from "src/app/custom/common";

// Fonction pour obtenir le domaine par défaut de manière sécurisée
function getDefaultDomain(): string {
  try {
    // Rediriger vers le dashboard au lieu du module billets problématique
    return "dashboard"; // Utiliser le dashboard par défaut
  } catch (error) {
    console.warn(
      "Erreur lors de la récupération du domaine par défaut:",
      error
    );
  }
  return "dashboard";
}

const routes: Routes = [
  {
    canActivate: [AuthGuard, MenuDataGuard],
    path: "",
    component: HomePageComponent,
    children: [
      {
        path: "",
        redirectTo: getDefaultDomain(),
        pathMatch: "full",
      },
      // Routes Dashboard directes
      {
        path: "dashboard",
        children: [
          {
            path: "general",
            loadChildren: () =>
              import("src/app/pages/dashboard-view/dashboard-view.module").then(
                (m) => m.DashboardViewModule
              ),
          },
          {
            path: "finance", 
            loadChildren: () =>
              import("src/app/pages/dashboard-view/dashboard-view.module").then(
                (m) => m.DashboardViewModule
              ),
          },
          {
            path: "projects",
            loadChildren: () =>
              import("src/app/pages/dashboard-view/dashboard-view.module").then(
                (m) => m.DashboardViewModule
              ),
          },
          {
            path: "reporting",
            loadChildren: () =>
              import("src/app/pages/reports/dashboard/reports-dashboard.module").then(
                (m) => m.ReportsDashboardModule
              ),
          },
          { path: "", redirectTo: "reporting", pathMatch: "full" }
        ]
      },
      {
        path: ":name",
        pathMatch: "prefix",
        canActivate: [MenuDataGuard], // Réactivé maintenant que l'auth fonctionne
        loadChildren: () =>
          import("src/app/pages/projects/projects.module").then(
            (m) => m.ProjectsPageModule
          ),
      },
    ],
  },
];

@NgModule({
  imports: [
    CommonModule, // ngrx related imports
    LayoutModule,
    HeaderModule,
    RouterModule.forChild(routes),
  ],

  declarations: [HomePageComponent],
  providers: [],
})
export class HomePagesModule {}