import {lazy, useContext} from 'react';

import Require2FA from '../components/Require2FA';
import Loadable from 'components/Loadable';
import MainLayout from "../layout/MainLayout";
import AbilityContext from "../components/AbilityProvider";
import {handleRouteAbility} from "../utils/CommonFunction";
import {Navigate} from "react-router";

// render - login
const SettingPage = Loadable(lazy(() => import('pages/lionerSettingPage')));
const LogoutPage = Loadable(lazy(() => import('pages/extra-pages/LogoutPage')));
const PasswordPolicyPage = Loadable(lazy(()=> import('pages/lionerPasswordPolicyPage')))
const UserSearchPage = Loadable(lazy(()=>import ('pages/lionerUserSearchPage')));
const ConsultantSearchPage = Loadable(lazy(()=>import ('pages/pdf/ConsultantSearchPage')));
const ConsultantMaintainPage = Loadable(lazy(() => import('pages/pdf/ConsultantMaintainPage')));
const CategoryPage = Loadable(lazy(()=>import ('pages/lionerCategoryPage')));
const UserMaintainPage = Loadable(lazy(() => import('pages/lionerUserDetailPage')));
const UserGroupSearchPage = Loadable(lazy(() => import('pages/lionerUserGroupSearchPage')));
const UserGroupDetailPage = Loadable(lazy(() => import('pages/lionerUserGroupDetailPage')));
const DivisionSearchPage = Loadable(lazy(() => import('pages/lionerDivisionSearch')));
const DivisionEditPage = Loadable(lazy(() => import('pages/lionerDivisionEdit')));
const PromotionChannelPage = Loadable(lazy(() => import('pages/lionerPromotionChannelPage')));
const TagPage = Loadable(lazy(() => import('pages/lionerTagPage')));
const AuditLogPage = Loadable(lazy(() => import('pages/lionerAuditTrailPage')));
const LoginLogPage = Loadable(lazy(() => import('pages/lionerLoginLog')));
const UserActionLogPage = Loadable(lazy(() => import('pages/lionerUserActionLog')));
const EmailConfigPage = Loadable(lazy(() => import('pages/lionerEmailConfig')));
const GenerateReminderPage = Loadable(lazy(() => import('pages/lionerManualButtonPage')));
const ClientDepartmentPage = Loadable(lazy(() => import('pages/lionerClientDepartmentPage')));
const ProfilePage = Loadable(lazy(() => import('pages/profile/profile')));
const FormSigPageSearchPanel = Loadable(lazy(() => import('pages/formSigPage')));

// ==============================|| AUTH ROUTING ||============================== //

