import React, { lazy } from "react";
import { IRouteDef } from "../types/routes";

const UserRecordsLayout = lazy(
  () => import("../components/userRecords/userRecordsLayout"),
);
const UserRecordsPage = lazy(
  () => import("@/record-management/pages/User/userRecords"),
);
const Dashboard = lazy(
  () => import("@/record-management/pages/User/dashboard"),
);
const DashboardRO = lazy(
  () => import("@/record-management/pages/RecordOfficer/dashboard"),
);
const UploadTeeterAndSignaturePage = lazy(
  () => import("@/record-management/pages/uploadTeeterAndSignature"),
);
const EditUserRecordsPage = lazy(
  () => import("@/record-management/pages/User/editUserRecordPage"),
);
const AddUserRecordsPage = lazy(
  () => import("@/record-management/pages/User/addUserRecordPage"),
);

const cc = lazy(
  () => import("@/record-management/components/userIncoming/viewIncoming/cc"),
);
const IncomingViewer = lazy(
  () =>
    import("@/record-management/components/userIncoming/viewIncoming/incoming"),
);
const internal = lazy(
  () =>
    import("@/record-management/components/userIncoming/viewIncoming/internal"),
);
const forYourReference = lazy(
  () =>
    import("@/record-management/components/userIncoming/viewIncoming/forYourReference"),
);

const ExternalIncomingRecordsPage = lazy(
  () => import("@/record-management/pages/User/externalIncomingRecords"),
);
const InternalIncomingRecordsPage = lazy(
  () => import("@/record-management/pages/User/internalIncomingRecords"),
);
const CCIncomingRecordsPage = lazy(
  () => import("@/record-management/pages/User/ccIncomingRecords"),
);
const ForYourReferenceIncomingRecordsPage = lazy(
  () =>
    import("@/record-management/pages/User/forYourReferenceIncomingRecords"),
);
const ApproveRecordsPage = lazy(
  () => import("@/record-management/pages/User/approveRecords"),
);
const ApproveRecordPage = lazy(
  () =>
    import("@/record-management/components/Approval/approve/approveRecordPage"),
);
const UserIncomingRecordsLayout = lazy(
  () =>
    import("@/record-management/components/userIncoming/userIncomingLayout"),
);

const IncomingRecordsPage = lazy(
  () => import("@/record-management/pages/RecordOfficer/incomingRecords"),
);
const IncomingRecordsHistory = lazy(
  () =>
    import("@/record-management/pages/RecordOfficer/incomingRecordsHistory"),
);
const IncomingnonSmartExternal = lazy(
  () => import("@/record-management/pages/RecordOfficer/nonSmartExternal"),
);
const AddIncomingRecordPage = lazy(
  () => import("@/record-management/pages/RecordOfficer/AddIncomingRecord"),
);
const SoftDeleteRO = lazy(
  () =>
    import("@/record-management/components/recordOfficer/incomingRecords/softDelete"),
);
const DelegationRecordsPage = lazy(
  () =>
    import("@/record-management/pages/User/Delegation/delegationRecordsPage"),
);
const EditDelegationPage = lazy(
  () => import("@/record-management/components/delegation/editDelegation"),
);
const delegationLetterView = lazy(
  () =>
    import("@/record-management/components/delegation/viewDelegationLetter"),
);

const OutgoingRecordsPage = lazy(
  () => import("@/record-management/pages/RecordOfficer/outgoingRecords"),
);
const PendingRecordsPage = lazy(
  () => import("@/record-management/pages/RecordOfficer/pendingRecordsPage"),
);
const ConvertRecordsPage = lazy(
  () => import("@/record-management/pages/RecordOfficer/convertRecordsPage"),
);
const ViewRecordDetailsPage = lazy(
  () => import("@/record-management/pages/User/viewRecordDetailsPage"),
);
const ViewRecordDetailsCollaboration = lazy(
  () =>
    import("@/record-management/components/collaborations/viewCollaborationRecord"),
);
const ViewLetter = lazy(
  () => import("@/record-management/pages/User/ViewLetter"),
);

