import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import {
  CanActivateCartGuard,
  CanActivateCartWithProcessIdGuard,
  CanActivateCustomerGuard,
  CanActivateCustomerWithProcessIdGuard,
  CanActivateGoodsInGuard,
  CanActivateGoodsOutGuard,
  CanActivateGoodsOutWithProcessIdGuard,
  CanActivateProductGuard,
  CanActivateProductWithProcessIdGuard,
  CanActivateRemissionGuard,
  CanActivateTaskCalendarGuard,
  IsAuthenticatedGuard,
} from './guards';
import { BranchSectionResolver, CustomerSectionResolver, ProcessIdResolver } from './resolvers';
import { ShellComponent, ShellModule } from './shell';
import { TokenLoginComponent, TokenLoginModule } from './token-login';

const routes: Routes = [
  {
    path: 'login',
    children: [
      { path: ':token', component: TokenLoginComponent },
      { path: '**', redirectTo: 'kunde', pathMatch: 'full' },
    ],
  },
  {
    path: '',
    canActivate: [IsAuthenticatedGuard],
    children: [
      {
        path: '',
        canActivate: [],
        children: [
          {
            path: 'kunde',
            component: ShellComponent,
            children: [
              {
                path: 'dashboard',
                loadChildren: () => import('@page/dashboard').then((m) => m.DashboardModule),
              },
              {
                path: 'product',
                loadChildren: () => import('@page/catalog').then((m) => m.PageCatalogModule),
                canActivate: [CanActivateProductGuard],
              },
              {
                path: ':processId/product',
                loadChildren: () => import('@page/catalog').then((m) => m.PageCatalogModule),
                canActivate: [CanActivateProductWithProcessIdGuard],
                resolve: { processId: ProcessIdResolver },
              },
              {
                path: 'customer',
                loadChildren: () => import('@page/customer').then((m) => m.PageCustomerModule),
                canActivate: [CanActivateCustomerGuard],
              },
              {
                path: ':processId/customer',
                loadChildren: () => import('@page/customer').then((m) => m.PageCustomerModule),
                canActivate: [CanActivateCustomerWithProcessIdGuard],
                resolve: { processId: ProcessIdResolver },
              },
              {
                path: 'cart',
                loadChildren: () => import('@page/checkout').then((m) => m.PageCheckoutModule),
                canActivate: [CanActivateCartGuard],
              },
              {
                path: ':processId/cart',
                loadChildren: () => import('@page/checkout').then((m) => m.PageCheckoutModule),
                canActivate: [CanActivateCartWithProcessIdGuard],
              },
              {
                path: 'goods/out',
                loadChildren: () => import('@page/goods-out').then((m) => m.GoodsOutModule),
                canActivate: [CanActivateGoodsOutGuard],
              },
              {
                path: ':processId/goods/out',
                loadChildren: () => import('@page/goods-out').then((m) => m.GoodsOutModule),
                canActivate: [CanActivateGoodsOutWithProcessIdGuard],
                resolve: { processId: ProcessIdResolver },
              },
              { path: '**', redirectTo: 'dashboard', pathMatch: 'full' },
            ],
            resolve: { section: CustomerSectionResolver },
          },
          {
            path: 'filiale',
            component: ShellComponent,
            children: [
              {
                path: 'task-calendar',
                loadChildren: () => import('@page/task-calendar').then((m) => m.PageTaskCalendarModule),
                canActivate: [CanActivateTaskCalendarGuard],
              },
              {
                path: 'goods/in',
                loadChildren: () => import('@page/goods-in').then((m) => m.GoodsInModule),
                canActivate: [CanActivateGoodsInGuard],
              },
              {
                path: 'remission',
                loadChildren: () => import('@page/remission').then((m) => m.PageRemissionModule),
                canActivate: [CanActivateRemissionGuard],
              },
              { path: '**', redirectTo: 'task-calendar', pathMatch: 'full' },
            ],
            resolve: { section: BranchSectionResolver },
          },
          { path: '**', redirectTo: 'kunde', pathMatch: 'full' },
        ],
      },
    ],
  },
];

@NgModule({
  imports: [RouterModule.forRoot(routes), ShellModule, TokenLoginModule],
  exports: [RouterModule],
})
export class AppRoutingModule {}
