import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import {
  CanActivateCartGuard,
  CanActivateCartWithProcessIdGuard,
  CanActivateCustomerGuard,
  CanActivateCustomerOrdersGuard,
  CanActivateCustomerOrdersWithProcessIdGuard,
  CanActivateCustomerWithProcessIdGuard,
  CanActivateProductGuard,
  CanActivateProductWithProcessIdGuard,
  IsAuthenticatedGuard,
} from './guards';
import { MainComponent } from './main.component';
import {
  BranchSectionResolver,
  CustomerSectionResolver,
  ProcessIdResolver,
} from './resolvers';
import { TokenLoginComponent, TokenLoginModule } from './token-login';
import {
  ActivateProcessIdGuard,
  ActivateProcessIdWithConfigKeyGuard,
} from './guards/activate-process-id.guard';
import { MatomoRouteData } from 'ngx-matomo-client';
import {
  tabResolverFn,
  processResolverFn,
  hasTabIdGuard,
} from '@isa/core/tabs';
import { provideScrollPositionRestoration } from '@isa/utils/scroll-position';

const routes: Routes = [
  { path: '', redirectTo: 'kunde/dashboard', pathMatch: 'full' },
  {
    path: 'login',
    children: [
      { path: ':token', component: TokenLoginComponent },
      { path: '**', redirectTo: 'kunde', pathMatch: 'full' },
    ],
  },
  {
    path: '',
    canActivate: [IsAuthenticatedGuard],
    children: [
      {
        path: 'kunde',
        component: MainComponent,
        children: [
          {
            path: 'dashboard',
            loadChildren: () =>
              import('@page/dashboard').then((m) => m.DashboardModule),
            data: {
              matomo: {
                title: 'Dashboard',
              } as MatomoRouteData,
            },
          },
          {
            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,
            },
          },
          // TODO: Check if order and :processId/order is still being used
          // If not, remove these routes and the related guards and resolvers
          {
            path: 'order',
            loadChildren: () =>
              import('@page/customer-order').then((m) => m.CustomerOrderModule),
            canActivate: [CanActivateCustomerOrdersGuard],
          },
          {
            path: ':processId/order',
            loadChildren: () =>
              import('@page/customer-order').then((m) => m.CustomerOrderModule),
            canActivate: [CanActivateCustomerOrdersWithProcessIdGuard],
            resolve: {
              processId: ProcessIdResolver,
            },
          },
          {
            path: 'customer',
            loadChildren: () =>
              import('@page/customer').then((m) => m.CustomerModule),
            canActivate: [CanActivateCustomerGuard],
          },
          {
            path: ':processId/customer',
            loadChildren: () =>
              import('@page/customer').then((m) => m.CustomerModule),
            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],
            resolve: {
              processId: ProcessIdResolver,
            },
          },
          {
            path: 'pickup-shelf',
            canActivate: [ActivateProcessIdGuard],
            // NOTE: This is a workaround for the canActivate guard not being called
            loadChildren: () =>
              import('@page/pickup-shelf').then((m) => m.PickupShelfOutModule),
          },
          {
            path: ':processId/pickup-shelf',
            canActivate: [ActivateProcessIdGuard],
            loadChildren: () =>
              import('@page/pickup-shelf').then((m) => m.PickupShelfOutModule),
            resolve: {
              processId: ProcessIdResolver,
            },
          },
          { path: '**', redirectTo: 'dashboard', pathMatch: 'full' },
        ],
        resolve: { section: CustomerSectionResolver },
      },
      {
        path: 'filiale',
        component: MainComponent,
        children: [
          {
            path: 'task-calendar',
            loadChildren: () =>
              import('@page/task-calendar').then(
                (m) => m.PageTaskCalendarModule,
              ),
            canActivate: [ActivateProcessIdWithConfigKeyGuard('taskCalendar')],
          },
          {
            path: 'pickup-shelf',
            canActivate: [ActivateProcessIdWithConfigKeyGuard('pickupShelf')],
            // NOTE: This is a workaround for the canActivate guard not being called
            loadChildren: () =>
              import('@page/pickup-shelf').then((m) => m.PickupShelfInModule),
          },
          {
            path: 'goods/in',
            loadChildren: () =>
              import('@page/goods-in').then((m) => m.GoodsInModule),
            canActivate: [ActivateProcessIdWithConfigKeyGuard('goodsIn')],
          },
          {
            path: 'package-inspection',
            loadChildren: () =>
              import('@page/package-inspection').then(
                (m) => m.PackageInspectionModule,
              ),
            canActivate: [
              ActivateProcessIdWithConfigKeyGuard('packageInspection'),
            ],
          },
          {
            path: 'assortment',
            loadChildren: () =>
              import('@page/assortment').then((m) => m.AssortmentModule),
            canActivate: [ActivateProcessIdWithConfigKeyGuard('assortment')],
          },
          { path: '**', redirectTo: 'task-calendar', pathMatch: 'full' },
        ],
        resolve: { section: BranchSectionResolver },
      },
    ],
  },
  {
    path: ':tabId',
    component: MainComponent,
    resolve: { process: processResolverFn, tab: tabResolverFn },
    canActivate: [IsAuthenticatedGuard, hasTabIdGuard],
    children: [
      {
        path: 'reward',
        children: [
          {
            path: '',
            loadChildren: () =>
              import('@isa/checkout/feature/reward-catalog').then(
                (m) => m.routes,
              ),
          },
          {
            path: 'cart',
            loadChildren: () =>
              import('@isa/checkout/feature/reward-shopping-cart').then(
                (m) => m.routes,
              ),
          },
          {
            path: 'order-confirmation',
            loadChildren: () =>
              import('@isa/checkout/feature/reward-order-confirmation').then(
                (m) => m.routes,
              ),
          },
        ],
      },

      {
        path: 'return',
        loadChildren: () =>
          import('@isa/oms/feature/return-search').then((m) => m.routes),
      },
      {
        path: 'remission',
        children: [
          {
            path: 'return-receipt',
            loadChildren: () =>
              import(
                '@isa/remission/feature/remission-return-receipt-list'
              ).then((m) => m.routes),
          },
          {
            path: '',
            loadChildren: () =>
              import('@isa/remission/feature/remission-list').then(
                (m) => m.routes,
              ),
          },
        ],
      },
    ],
  },
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes, {
      bindToComponentInputs: true,
      enableTracing: false,
    }),
    TokenLoginModule,
  ],
  exports: [RouterModule],
  providers: [provideScrollPositionRestoration()],
})
export class AppRoutingModule {}
