import { Suspense, lazy, ElementType } from 'react';
import { Navigate, useRoutes, useLocation } from 'react-router-dom';
// layouts
import DashboardLayout from '../layouts/dashboard';
import LogoOnlyLayout from '../layouts/LogoOnlyLayout';
// components
import LoadingScreen from '../components/LoadingScreen';
import GuestGuard from '../guards/GuestGuard';
import { RegisterForm } from '../sections/auth/register';
import Register from '../pages/auth/Register';
import VerifyCode from '../pages/auth/VerifyCode';
import { AuthProvider } from '../contexts/LaravelAuthContext';
import AuthGuard from '../guards/AuthGuard';

// ----------------------------------------------------------------------

const Loadable = (Component: ElementType) => (props: any) => {
  // eslint-disable-next-line react-hooks/rules-of-hooks
  const { pathname } = useLocation();

  return (
    <Suspense fallback={<LoadingScreen isDashboard={pathname.includes('/dashboard')} />}>
      <Component {...props} />
    </Suspense>
  );
};

export default function Router() {
  return useRoutes([
    {
      path: 'auth',
      children: [
        {
          path: 'login',
          element: (
            <AuthProvider>
              <GuestGuard>
                <Login />
              </GuestGuard>
            </AuthProvider>
          ),
        },
        {
          path: 'register',
          element: (
            <AuthProvider>
              <GuestGuard>
                <RegisterForm />
              </GuestGuard>
            </AuthProvider>
          ),
        },
        // { path: 'login-unprotected', element: <Login /> },
        // { path: 'register-unprotected', element: <Register /> },
        { path: 'reset-password', element: <ResetPassword /> },
        { path: 'forget-password', element: <ForgetPassword /> },
        // { path: 'verify', element: <VerifyCode /> },
      ],
    },
    // {
    //   path: '/',
    //   element: <Navigate to="/dashboard/one" replace />,
    // },
    {
      path: '/',
      element: (
        <AuthProvider>
          <AuthGuard>
            <DashboardLayout />
          </AuthGuard>
        </AuthProvider>
      ),
      children: [
        { element: <Navigate to="/dashboard" replace />, index: true },
        {
          path: 'dashboard',
          element: <Dashboard />,
        },
        {
          path: 'members',
          element: <Members />,
        },
        {
          path: 'corporates',
          element: <Corporate />,
        },
        {
          path: 'corporates/create',
          element: <CorporateCreate />,
        },
        {
          path: 'corporate',
          element: <ConfigLayout />, // Layout that use the context
          children: [
            {
              path: ':corporate_id',
              element: <CorporateShow />,
            },
            {
              path: ':corporate_id/services',
              element: <CorporateServices />,
            },
            {
              path: ':corporate_id/services/:service_code',
              element: <CorporateServicesCreate />,
            },
            {
              path: ':corporate_id/plans/create',
              element: <PlanCreate />,
            },
            {
              path: ':corporate_id/plans',
              element: <Plans />,
            },
            {
              path: ':corporate_id/benefits/create',
              element: <BenefitCreate />,
            },
            {
              path: ':corporate_id/benefits',
              element: <Benefits />,
            },

            {
              path: ':corporate_id/members',
              element: <CorporateMembers />,
            },

            {
              path: ':corporate_id/divisions',
              element: <CorporateDivisions />,
            },
            {
              path: ':corporate_id/divisions/create',
              element: <CorporateDivisionsCreate />,
            },
            {
              path: ':corporate_id/divisions/:id/edit',
              element: <CorporateDivisionsCreate />,
            },
            {
              path: ':corporate_id/formularium',
              element: <CorporateFormularium />,
            },

            {
              path: ':corporate_id/diagnosis-exclusions',
              element: <DiagnosisExclusions />,
            },

            {
              path: ':corporate_id/hospitals',
              element: <CorporateHospitals />,
            },

            {
              path: ':corporate_id/claim-history',
              element: <CorporateClaimHistories />,
            },
          ],
        },
        {
          path: 'corporates/:corporate_id/edit',
          element: <CorporateCreate />,
        },

        {
          path: 'corporates/:corporate_id/corporate-plans/create',
          element: <CorporatePlanCreate />,
        },
        {
          path: 'corporates/:corporate_id/corporate-plans/:id/edit',
          element: <CorporatePlanCreate />,
        },
        {
          path: 'corporates/:corporate_id/corporate-plans',
          element: <CorporatePlans />,
        },

        {
          path: 'corporates/:corporate_id/corporate-benefits/create',
          element: <CorporateBenefitsCreate />,
        },
        {
          path: 'corporates/:corporate_id/corporate-benefits',
          element: <CorporateBenefits />,
        },
        {
          path: 'corporates/:corporate_id/corporate-benefits/:id/edit',
          element: <CorporateBenefitsCreate />,
        },

        {
          path: 'master/doctors',
          element: <MasterDoctors />,
        },
        {
          path: 'master/doctors/create',
          element: <MasterDoctorsCreate />,
        },
        {
          path: 'master/doctors/:id/edit',
          element: <MasterDoctorsCreate />,
        },
        {
          path: 'master/hospitals',
          element: <MasterHospitals />,
        },
        {
          path: 'master/hospitals/create',
          element: <MasterHospitalsCreate />,
        },
        {
          path: 'master/hospitals/:id/edit',
          element: <MasterHospitalsCreate />,
        },
        {
          path: 'master/diagnosis',
          element: <MasterDiagnosis />,
        },

        {
          path: 'master/drugs',
          element: <MasterDrug />,
        },

        {
          path: 'master/formularium',
          element: <MasterFormularium />,
        },
        {
          path: 'master/formularium/create',
          element: <MasterFormulariumCreate />,
        },

        {
          path: 'report/appointments',
          element: <Appointment />,
        },
        {
          path: 'report/appointments/:id',
          element: <AppointmentCreate />,
        },
        {
          path: 'report/appointments/:id/show',
          element: <AppointmentShow />,
        },
        {
          path: 'report/appointments/:id/edit',
          element: <AppointmentCreate />,
        },
        {
          path: 'report/live-chat',
          element: <Livechat />,
        },
        {
          path: 'report/live-chat/:id',
          element: <LivechatCreate />,
        },
        {
          path: 'report/live-chat/:id/show',
          element: <LivechatShow />,
        },
        {
          path: 'report/live-chat/:id/edit',
          element: <LivechatCreate />,
        },

        {
          path: 'claims',
          element: <Claims />,
        },
        {
          path: 'claim-requests',
          element: <ClaimRequests />,
        },
        {
          path: 'claims/create',
          element: <ClaimsCreate />,
        },
        {
          path: 'claims/:id',
          element: <ClaimShow />,
        },
        {
          path: 'profile',
          element: <Profile />,
        },
      ],
    },
    // {
    //   path: '/dashboard',
    //   element: <DashboardLayout />,
    //   children: [
    //     { element: <Navigate to="/dashboard/one" replace />, index: true },
    //     { path: 'one', element:
    //         <AuthProvider><PageOne /></AuthProvider> },
    //     { path: 'two', element:
    //         <AuthProvider><PageTwo /></AuthProvider> },
    //     { path: 'three', element:
    //         <AuthProvider><PageThree /></AuthProvider> },
    //     {
    //       path: 'user',
    //       children: [
    //         { element: <Navigate to="/dashboard/user/four" replace />, index: true },
    //         { path: 'four', element: <AuthProvider><PageFour /></AuthProvider> },
    //         { path: 'six', element: <AuthProvider><PageSix /> </AuthProvider> },
    //       ],
    //     },
    //   ],
    // },
    {
      path: '*',
      element: <LogoOnlyLayout />,
      children: [
        { path: '404', element: <NotFound /> },
        { path: '*', element: <Navigate to="/404" replace /> },
      ],
    },
    { path: '*', element: <Navigate to="/404" replace /> },
  ]);
}