const ViewOutgoingRecordOfficerDetailsPage = lazy(
  () =>
    import("@/record-management/components/recordOfficer/outgoingRecords/viewOutgoing"),
);
const ViewOutgoingRecordOfficerDetailsPage2 = lazy(
  () =>
    import("@/record-management/components/recordOfficer/outgoingRecords/outgoingview"),
);
const viewOutgoing = lazy(
  () =>
    import("@/record-management/components/recordOfficer/outgoingRecords/view"),
);
const viewPending = lazy(
  () =>
    import("@/record-management/components/pendingRecords/viewPendingRecords"),
);
const ViewApproveRecordDetailsPage = lazy(
  () => import("@/record-management/pages/Approval/viewApprovalRecordPage"),
);
const ViewProcessedApprovalDetailsPage = lazy(
  () =>
    import("@/record-management/pages/Approval/viewProcessedApprovalDetailsPage"),
);
const RecordOfficerSettingsPage = lazy(
  () => import("@/record-management/pages/Settings/recordOfficerSettingsPage"),
);
const RecordPreviewPage = lazy(
  () => import("@/record-management/pages/RecordPreviewPage"),
);
const SoftDelete = lazy(
  () => import("@/record-management/components/userRecords/softDelete"),
);

const ViewLetterTemplatePage = lazy(
  () =>
    import("@/record-management/pages/LetterTemplates/viewLetterTemplatePage"),
);
const AddLetterTemplatePage = lazy(
  () =>
    import("@/record-management/pages/LetterTemplates/addLetterTemplatePage"),
);
const EditLetterTemplatePage = lazy(
  () =>
    import("@/record-management/pages/LetterTemplates/editLetterTemplatePage"),
);
const AddDelegationPage = lazy(
  () => import("@/record-management/pages/User/Delegation/addDelegationPage"),
);
const Collaborations = lazy(
  () => import("../components/collaborations/collaboration"),
);
const RecordOfficerIncomingViewer = lazy(
  () =>
    import("@/record-management/components/recordOfficer/incomingRecords/viewIncoming"),
);
const RecordOfficerIncomingEdit = lazy(
  () => import("@/record-management/pages/RecordOfficer/EditIncomingRecord"),
);
const AdvancedDashboardPage = lazy(
  () => import("@/pages/AdvancedDashboardPage"),
);

const ReviewAllPendingUsers = lazy(
  () => import("@/user-management/components/content/AllPendingUsers"),
);

export const approvalPermissionKeys = ["can:sendToRO", "can:approveRecords"];

export const hasApprovalPermission = (permissions: string[]) =>
  approvalPermissionKeys.some((key) => permissions.includes(key));

const UserRoutesDef = (
  permissions: string[],
  selectedPositionPermissions: string[] = [],
): IRouteDef[] => {
  const routes: IRouteDef[] = [
    { path: "/dashboard", element: React.createElement(Dashboard) },
    {
      path: "/advanced-dashboard",
      element: React.createElement(AdvancedDashboardPage),
    },

    {
      path: "/userRecords",
      element: React.createElement(UserRecordsLayout),
      children: [
        { index: true, element: React.createElement(UserRecordsPage) },
        {
          path: "addUserRecord/:id?",
          element: React.createElement(AddUserRecordsPage),
        },
        {
          path: "editUserRecord/:id",
          element: React.createElement(EditUserRecordsPage),
        },

        {
          path: "viewRecord/:id",
          element: React.createElement(ViewRecordDetailsPage),
        },
        {
          path: "record-preview",
          element: React.createElement(RecordPreviewPage),
        },
        {
          path: "softDelete",
          element: React.createElement(SoftDelete),
        },
      ],
    },

    { path: "/collaborations", element: React.createElement(Collaborations) },
    {
      path: "/collaborations/viewRecord/:id",
      element: React.createElement(ViewRecordDetailsCollaboration),
    },

    { path: "/viewLetterPage/:id", element: React.createElement(ViewLetter) },

    {
      path: "/delegation",
      element: React.createElement(DelegationRecordsPage),
    },
    {
      path: "/delegation/new",
      element: React.createElement(AddDelegationPage),
    },
    {
      path: "/editDelegation/:id",
      element: React.createElement(EditDelegationPage),
    },
    {
      path: "/viewDelegationLetter/:id",
      element: React.createElement(delegationLetterView),
    },

    {
      path: "/userIncoming",
      element: React.createElement(UserIncomingRecordsLayout),
      children: [
        {
          index: true,
          element: React.createElement(ExternalIncomingRecordsPage),
        },
        {
          path: "internal",
          element: React.createElement(InternalIncomingRecordsPage),
        },
        {
          path: "cc",
          element: React.createElement(CCIncomingRecordsPage),
        },
        {
          path: "for-your-reference",
          element: React.createElement(ForYourReferenceIncomingRecordsPage),
        },
      ],
    },

    {
      path: "/viewIncoming/internal/:id",
      element: React.createElement(internal),
    },
    {
      path: "/viewIncoming/incoming/:id",
      element: React.createElement(IncomingViewer),
    },
    {
      path: "/uploadTeeterandSignature",
      element: React.createElement(UploadTeeterAndSignaturePage),
    },
    { path: "/viewIncoming/cc/:id", element: React.createElement(cc) },
    {
      path: "/viewIncoming/for-your-reference/:id",
      element: React.createElement(forYourReference),
    },
  ];

  const hasPermissionToApproveUser = ["can:activateEmployee"];
  if (hasPermissionToApproveUser.some((key) => permissions.includes(key))) {
    routes.push({
      path: "/employees/pending",
      element: React.createElement(ReviewAllPendingUsers),
    });
  }
  if (hasApprovalPermission(selectedPositionPermissions)) {
    routes.push(
      { path: "/approval", element: React.createElement(ApproveRecordsPage) },
      {
        path: "/approval/:recordId",
        element: React.createElement(ApproveRecordPage),
      },
      // {
      //   path: "/uploadTeeterandSignature",
      //   element: React.createElement(UploadTeeterAndSignaturePage),
      // },
      {
        path: "/viewApproval/:id",
        element: React.createElement(ViewApproveRecordDetailsPage),
      },
      {
        path: "/viewProcessedApproval/:id",
        element: React.createElement(ViewProcessedApprovalDetailsPage),
      },
    );
  }
  if (permissions.includes("can:dispatchRecords")) {
    return [
      {
        path: "/recordOfficer/outgoing",
        element: React.createElement(OutgoingRecordsPage),
      },
      {
        path: "/recordOfficer/incoming",
        element: React.createElement(IncomingRecordsPage),
      },
      {
        path: "/pending",
        element: React.createElement(PendingRecordsPage),
      },
    ];
  }
  return routes;
};

