import * as icon from '@mdi/js'
import { MenuAsideItem } from './interfaces'
import { WORKSPACE_ROLES } from './helpers/workspace'

const optionalIcon = (name: string, fallback: string = icon.mdiTable): string => {
  const iconSet = icon as Record<string, string>

  return iconSet[name] || fallback
}

const superAdminWorkspaceRoles = [WORKSPACE_ROLES.superAdmin]
const adminWorkspaceRoles = [WORKSPACE_ROLES.administrator]

const superAdminGroupedNavigation: MenuAsideItem[] = [
  {
    label: 'Platform Governance',
    icon: icon.mdiShieldAccountOutline,
    roles: superAdminWorkspaceRoles,
    withDevider: true,
    menu: [
      {
        href: '/organizations/organizations-list',
        label: 'Organizations',
        icon: optionalIcon('mdiDomain'),
        permissions: 'READ_ORGANIZATIONS',
      },
      {
        href: '/users/users-list',
        label: 'Users',
        icon: icon.mdiAccountGroup,
        permissions: 'READ_USERS',
      },
      {
        href: '/roles/roles-list',
        label: 'Roles',
        icon: optionalIcon('mdiShieldAccountVariantOutline'),
        permissions: 'READ_ROLES',
      },
      {
        href: '/permissions/permissions-list',
        label: 'Permissions',
        icon: optionalIcon('mdiShieldAccountOutline'),
        permissions: 'READ_PERMISSIONS',
      },
      {
        href: '/role_permissions/role_permissions-list',
        label: 'Role permissions',
        icon: optionalIcon('mdiLinkVariant'),
        permissions: 'READ_ROLE_PERMISSIONS',
      },
    ],
  },
  {
    label: 'Workflow & Oversight',
    icon: icon.mdiSitemap,
    roles: superAdminWorkspaceRoles,
    menu: [
      {
        href: '/approval_workflows/approval_workflows-list',
        label: 'Approval workflows',
        icon: optionalIcon('mdiSitemap'),
        permissions: 'READ_APPROVAL_WORKFLOWS',
      },
      {
        href: '/approval_steps/approval_steps-list',
        label: 'Approval steps',
        icon: optionalIcon('mdiStairs'),
        permissions: 'READ_APPROVAL_STEPS',
      },
      {
        href: '/notifications/notifications-list',
        label: 'Notifications',
        icon: optionalIcon('mdiBell'),
        permissions: 'READ_NOTIFICATIONS',
      },
      {
        href: '/audit_logs/audit_logs-list',
        label: 'Audit logs',
        icon: optionalIcon('mdiClipboardTextClock'),
        permissions: 'READ_AUDIT_LOGS',
      },
    ],
  },
]

const adminGroupedNavigation: MenuAsideItem[] = [
  {
    label: 'Organization Setup',
    icon: icon.mdiAccountGroup,
    roles: adminWorkspaceRoles,
    withDevider: true,
    menu: [
      {
        href: '/users/users-list',
        label: 'Users',
        icon: icon.mdiAccountGroup,
        permissions: 'READ_USERS',
      },
      {
        href: '/provinces/provinces-list',
        label: 'Provinces',
        icon: optionalIcon('mdiMapMarker'),
        permissions: 'READ_PROVINCES',
      },
      {
        href: '/departments/departments-list',
        label: 'Departments',
        icon: optionalIcon('mdiOfficeBuilding'),
        permissions: 'READ_DEPARTMENTS',
      },
      {
        href: '/documents/documents-list',
        label: 'Documents',
        icon: optionalIcon('mdiFolderFile'),
        permissions: 'READ_DOCUMENTS',
      },
    ],
  },
  {
    label: 'Workflow Control',
    icon: icon.mdiSitemap,
    roles: adminWorkspaceRoles,
    menu: [
      {
        href: '/approval_workflows/approval_workflows-list',
        label: 'Approval workflows',
        icon: optionalIcon('mdiSitemap'),
        permissions: 'READ_APPROVAL_WORKFLOWS',
      },
      {
        href: '/approval_steps/approval_steps-list',
        label: 'Approval steps',
        icon: optionalIcon('mdiStairs'),
        permissions: 'READ_APPROVAL_STEPS',
      },
      {
        href: '/approvals/approvals-list',
        label: 'Approvals',
        icon: optionalIcon('mdiChecklist'),
        permissions: 'READ_APPROVALS',
      },
      {
        href: '/role_permissions/role_permissions-list',
        label: 'Role access',
        icon: optionalIcon('mdiLinkVariant'),
        permissions: 'READ_ROLE_PERMISSIONS',
      },
    ],
  },
  {
    label: 'Assurance & Notices',
    icon: icon.mdiBellOutline,
    roles: adminWorkspaceRoles,
    menu: [
      {
        href: '/notifications/notifications-list',
        label: 'Notifications',
        icon: optionalIcon('mdiBell'),
        permissions: 'READ_NOTIFICATIONS',
      },
      {
        href: '/compliance_alerts/compliance_alerts-list',
        label: 'Compliance alerts',
        icon: optionalIcon('mdiShieldAlert'),
        permissions: 'READ_COMPLIANCE_ALERTS',
      },
      {
        href: '/audit_logs/audit_logs-list',
        label: 'Audit logs',
        icon: optionalIcon('mdiClipboardTextClock'),
        permissions: 'READ_AUDIT_LOGS',
      },
    ],
  },
]

