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';
import { Link, useParams, useSearchParams } from 'react-router-dom';
import Prescription from '@/pages/Report/Prescription/Index';
import DoctorRating from '@/pages/Report/DoctorRating/Index';

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

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: 'corporates',
          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/services/:service_code/history',
              element: <CorporateServicesHistory />,
            },
            {
              path: ':corporate_id/plans/create',
              element: <PlanCreate />,
            },
            {
              path: ':corporate_id/plans/:plan_id/history',
              element: <CorporatePlansHistory />,
            },
            {
              path: ':corporate_id/plans',
              element: <Plans />,
            },
            {
              path: ':corporate_id/benefits/create',
              element: <BenefitCreate />,
            },
            {
              path: ':corporate_id/benefits',
              element: <Benefits />,
            },
            {
              path: ':corporate_id/benefits/:benefit_id/history',
              element: <CorporateBenefitsHistory />,
            },
            {
              path: ':corporate_id/benefits/:benefit_id/edit',
              element: <CorporateBenefitsEdit />,
            },
            {
              path: ':corporate_id/members',
              element: <CorporateMembers />,
            },
            {
              path: ':corporate_id/members/:member_id/history',
              element: <CorporateHistoryMembers />,
            },

            {
              path: ':corporate_id/divisions',
              element: <CorporateDivisions />,
            },
            {
              path: ':corporate_id/divisions/create',
              element: <CorporateDivisionsCreate />,
            },
            {
              path: ':corporate_id/divisions/:id/edit',
              element: <CorporateDivisionsCreate />,
            },
            {
              path: ':corporate_id/formulariums',
              element: <CorporateFormularium />,
            },
            {
              path: ':corporate_id/formulariums/create',
              element: <CorporateFormulariumCreate />
            },
            {
              path: ':corporate_id/formulariums/:id/history',
              element: <CorporateFormulariumHistory />
            },

            {
              path: ':corporate_id/diagnosis-exclusions',
              element: <DiagnosisExclusions />,
            },
            {
                path: ':corporate_id/diagnosis-exclusions/:exclusion_id/edit',
                element: <EditDiagnosisExclusions />,
            },
            {
              path: ':corporate_id/diagnosis-exclusions/history',
              element: <DiagnosisExclusionsHistory />,
            },

            {
              path: ':corporate_id/hospitals',
              element: <CorporateHospitals />,
            },
            {
              path: ':corporate_id/hospitals/create',
              element: <HospitalCreateUpdate />,
            },
            {
              path: ':corporate_id/hospitals/edit/:id/:organization_id',
              element: <HospitalCreateUpdate />,
            },
            {
              path: ':corporate_id/hospitals/:id/history',
              element: <HospitalHistory />,
            },
            {
              path: ':corporate_id/claim-history',
              element: <CorporateClaimHistories />,
            },
            {
              path: ':corporate_id/corporate-history',
              element: <CorporateHistories />,
            },
          ],
        },
        {
          path: 'corporates/:corporate_id/edit',
          element: <CorporateCreate />,
        },
        {
            path: 'case_management', // Case Management
            element: '',
            children: [
                {
                    path: 'daily_monitoring', // Daily Monitoring
                    element: <DailyMonitoring />
                },
                {
                    path: 'daily_monitoring/:member_id/claims',
                    element: <DailyMonitoringClaims />
                },
                {
                    path: 'daily_monitoring/:member_id/claims/:claim_code/add_monitoring',
                    element: <DetailMonitoringForm />
                },
                {
                    path: 'daily_monitoring/:member_id/claims/:claim_code/list_monitoring',
                    element: <DetailMonitoringList />
                },
                {
                    path: 'laboratorium_result', // Laboratorium Result
                    element: <LaboratoriumResult />
                },
                {
                    path: 'laboratorium_result/:member_id/claims',
                    element: <LaboratoriumResultClaims />
                },
                {
                    path: 'laboratorium_result/:member_id/claims/:claim_code/add_lab_result',
                    element: <DetailLabResultForm />
                },
                {
                    path: 'laboratorium_result/:member_id/claims/:claim_code/list_lab_result',
                    element: <DetailLabResultList />
                },
                {
                    path: 'inpatient_monitoring', // Inpatient Monitoring
                    element: <InpatientMonitoring />
                },
            ]
        },
        {
          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: <MasterDiagnosisTemplate />,
        },
        {
          path: 'master/diagnosis/create',
          element: <MasterDiagnosisTemplateCreate />,
        },
        {
          path: 'master/diagnosis-template/:id/diagnosis-template-history',
          element: <MasterDiagnosisTemplateHistories />,
        },
        {
          path: 'master/diagnosis-template/:id/edit',
          element: <MasterDiagnosisTemplateCreate />,
        },

        {
          path: 'master/diagnosis/:diagnosis_template_id',
          element: <MasterDiagnosis />,
        },
        {
          path: 'master/diagnosis/:id/diagnosis-history',
          element: <MasterDiagnosisHistories />,
        },

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

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

        {
          path: 'master/formularium-template',
          element: <MasterFormulariumTemplate />,
        },
        {
          path: 'master/formularium-template/create',
          element: <MasterFormulariumTemplateCreate />,
        },
        {
          path: 'master/formularium-template/:id/formularium-template-history',
          element: <MasterFormulariumTemplateHistories />,
        },
        {
          path: 'master/formularium-template/:id/edit',
          element: <MasterFormulariumTemplateCreate />,
        },
        {
          path: 'master/formularium-template-v2',
          element: <MasterFormulariumTemplateV2 />
        },
        {
          path: 'master/formularium-template-v2/create',
          element: <MasterFormulariumTemplateCreateV2 />
        },
        {
          path: 'master/formularium-template-v2/:id/edit',
          element: <MasterFormulariumTemplateCreateV2 />
        },
        {
          path: 'master/formularium-template-v2/:id/history',
          element: <MasterFormulariumTemplateHistoriesV2 />
        },
        {
          path: 'master/formularium-template-v2/:id/detail',
          element: <MasterFormulariumTemplateDetailV2 />
        },

        {
          path: 'report/logs',
          element: <Log />,
        },
        {
          path: 'report/logs/:id',
          element: <LogCreate />,
        },
        {
          path: 'report/logs/:id/show',
          element: <LogShow />,
        },

        {
          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: 'report/prescription',
          element: <Prescription/>,
        },
        {
          path: 'report/doctorrating',
          element: <DoctorRating/>,
        },
        {
          path: 'report/linksehat-payments',
          element: <LinksehatPayment />,
        },

        {
          path: 'claims',
          element: <Claims />,
        },
        {
          path: 'claims/create',
          element: <ClaimsCreate />,
        },
        {
          path: 'claims/edit/:id',
          element: <ClaimsCreate />,
        },
        {
          path: 'claims/detail/:id',
          element: <ClaimsDetail />,
        },
        {
          path: 'claims/:id',
          element: <ClaimShow />,
        },
        {
          path: 'claim-requests',
          element: <ClaimRequests />,
        },
        {
          path: 'claim-requests/create',
          element: <ClaimRequestsCreate />,
        },
        {
          path: 'claim-requests/edit/:id',
          element: <ClaimRequestsCreate />,
        },
        {
          path: 'claim-requests/detail/:id',
          element: <ClaimRequestsDetail />,
        },
        {
          path: 'profile',
          element: <Profile />,
        },
        {
          path: 'cs-membership',
          element: <Membership />,
        },
        {
          path: 'custormer-service/request',
          element: <RequestLog />,
        },
        {
          path: 'custormer-service/request/detail/:id',
          element: <RequestLogDetail />,
        },
        {
          path: 'custormer-service/final-log',
          element: <FinalLog />,
        },
        {
          path: 'custormer-service/final-log/detail/:id',
          element: <FinalLogDetail />,
        },
      ],
    },
    // {
    //   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 CorporateHistoryMembers = Loadable(lazy(() => import('../pages/Corporates/Member/History')));

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 CorporateBenefitsHistory = Loadable(
  lazy(() => import('../pages/Corporates/Benefit/sections/History'))
);
const CorporateBenefitsEdit = Loadable(
  lazy(() => import('../pages/Corporates/Benefit/Create'))
);

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 CorporatePlansHistory = Loadable(lazy(() => import('../pages/Corporates/Plan/sections/History')));

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