const Login = Loadable(lazy(() => import('../pages/auth/Login')));
const ResetPassword = Loadable(lazy(() => import('../pages/auth/ResetPassword')));
const ForgetPassword = Loadable(lazy(() => import('../pages/auth/ForgetPassword')));

// Dashboard
const Dashboard = Loadable(lazy(() => import('../pages/Dashboard')));
const NotFound = Loadable(lazy(() => import('../pages/Page404')));

// Members
const Members = Loadable(lazy(() => import('../pages/Members/Index')));
const MedicinesCreate = Loadable(lazy(() => import('../pages/Medicines/Create')));

// Corporate
const ConfigLayout = Loadable(lazy(() => import('../pages/Corporates/ConfigLayout')));

const Corporate = Loadable(lazy(() => import('../pages/Corporates/Index')));
const CorporateCreate = Loadable(lazy(() => import('../pages/Corporates/CreateUpdate')));
const CorporateShow = Loadable(lazy(() => import('../pages/Corporates/Show')));

const CorporateDivisions = Loadable(lazy(() => import('../pages/Corporates/Division/Index')));
const CorporateDivisionsCreate = Loadable(
  lazy(() => import('../pages/Corporates/Division/CreateUpdate'))
);

const CorporateMembers = Loadable(lazy(() => import('../pages/Corporates/Member/Index')));

