import { Route } from '@angular/router';
import { initialDataResolver } from 'app/app.resolvers';
import { AuthGuard } from 'app/core/auth/guards/auth.guard';
import { NoAuthGuard } from 'app/core/auth/guards/noAuth.guard';
import { RoleGuard } from 'app/core/auth/guards/role.guard';
import { LayoutComponent } from 'app/layout/layout.component';

// @formatter:off
/* eslint-disable max-len */
/* eslint-disable @typescript-eslint/explicit-function-return-type */
export const appRoutes: Route[] = [

    // Redirect empty path to '/example'
    {path: '', pathMatch : 'full', redirectTo: 'example'},

    // Redirect signed-in user based on their role
    {path: 'signed-in-redirect', pathMatch : 'full', redirectTo: 'example'},

    // Auth routes for guests
    {
        path: '',
        canActivate: [NoAuthGuard],
        canActivateChild: [NoAuthGuard],
        component: LayoutComponent,
        data: {
            layout: 'empty'
        },
        children: [
            {path: 'confirmation-required', loadChildren: () => import('app/modules/auth/confirmation-required/confirmation-required.routes')},
            {path: 'forgot-password', loadChildren: () => import('app/modules/auth/forgot-password/forgot-password.routes')},
            {path: 'reset-password', loadChildren: () => import('app/modules/auth/reset-password/reset-password.routes')},
            {path: 'sign-in', loadChildren: () => import('app/modules/auth/sign-in/sign-in.routes')},
            {path: 'sign-up', loadChildren: () => import('app/modules/auth/sign-up/sign-up.routes')}
        ]
    },

    // Auth routes for authenticated users
    {
        path: '',
        canActivate: [AuthGuard],
        canActivateChild: [AuthGuard],
        component: LayoutComponent,
        data: {
            layout: 'empty'
        },
        children: [
            {path: 'sign-out', loadChildren: () => import('app/modules/auth/sign-out/sign-out.routes')},
            {path: 'unlock-session', loadChildren: () => import('app/modules/auth/unlock-session/unlock-session.routes')}
        ]
    },

    // Landing routes
    {
        path: '',
        component: LayoutComponent,
        data: {
            layout: 'empty'
        },
        children: [
            {path: 'home', loadChildren: () => import('app/modules/landing/home/home.routes')},
        ]
    },

    // Admin routes with role-based access control
    {
        path: '',
        canActivate: [AuthGuard],
        canActivateChild: [AuthGuard],
        component: LayoutComponent,
        resolve: {
            initialData: initialDataResolver
        },
        children: [
            // Dashboard - Accessible to all authenticated users
            {
                path: 'example', 
                loadChildren: () => import('app/modules/admin/example/example.routes')
            },

            // E-commerce - Management, SuperAdmin, Admin only
            {
                path: 'apps/ecommerce', 
                canActivate: [RoleGuard],
                data: { roles: ['Management', 'SuperAdmin', 'Admin'] },
                loadChildren: () => import('app/modules/admin/apps/ecommerce/ecommerce.routes')
            },

            // Machines - All roles except Refiller
            {
                path: 'machines', 
                canActivate: [RoleGuard],
                data: { roles: ['Management', 'SuperAdmin', 'Admin', 'Client', 'Servicer'] },
                loadChildren: () => import('app/modules/admin/dashboard/machines/machines.routes')
            },

            // Transactions - Management, SuperAdmin, Admin, Client
            {
                path: 'transactions', 
                canActivate: [RoleGuard],
                data: { roles: ['Management', 'SuperAdmin', 'Admin', 'Client'] },
                loadChildren: () =>import('app/modules/admin/dashboard/transactions/transactions.routes')
            },

            // Products - All roles
            {
                path: 'products', 
                canActivate: [RoleGuard],
                data: { roles: ['Management', 'SuperAdmin', 'Admin', 'Client', 'Refiller', 'Servicer'] },
                loadChildren: () =>import('app/modules/admin/dashboard/products/products.routes')
            },

            // API Integration - Management, SuperAdmin only
            {
                path: 'api-integration', 
                canActivate: [RoleGuard],
                data: { roles: ['Management', 'SuperAdmin'] },
                loadChildren: () =>import('app/modules/admin/dashboard/api-integration/api-integration.routes')
            },

            // Warehouse - Management, SuperAdmin, Admin, Refiller
            { 
                path: 'warehouse', 
                canActivate: [RoleGuard],
                data: { roles: ['Management', 'SuperAdmin', 'Admin', 'Refiller'] },
                loadChildren: () => import('app/modules/admin/dashboard/warehouse/warehouse.routes') 
            },

            // Advertisements - Management, SuperAdmin, Admin
            { 
                path: 'advertisements', 
                canActivate: [RoleGuard],
                data: { roles: ['Management', 'SuperAdmin', 'Admin'] },
                loadChildren: () => import('app/modules/admin/dashboard/advertisements/advertisements.routes') 
            },

            // User Management - Management, SuperAdmin, Admin only
            { 
                path: 'user-management', 
                canActivate: [RoleGuard],
                data: { roles: ['Management', 'SuperAdmin', 'Admin'] },
                loadChildren: () => import('app/modules/admin/dashboard/user-management/user-management.routes') 
            },

            // User - Management, SuperAdmin, Admin only
            {
                path: 'user', 
                canActivate: [RoleGuard],
                data: { roles: ['Management', 'SuperAdmin', 'Admin','Client'] },
                loadChildren: () =>import('app/modules/admin/dashboard/user/user.routes')
            },

            // Role Management - Management, SuperAdmin only
            {
                path: 'role-management', 
                canActivate: [RoleGuard],
                data: { roles: ['Management', 'SuperAdmin'] },
                loadChildren: () =>import('app/modules/admin/dashboard/role-management/role-management.routes')
            },

            // Company - Management, SuperAdmin, Admin
            { 
                path: 'company', 
                canActivate: [RoleGuard],
                data: { roles: ['Management', 'SuperAdmin', 'Admin'] },
                loadChildren: () => import('app/modules/admin/dashboard/company/company.routes') 
            },

            // Purchase Order - Management, SuperAdmin, Admin, Refiller
            { 
                path: 'purchase-order', 
                canActivate: [RoleGuard],
                data: { roles: ['Management', 'SuperAdmin', 'Admin', 'Refiller'] },
                loadChildren: () => import('app/modules/admin/dashboard/purchase-order/purchase-order.routes') 
            },

            // Offers & Coupons - Management, SuperAdmin, Admin
            { 
                path: 'offers-coupons', 
                canActivate: [RoleGuard],
                data: { roles: ['Management', 'SuperAdmin', 'Admin'] },
                loadChildren: () => import('app/modules/admin/dashboard/offers-coupons/offers-coupons.routes') 
            },

            // Warehouse Transactions - Management, SuperAdmin, Admin, Refiller
            { 
                path: 'w-transactions', 
                canActivate: [RoleGuard],
                data: { roles: ['Management', 'SuperAdmin', 'Admin', 'Refiller'] },
                loadChildren: () => import('app/modules/admin/dashboard/w-transactions/w-transactions.routes') 
            },

            // Help & Support - All authenticated users
            {
                path: 'help-support', 
                loadChildren: () =>import('app/modules/admin/dashboard/help-support/help-support.routes')
            },

            // Support History - Management, SuperAdmin, Admin
            {
                path: 'support-history', 
                canActivate: [RoleGuard],
                data: { roles: ['Management', 'SuperAdmin', 'Admin'] },
                loadChildren: () =>import('app/modules/admin/dashboard/support-history/support-history.routes')
            }   
        ]
    }
];