const CorporateFormularium = Loadable(lazy(() => import('../pages/Corporates/Formularium/Index')));
const CorporateFormulariumCreate = Loadable(lazy(() => import('../pages/Corporates/Formularium/New/CreateForm')));
const CorporateFormulariumHistory = Loadable(lazy(() => import('../pages/Corporates/Formularium/New/History')))

/**
 * Case Management
 * -------------------------------
 */
// Daily Monitoring
const DailyMonitoring       = Loadable(lazy(() => import('../pages/CaseManagement/DailyMonitoring/index')))
const DailyMonitoringClaims = Loadable(lazy(() => import('../pages/CaseManagement/DailyMonitoring/Claim')))
const DetailMonitoringForm  = Loadable(lazy(() => import('../pages/CaseManagement/DailyMonitoring/Components/DetailMonitoringForm')))
const DetailMonitoringList  = Loadable(lazy(() => import('../pages/CaseManagement/DailyMonitoring/Components/DetailMonitoringList')))
// Laboratorium Result
const LaboratoriumResult       = Loadable(lazy(() => import('../pages/CaseManagement/LaboratoriumResult/index')))
const LaboratoriumResultClaims = Loadable(lazy(() => import('../pages/CaseManagement/LaboratoriumResult/Claim')))
const DetailLabResultForm      = Loadable(lazy(() => import('../pages/CaseManagement/LaboratoriumResult/Components/DetailLabResultForm')))
const DetailLabResultList      = Loadable(lazy(() => import('../pages/CaseManagement/LaboratoriumResult/Components/DetailLabResultList')))
// Inpatient Monitoring
const InpatientMonitoring = Loadable(lazy(() => import('../pages/CaseManagement/InpatientMonitoring/Index')))