const BenefitCreate = Loadable(lazy(() => import('../pages/Corporates/Benefit/Create')));
const Benefits = Loadable(lazy(() => import('../pages/Corporates/Benefit/Index')));

const CorporateBenefitsCreate = Loadable(
  lazy(() => import('../pages/Corporates/CorporateBenefit/CreateUpdate'))
);
const CorporateBenefits = Loadable(
  lazy(() => import('../pages/Corporates/CorporateBenefit/Index'))
);

const CorporatePlanCreate = Loadable(
  lazy(() => import('../pages/Corporates/CorporatePlan/CreateUpdate'))
);
const CorporatePlans = Loadable(lazy(() => import('../pages/Corporates/CorporatePlan/Index')));

const PlanCreate = Loadable(lazy(() => import('../pages/Corporates/Plan/Create')));
const Plans = Loadable(lazy(() => import('../pages/Corporates/Plan/Index')));

const DiagnosisExclusions = Loadable(
  lazy(() => import('../pages/Corporates/DiagnosisExclusion/Index'))
);

const CorporateFormularium = Loadable(lazy(() => import('../pages/Corporates/Formularium/Index')));

const MasterDiagnosis = Loadable(lazy(() => import('../pages/Master/Diagnosis/Index')));
const MasterDoctors = Loadable(lazy(() => import('../pages/Master/Doctors/Index')));
const MasterDoctorsCreate = Loadable(lazy(() => import('../pages/Master/Doctors/Create')));
const MasterHospitals = Loadable(lazy(() => import('../pages/Master/Hospitals/Index')));
const MasterHospitalsCreate = Loadable(lazy(() => import('../pages/Master/Hospitals/Create')));

const Appointment = Loadable(lazy(() => import('../pages/Report/Appointments/Index')));
const AppointmentCreate = Loadable(lazy(() => import('../pages/Report/Appointments/Create')));
const AppointmentShow = Loadable(lazy(() => import('../pages/Report/Appointments/Show')));

const Livechat = Loadable(lazy(() => import('../pages/Report/Livechat/Index')));
const LivechatCreate = Loadable(lazy(() => import('../pages/Report/Livechat/Create')));
const LivechatShow = Loadable(lazy(() => import('../pages/Report/Livechat/Show')));

const MasterDrug = Loadable(lazy(() => import('../pages/Master/Drug/Index')));

const MasterFormularium = Loadable(lazy(() => import('../pages/Master/Formularium/Index')));
const MasterFormulariumCreate = Loadable(lazy(() => import('../pages/Master/Formularium/Create')));

const CorporateServices = Loadable(lazy(() => import('../pages/Corporates/Services/Index')));
const CorporateServicesCreate = Loadable(lazy(() => import('../pages/Corporates/Services/Create')));

const CorporateHospitals = Loadable(lazy(() => import('../pages/Corporates/Hospital/Index')));
const CorporateClaimHistories = Loadable(
  lazy(() => import('../pages/Corporates/ClaimHistory/Index'))
);

const Profile = Loadable(lazy(() => import('../pages/Profile/Index')));

const Claims = Loadable(lazy(() => import('../pages/Claims/Index')));
const ClaimsCreate = Loadable(lazy(() => import('../pages/Claims/CreateUpdate')));
const ClaimShow = Loadable(lazy(() => import('../pages/Claims/Show')));

const ClaimRequests = Loadable(lazy(() => import('../pages/ClaimRequests/Index')));