import { Routes } from '@angular/router';

import { SimpleLayoutComponent } from '../../core/layout/simple-layout/simple-layout.component';
import { StoreLayoutComponent } from '../../core/layout/store-layout/store-layout.component';
import { authGuard, guestOnlyGuard } from '../../core/services/auth/auth.guards';
import { LoginPageComponent } from './pages/login-page/login-page.component';
import { hasMenuGuard } from '../../core/guards/menu.guard';
import { productDetailResolver } from './pages/product-detail-page/product-detail-page.resolver';
import { RecoverPasswordCodePageComponent } from './pages/recover-password-code-page/recover-password-code-page.component';
import { RecoverPasswordPageComponent } from './pages/recover-password-page/recover-password-page.component';
import { ResetPasswordPageComponent } from './pages/reset-password-page/reset-password-page.component';
import { RegisterPageComponent } from './pages/register-page/register-page.component';
import { StoreHomePageComponent } from './pages/store-home-page/store-home-page.component';
import { storeHomeCatalogResolver } from './pages/store-home-page/store-home-page.resolver';
import { checkoutPendingPurchaseGuard } from './pages/checkout-page/checkout-page.guard';

export const routes: Routes = [
  {
    path: '',
    component: StoreLayoutComponent,
    children: [
      {
        path: '',
        component: StoreHomePageComponent,
        canActivate: [hasMenuGuard('index')],
        resolve: {
          catalogData: storeHomeCatalogResolver,
        },
      },
      {
        path: 'login',
        canActivate: [guestOnlyGuard],
        component: SimpleLayoutComponent,
        children: [
          {
            path: '',
            component: LoginPageComponent,
          },
        ],
      },
      {
        path: 'register',
        canActivate: [guestOnlyGuard],
        component: SimpleLayoutComponent,
        children: [
          {
            path: '',
            component: RegisterPageComponent,
          },
        ],
      },
      {
        path: 'recuperar-contrasena',
        canActivate: [guestOnlyGuard],
        component: SimpleLayoutComponent,
        children: [
          {
            path: '',
            component: RecoverPasswordPageComponent,
          },
          {
            path: 'codigo',
            component: RecoverPasswordCodePageComponent,
          },
          {
            path: 'restablecer',
            component: ResetPasswordPageComponent,
          },
        ],
      },
      {
        path: 'buscar',
        loadComponent: () =>
          import('./pages/search-page/search-page.component').then((m) => m.SearchPageComponent),
      },
      {
        path: 'categoria/:id',
        loadComponent: () =>
          import('./pages/category-items-page/category-items-page.component').then(
            (m) => m.CategoryItemsPageComponent,
          ),
      },
      {
        path: 'producto/:id',
        canActivate: [hasMenuGuard('product.detail')],
        resolve: {
          productDetailData: productDetailResolver,
        },
        loadComponent: () =>
          import('./pages/product-detail-page/product-detail-page.component').then(
            (m) => m.ProductDetailPageComponent,
          ),
      },
      {
        path: 'checkout/status',
        component: SimpleLayoutComponent,
        children: [
          {
            path: ':id',
            loadComponent: () =>
              import('./pages/purchase-status-page/purchase-status-page.component').then(
                (m) => m.PurchaseStatusPageComponent,
              ),
          },
        ],
      },
      {
        path: 'checkout/:id',
        canActivate: [authGuard, hasMenuGuard('checkout')],
        canDeactivate: [checkoutPendingPurchaseGuard],
        loadComponent: () =>
          import('./pages/checkout-page/checkout-page.component').then(
            (m) => m.CheckoutPageComponent,
          ),
      },
      {
        path: 'ayuda',
        canActivate: [hasMenuGuard('help')],
        data: {
          menuCode: 'help',
          navigationLabel: 'Secciones de ayuda',
        },
        loadComponent: () =>
          import('./components/menu-page-layout/menu-page-layout.component').then(
            (m) => m.MenuPageLayoutComponent,
          ),
        children: [
          {
            path: 'preguntas-frecuentes',
            canActivate: [hasMenuGuard('help.faq')],
            loadComponent: () =>
              import('./pages/help-page/pages/faq-page/faq-page').then((m) => m.FaqPage),
          },
          {
            path: 'contacto',
            canActivate: [hasMenuGuard('help.contact')],
            loadComponent: () =>
              import('./pages/help-page/pages/contact-page/contact-page').then(
                (m) => m.ContactPage,
              ),
          },
          {
            path: '',
            redirectTo: 'preguntas-frecuentes',
            pathMatch: 'full',
          },
          {
            path: '**',
            loadComponent: () =>
              import('./pages/help-page/pages/pending-help-page/pending-help-page').then(
                (m) => m.PendingHelpPage,
              ),
          },
        ],
      },
      {
        path: 'mi-cuenta',
        canActivate: [authGuard, hasMenuGuard('account')],
        data: {
          menuCode: 'account',
          navigationLabel: 'Secciones de mi cuenta',
        },
        loadComponent: () =>
          import('./components/menu-page-layout/menu-page-layout.component').then(
            (m) => m.MenuPageLayoutComponent,
          ),
        children: [
          {
            path: 'datos-personales',
            canActivate: [hasMenuGuard('account.profile')],
            loadComponent: () =>
              import('./pages/account-page/pages/profile-page/profile-page').then(
                (m) => m.ProfilePage,
              ),
          },
          {
            path: 'compras',
            canActivate: [hasMenuGuard('account.purchases')],
            loadComponent: () =>
              import('./pages/account-page/pages/purchases-page/purchases-page').then(
                (m) => m.PurchasesPage,
              ),
          },
          {
            path: 'compras/:id',
            loadComponent: () =>
              import('./pages/account-page/pages/purchase-detail-page/purchase-detail-page').then(
                (m) => m.PurchaseDetailPage,
              ),
          },
          {
            path: 'tickets',
            canActivate: [hasMenuGuard('account.tickets')],
            loadComponent: () =>
              import('./pages/account-page/pages/tickets-page/tickets-page').then(
                (m) => m.TicketsPage,
              ),
          },
          {
            path: '',
            redirectTo: 'datos-personales',
            pathMatch: 'full',
          },
        ],
      },
    ],
  },
];
