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 sharedEntityRoles = [
  WORKSPACE_ROLES.directorGeneral,
  WORKSPACE_ROLES.financeDirector,
  WORKSPACE_ROLES.procurementLead,
  WORKSPACE_ROLES.complianceAuditLead,
  WORKSPACE_ROLES.projectDeliveryLead,
]

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: 'Platform Workflow',
    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: 'Workflow Readiness',
    icon: icon.mdiSitemap,
    roles: adminWorkspaceRoles,
    withDevider: true,
    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: 'Operational Notices',
    icon: icon.mdiBellOutline,
    roles: adminWorkspaceRoles,
    menu: [
      {
        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',
      },
      {
        href: '/documents/documents-list',
        label: 'Documents',
        icon: optionalIcon('mdiFolderFile'),
        permissions: 'READ_DOCUMENTS',
      },
      {
        href: '/compliance_alerts/compliance_alerts-list',
        label: 'Compliance alerts',
        icon: optionalIcon('mdiShieldAlert'),
        permissions: 'READ_COMPLIANCE_ALERTS',
      },
    ],
  },
  {
    label: 'Administration',
    icon: icon.mdiAccountGroup,
    roles: adminWorkspaceRoles,
    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',
      },
    ],
  },
  {
    label: 'Budget & Planning',
    icon: optionalIcon('mdiWalletOutline'),
    roles: adminWorkspaceRoles,
    menu: [
      {
        href: '/fiscal_years/fiscal_years-list',
        label: 'Fiscal years',
        icon: optionalIcon('mdiCalendarRange'),
        permissions: 'READ_FISCAL_YEARS',
      },
      {
        href: '/funding_sources/funding_sources-list',
        label: 'Funding sources',
        icon: optionalIcon('mdiCashMultiple'),
        permissions: 'READ_FUNDING_SOURCES',
      },
      {
        href: '/budget_programs/budget_programs-list',
        label: 'Budget programs',
        icon: optionalIcon('mdiChartDonut'),
        permissions: 'READ_BUDGET_PROGRAMS',
      },
      {
        href: '/budget_lines/budget_lines-list',
        label: 'Budget lines',
        icon: optionalIcon('mdiFormatListBulleted'),
        permissions: 'READ_BUDGET_LINES',
      },
      {
        href: '/allocations/allocations-list',
        label: 'Allocations',
        icon: optionalIcon('mdiDatabaseArrowRight'),
        permissions: 'READ_ALLOCATIONS',
      },
      {
        href: '/budget_reallocations/budget_reallocations-list',
        label: 'Reallocations',
        icon: optionalIcon('mdiSwapHorizontal'),
        permissions: 'READ_BUDGET_REALLOCATIONS',
      },
    ],
  },
  {
    label: 'Procurement',
    icon: icon.mdiGavel,
    roles: adminWorkspaceRoles,
    menu: [
      {
        href: '/procurement_plans/procurement_plans-list',
        label: 'Plans',
        icon: optionalIcon('mdiClipboardList'),
        permissions: 'READ_PROCUREMENT_PLANS',
      },
      {
        href: '/requisitions/requisitions-list',
        label: 'Requisitions',
        icon: optionalIcon('mdiFileDocumentEdit'),
        permissions: 'READ_REQUISITIONS',
      },
      {
        href: '/tenders/tenders-list',
        label: 'Tenders',
        icon: optionalIcon('mdiGavel'),
        permissions: 'READ_TENDERS',
      },
      {
        href: '/vendors/vendors-list',
        label: 'Vendors',
        icon: optionalIcon('mdiTruckFast'),
        permissions: 'READ_VENDORS',
      },
      {
        href: '/vendor_compliance_documents/vendor_compliance_documents-list',
        label: 'Vendor compliance',
        icon: optionalIcon('mdiFileCertificate'),
        permissions: 'READ_VENDOR_COMPLIANCE_DOCUMENTS',
      },
      {
        href: '/bids/bids-list',
        label: 'Bids',
        icon: optionalIcon('mdiFileSign'),
        permissions: 'READ_BIDS',
      },
      {
        href: '/bid_evaluations/bid_evaluations-list',
        label: 'Bid evaluations',
        icon: optionalIcon('mdiClipboardCheck'),
        permissions: 'READ_BID_EVALUATIONS',
      },
      {
        href: '/awards/awards-list',
        label: 'Awards',
        icon: optionalIcon('mdiTrophyAward'),
        permissions: 'READ_AWARDS',
      },
    ],
  },
  {
    label: 'Delivery & Contracts',
    icon: icon.mdiChartTimelineVariant,
    roles: adminWorkspaceRoles,
    menu: [
      {
        href: '/programs/programs-list',
        label: 'Programs',
        icon: optionalIcon('mdiViewGridOutline'),
        permissions: 'READ_PROGRAMS',
      },
      {
        href: '/projects/projects-list',
        label: 'Projects',
        icon: optionalIcon('mdiBriefcaseCheck'),
        permissions: 'READ_PROJECTS',
      },
      {
        href: '/project_milestones/project_milestones-list',
        label: 'Milestones',
        icon: optionalIcon('mdiTimelineCheck'),
        permissions: 'READ_PROJECT_MILESTONES',
      },
      {
        href: '/risks/risks-list',
        label: 'Risks',
        icon: optionalIcon('mdiAlertOctagon'),
        permissions: 'READ_RISKS',
      },
      {
        href: '/issues/issues-list',
        label: 'Issues',
        icon: optionalIcon('mdiBug'),
        permissions: 'READ_ISSUES',
      },
      {
        href: '/field_verifications/field_verifications-list',
        label: 'Field checks',
        icon: optionalIcon('mdiMapMarkerCheck'),
        permissions: 'READ_FIELD_VERIFICATIONS',
      },
      {
        href: '/contracts/contracts-list',
        label: 'Contracts',
        icon: optionalIcon('mdiFileDocumentOutline'),
        permissions: 'READ_CONTRACTS',
      },
      {
        href: '/contract_amendments/contract_amendments-list',
        label: 'Amendments',
        icon: optionalIcon('mdiFileReplaceOutline'),
        permissions: 'READ_CONTRACT_AMENDMENTS',
      },
      {
        href: '/contract_milestones/contract_milestones-list',
        label: 'Contract milestones',
        icon: optionalIcon('mdiTimelineCheck'),
        permissions: 'READ_CONTRACT_MILESTONES',
      },
    ],
  },
  {
    label: 'Grants & Beneficiaries',
    icon: optionalIcon('mdiHandCoin'),
    roles: adminWorkspaceRoles,
    menu: [
      {
        href: '/grants/grants-list',
        label: 'Grants',
        icon: optionalIcon('mdiHandCoin'),
        permissions: 'READ_GRANTS',
      },
      {
        href: '/beneficiaries/beneficiaries-list',
        label: 'Beneficiaries',
        icon: optionalIcon('mdiAccountGroup'),
        permissions: 'READ_BENEFICIARIES',
      },
      {
        href: '/grant_applications/grant_applications-list',
        label: 'Applications',
        icon: optionalIcon('mdiFileDocumentMultiple'),
        permissions: 'READ_GRANT_APPLICATIONS',
      },
      {
        href: '/grant_evaluations/grant_evaluations-list',
        label: 'Evaluations',
        icon: optionalIcon('mdiStarCheck'),
        permissions: 'READ_GRANT_EVALUATIONS',
      },
      {
        href: '/grant_tranches/grant_tranches-list',
        label: 'Tranches',
        icon: optionalIcon('mdiCashCheck'),
        permissions: 'READ_GRANT_TRANCHES',
      },
    ],
  },
  {
    label: 'Finance & Payments',
    icon: optionalIcon('mdiCashCheck'),
    roles: adminWorkspaceRoles,
    menu: [
      {
        href: '/expense_categories/expense_categories-list',
        label: 'Expense categories',
        icon: optionalIcon('mdiTagMultiple'),
        permissions: 'READ_EXPENSE_CATEGORIES',
      },
      {
        href: '/invoices/invoices-list',
        label: 'Invoices',
        icon: optionalIcon('mdiReceiptText'),
        permissions: 'READ_INVOICES',
      },
      {
        href: '/payment_requests/payment_requests-list',
        label: 'Payment requests',
        icon: optionalIcon('mdiCashFast'),
        permissions: 'READ_PAYMENT_REQUESTS',
      },
      {
        href: '/payment_batches/payment_batches-list',
        label: 'Payment batches',
        icon: optionalIcon('mdiPackageVariantClosed'),
        permissions: 'READ_PAYMENT_BATCHES',
      },
      {
        href: '/payments/payments-list',
        label: 'Payments',
        icon: optionalIcon('mdiBankTransfer'),
        permissions: 'READ_PAYMENTS',
      },
      {
        href: '/obligations/obligations-list',
        label: 'Obligations',
        icon: optionalIcon('mdiBookArrowDown'),
        permissions: 'READ_OBLIGATIONS',
      },
      {
        href: '/ledger_entries/ledger_entries-list',
        label: 'Ledger entries',
        icon: optionalIcon('mdiBookOpenPageVariant'),
        permissions: 'READ_LEDGER_ENTRIES',
      },
    ],
  },
]

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: 'Admin 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