const RecordOfficerRoutesDef: IRouteDef[] = [
  {
    path: "/dashboard",
    element: React.createElement(DashboardRO),
  },
  {
    path: "/recordOfficer/outgoing",
    element: React.createElement(OutgoingRecordsPage),
  },
  {
    path: "/recordOfficer/incoming",
    element: React.createElement(IncomingRecordsPage),
  },
  {
    path: "/recordOfficer/incomingHistory",
    element: React.createElement(IncomingRecordsHistory),
  },
  {
    path: "/recordOfficer/incomingNonSmartExternal",
    element: React.createElement(IncomingnonSmartExternal),
  },
  {
    path: "/recordOfficer/incoming/add",
    element: React.createElement(AddIncomingRecordPage),
  },
  {
    path: "/recordOfficer/incoming/softDelete",
    element: React.createElement(SoftDeleteRO),
  },
  { path: "/pending", element: React.createElement(PendingRecordsPage) },
  { path: "/convert/:id", element: React.createElement(ConvertRecordsPage) },
  {
    path: "/recordViewIncoming/outgoing/:id",
    element: React.createElement(ViewOutgoingRecordOfficerDetailsPage),
  },
  {
    path: "/outgoingview/:id",
    element: React.createElement(ViewOutgoingRecordOfficerDetailsPage2),
  },
  {
    path: "/viewIncoming/:id/:type",
    element: React.createElement(RecordOfficerIncomingViewer),
  },
  {
    path: "/editIncoming/:id",
    element: React.createElement(RecordOfficerIncomingEdit),
  },
  { path: "/viewPending/:id", element: React.createElement(viewPending) },
  { path: "/view/:id", element: React.createElement(viewOutgoing) },
  {
    path: "/officersettings",
    element: React.createElement(RecordOfficerSettingsPage),
  },
  {
    path: "/letterTemplates/add",
    element: React.createElement(AddLetterTemplatePage),
  },
  {
    path: "/letterTemplates/view/:id",
    element: React.createElement(ViewLetterTemplatePage),
  },
  {
    path: "/letterTemplates/edit/:id",
    element: React.createElement(EditLetterTemplatePage),
  },
];

export const RoutesDef = (
  permissions: string[],
  selectedPositionPermissions: string[] = [],
): IRouteDef[] => {
  if (permissions.includes("can:dispatchRecords")) {
    return RecordOfficerRoutesDef;
  }
  return UserRoutesDef(permissions, selectedPositionPermissions);
};