const SettingRoutes = () => {
    const ability = useContext(AbilityContext);
    
    return {
        path: '/',
        element: <MainLayout />,
        children: [
            {
                path: 'category',
                element: (
                    handleRouteAbility(
                        ability.can('MAINTAIN', 'CATEGORY'),
                        <CategoryPage />,
                        <Navigate to="/" />
                    )
                ),
            },
            {
                path: 'tag',
                element:(
                    handleRouteAbility(
                        ability.can('MAINTAIN', 'TAG'),
                        <TagPage />,
                        <Navigate to="/" />
                    )
                ),
            },
            {
                path: 'promotionChannel',
                element: (
                    handleRouteAbility(
                        ability.can('MAINTAIN', 'PROMOTION_CHANNEL'),
                        <PromotionChannelPage />,
                        <Navigate to="/" />
                    )
                ),
            },
            {
                path: 'clientDepartment',
                element: (
                    handleRouteAbility(
                        ability.can('MAINTAIN', 'CLIENT_DEPARTMENT'),
                        <ClientDepartmentPage />,
                        <Navigate to="/" />
                    )
                ),
            },
            {
                path: 'division',
                element: (
                    handleRouteAbility(
                        ability.can('MAINTAIN', 'DIVISION'),
                        <DivisionSearchPage />,
                        <Navigate to="/" />
                    )
                ),
            },
            {
                path: 'division/:id',
                element:(
                    handleRouteAbility(
                        ability.can('MAINTAIN', 'DIVISION'),
                        <DivisionEditPage />,
                        <Navigate to="/" />
                    )
                ),
            },
            {
                path: 'usergroupSearchview',
                element: (
                    <Require2FA>
                    {handleRouteAbility(
                        ability.can('MAINTAIN', 'USER_GROUP'),
                        <UserGroupSearchPage />,
                        <Navigate to="/" />
                    )}
                    </Require2FA>
                ),
            },
            {
                path: 'userGroup/:id',
                element:(
                    <Require2FA>
                    {handleRouteAbility(
                        ability.can('MAINTAIN', 'USER_GROUP'),
                        <UserGroupDetailPage />,
                        <Navigate to="/" />
                    )}
                    </Require2FA>
                ),
            },
            {
                path: 'user/:id',
                element: (
                    <Require2FA>
                    {handleRouteAbility(
                        ability.can('VIEW', 'USER'),
                        <UserMaintainPage />,
                        <Navigate to="/" />
                    )}
                    </Require2FA>
                ),
            },
            {
                path: 'userSearchview',
                element:(
                    <Require2FA>
                    {handleRouteAbility(
                        ability.can('VIEW', 'USER'),
                        <UserSearchPage />,
                        <Navigate to="/" />
                    )}
                    </Require2FA>
                ),
            },
            {
                path: 'consultant',
                element:  (
                    <Require2FA>
                    {handleRouteAbility(
                        ability.can('VIEW', 'USER'),
                        <ConsultantSearchPage />,
                        <Navigate to="/" />
                    )}
                    </Require2FA>
                ),
            },
            {
                path: 'consultant/:id',
                element: (
                    <Require2FA>
                    {handleRouteAbility(
                        ability.can('VIEW', 'USER'),
                        <ConsultantMaintainPage />,
                        <Navigate to="/" />
                    )}
                    </Require2FA>
                ),
            },
            
            {
                path: 'setting',
                element: (
                    <Require2FA>
                    {handleRouteAbility(
                        ability.can('MANAGE', 'SYSTEM_CONFIGURATION'),
                        <SettingPage />,
                        <Navigate to="/" />
                    )}
                    </Require2FA>
                ),
            },
            {
                path: 'auditLog',
                element:(
                    <Require2FA>
                    {handleRouteAbility(
                        ability.can('VIEW', 'AUDIT_LOG'),
                        <AuditLogPage />,
                        <Navigate to="/" />
                    )}
                    </Require2FA>
                ),
            },
            {
                path: 'userActionLog',
                element: (
                    <Require2FA>
                    {handleRouteAbility(
                        ability.can('VIEW', 'USER'),
                        <UserActionLogPage />,
                        <Navigate to="/" />
                    )}
                    </Require2FA>
                ),
            },
            {
                path: 'loginLog',
                element: (
                    <Require2FA>
                    {handleRouteAbility(
                        ability.can('VIEW', 'LOGIN_LOG'),
                        <LoginLogPage />,
                        <Navigate to="/" />
                    )}
                    </Require2FA>
                ),
            },
            {
                path: 'passwordpolicy',
                element: (
                    handleRouteAbility(
                        ability.can('MANAGE', 'SYSTEM_CONFIGURATION'),
                        <PasswordPolicyPage />,
                        <Navigate to="/" />
                    )
                ),
            },
            {
                path: 'profile',
                element: (
                    handleRouteAbility(
                    true, // Or use a specific ability like ability.can('VIEW', 'PROFILE')
                    <ProfilePage />,
                    <Navigate to="/" />
                    )
                ),
            },
            {
                path: 'emailConfig',
                element: (
                    handleRouteAbility(
                        ability.can('MANAGE', 'SYSTEM_CONFIGURATION'),
                        <EmailConfigPage />,
                        <Navigate to="/" />
                    )
                ),
            },
            {
                path: 'generateReminder',
                element: (
                    handleRouteAbility(
                        ability.can('MANAGE', 'SYSTEM_CONFIGURATION'),
                        <GenerateReminderPage />,
                        <Navigate to="/" />
                    )
                ),
            },
            {
                path: 'formSigPage',
                element: (
                    handleRouteAbility(
                        ability.can('MANAGE', 'SYSTEM_CONFIGURATION'),
                        <FormSigPageSearchPanel />,
                        <Navigate to="/" />
                    )
                ),
            },
            {
                path: 'logout',
                element: <LogoutPage />
            }
        ]
    }
};

export default SettingRoutes;