const sharedEntityNavigation: MenuAsideItem[] = []

const menuAside: MenuAsideItem[] = [
  {
    href: '/executive-summary',
    icon: icon.mdiBankOutline,
    label: 'Role Workspace',
    labelByRole: {
      [WORKSPACE_ROLES.superAdmin]: 'Platform Administration',
      [WORKSPACE_ROLES.administrator]: 'Organization Administration',
      [WORKSPACE_ROLES.directorGeneral]: 'Executive Oversight',
      [WORKSPACE_ROLES.financeDirector]: 'Financial Management',
      [WORKSPACE_ROLES.procurementLead]: 'Procurement Management',
      [WORKSPACE_ROLES.complianceAuditLead]: 'Compliance & Audit',
      [WORKSPACE_ROLES.projectDeliveryLead]: 'Project Delivery',
    },
  },
  {
    href: '/dashboard',
    icon: icon.mdiViewDashboardOutline,
    label: 'Role Widgets',
    labelByRole: {
      [WORKSPACE_ROLES.superAdmin]: 'Platform Widgets',
      [WORKSPACE_ROLES.administrator]: 'Operations Widgets',
    },
    roles: [WORKSPACE_ROLES.superAdmin, WORKSPACE_ROLES.administrator],
  },
  ...superAdminGroupedNavigation,
  ...adminGroupedNavigation,
  {
    href: '/projects/projects-list',
    label: 'Strategic Portfolio',
    icon: icon.mdiChartTimelineVariant,
    permissions: 'READ_PROJECTS',
    roles: [WORKSPACE_ROLES.directorGeneral],
    withDevider: true,
  },
  {
    href: '/contracts/contracts-list',
    label: 'Contract Exposure',
    icon: icon.mdiFileDocumentOutline,
    permissions: 'READ_CONTRACTS',
    roles: [WORKSPACE_ROLES.directorGeneral],
  },
  {
    href: '/payment_requests/payment_requests-list',
    label: 'Payment Control',
    icon: icon.mdiCashCheck,
    permissions: 'READ_PAYMENT_REQUESTS',
    roles: [WORKSPACE_ROLES.financeDirector],
    withDevider: true,
  },
  {
    href: '/allocations/allocations-list',
    label: 'Allocation Register',
    icon: icon.mdiWalletOutline,
    permissions: 'READ_ALLOCATIONS',
    roles: [WORKSPACE_ROLES.financeDirector],
  },
  {
    href: '/requisitions/requisitions-list',
    label: 'Requisition Queue',
    icon: icon.mdiClipboardListOutline,
    permissions: 'READ_REQUISITIONS',
    roles: [WORKSPACE_ROLES.procurementLead],
    withDevider: true,
  },
  {
    href: '/tenders/tenders-list',
    label: 'Tender Pipeline',
    icon: icon.mdiGavel,
    permissions: 'READ_TENDERS',
    roles: [WORKSPACE_ROLES.procurementLead],
  },
  {
    href: '/compliance_alerts/compliance_alerts-list',
    label: 'Control Exceptions',
    icon: icon.mdiShieldAlertOutline,
    permissions: 'READ_COMPLIANCE_ALERTS',
    roles: [WORKSPACE_ROLES.complianceAuditLead],
    withDevider: true,
  },
  {
    href: '/audit_logs/audit_logs-list',
    label: 'Audit Trail',
    icon: icon.mdiClipboardTextClock,
    permissions: 'READ_AUDIT_LOGS',
    roles: [WORKSPACE_ROLES.complianceAuditLead],
  },
  {
    href: '/projects/projects-list',
    label: 'Delivery Portfolio',
    icon: icon.mdiChartTimelineVariant,
    permissions: 'READ_PROJECTS',
    roles: [WORKSPACE_ROLES.projectDeliveryLead],
    withDevider: true,
  },
  {
    href: '/project_milestones/project_milestones-list',
    label: 'Milestone Tracker',
    icon: icon.mdiFlagCheckered,
    permissions: 'READ_PROJECT_MILESTONES',
    roles: [WORKSPACE_ROLES.projectDeliveryLead],
  },
  ...sharedEntityNavigation,
  {
    href: '/profile',
    label: 'Profile',
    icon: icon.mdiAccountCircle,
  },
  {
    href: '/api-docs',
    target: '_blank',
    label: 'Swagger API',
    icon: icon.mdiFileCode,
    permissions: 'READ_API_DOCS',
  },
]

export default menuAside