/**
 * Customer Service
 */
// Request 
const RequestLog    = Loadable(lazy(() => import('../pages/CustomerService/Request/Index')))
const RequestLogDetail  = Loadable(lazy(() => import('../pages/CustomerService/Request/Detail')))
// Final LOG
const FinalLog      = Loadable(lazy(() => import('../pages/CustomerService/FinalLog/Index')))
const FinalLogDetail      = Loadable(lazy(() => import('../pages/CustomerService/FinalLog/Detail')))




const MasterDiagnosisTemplate = Loadable(lazy(() => import('../pages/Master/Diagnosis/Master/Index')));
const MasterDiagnosisTemplateCreate = Loadable(lazy(() => import('../pages/Master/Diagnosis/Master/CreateUpdate')));
const MasterDiagnosisTemplateHistories = Loadable(
  lazy(() => import('../pages/Master/Diagnosis/Master/History'))
);
const MasterDiagnosis = Loadable(lazy(() => import('../pages/Master/Diagnosis/Index')));
const MasterDiagnosisHistories = Loadable(
  lazy(() => import('../pages/Master/Diagnosis/History'))
);
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 Log = Loadable(lazy(() => import('../pages/Report/Log/Index')));
const LogCreate = Loadable(lazy(() => import('../pages/Report/Log/Create')));
const LogShow = Loadable(lazy(() => import('../pages/Report/Log/Show')));

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 LinksehatPayment = Loadable(lazy(() => import('../pages/Report/LinksehatPayments/Index')));

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 MasterFormulariumTemplate = Loadable(lazy(() => import('../pages/Master/Formularium/Master/Index')));
const MasterFormulariumTemplateCreate = Loadable(lazy(() => import('../pages/Master/Formularium/Master/CreateUpdate')));
const MasterFormulariumTemplateHistories = Loadable(lazy(() => import('../pages/Master/Formularium/Master/History')));

const MasterFormulariumTemplateV2 = Loadable(lazy(() => import('../pages/Master/FormulariumV2/Index')));
const MasterFormulariumTemplateCreateV2 = Loadable(lazy(() => import('../pages/Master/FormulariumV2/CreateUpdate')));
const MasterFormulariumTemplateHistoriesV2 = Loadable(lazy(() => import('../pages/Master/FormulariumV2/History')));
const MasterFormulariumTemplateDetailV2 = Loadable(lazy(() => import('../pages/Master/FormulariumV2/Detail/Index')))

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

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

const CorporateHistories = Loadable(
  lazy(() => import('../pages/Corporates/History'))
);

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 ClaimsDetail = Loadable(lazy(() => import('../pages/Claims/Detail')));
const ClaimShow = Loadable(lazy(() => import('../pages/Claims/Show')));

const ClaimRequests = Loadable(lazy(() => import('../pages/ClaimRequests/Index')));
const ClaimRequestsCreate = Loadable(lazy(() => import('../pages/ClaimRequests/CreateUpdate')));
const ClaimRequestsDetail = Loadable(lazy(() => import('../pages/ClaimRequests/Detail')));


const Membership = Loadable(lazy(() => import('../pages/Service/Membership/index')));
