export const SYSTEM_ROLES = ['super_admin', 'user'] as const;
export const MEMBERSHIP_ROLES = ['admin', 'user'] as const;
export const BUSINESS_ROLES = [
  'marketing',
  'sales',
  'sales_support',
  'sales_manager',
  'department_manager',
  'top_manager'
] as const;

export type SystemRole = (typeof SYSTEM_ROLES)[number];
export type MembershipRole = (typeof MEMBERSHIP_ROLES)[number];
export type BusinessRole = (typeof BUSINESS_ROLES)[number];

export const PERMISSIONS = {
  productsRead: 'products:read',
  productsWrite: 'products:write',
  organizationManage: 'organization:manage',
  usersManage: 'users:manage',
  reportRead: 'report:read',
  crmLeadRead: 'crm.lead.read',
  crmLeadCreate: 'crm.lead.create',
  crmLeadUpdate: 'crm.lead.update',
  crmLeadAssign: 'crm.lead.assign',
  crmLeadDelete: 'crm.lead.delete',
  crmCustomerRead: 'crm.customer.read',
  crmCustomerCreate: 'crm.customer.create',
  crmCustomerUpdate: 'crm.customer.update',
  crmCustomerDelete: 'crm.customer.delete',
  crmContactRead: 'crm.contact.read',
  crmContactCreate: 'crm.contact.create',
  crmContactUpdate: 'crm.contact.update',
  crmContactDelete: 'crm.contact.delete',
  crmEnquiryRead: 'crm.enquiry.read',
  crmEnquiryCreate: 'crm.enquiry.create',
  crmEnquiryUpdate: 'crm.enquiry.update',
  crmEnquiryDelete: 'crm.enquiry.delete',
  crmEnquiryAssign: 'crm.enquiry.assign',
  crmEnquiryReassign: 'crm.enquiry.reassign',
  crmEnquiryFollowupRead: 'crm.enquiry.followup.read',
  crmEnquiryFollowupCreate: 'crm.enquiry.followup.create',
  crmEnquiryFollowupUpdate: 'crm.enquiry.followup.update',
  crmEnquiryFollowupDelete: 'crm.enquiry.followup.delete',
  crmQuotationRead: 'crm.quotation.read',
  crmQuotationCreate: 'crm.quotation.create',
  crmQuotationUpdate: 'crm.quotation.update',
  crmQuotationDelete: 'crm.quotation.delete',
  crmQuotationItemManage: 'crm.quotation.item.manage',
  crmQuotationCustomerManage: 'crm.quotation.customer.manage',
  crmQuotationTopicManage: 'crm.quotation.topic.manage',
  crmQuotationFollowupManage: 'crm.quotation.followup.manage',
  crmQuotationAttachmentManage: 'crm.quotation.attachment.manage',
  crmQuotationRevisionCreate: 'crm.quotation.revision.create',
  crmApprovalRead: 'crm.approval.read',
  crmApprovalSubmit: 'crm.approval.submit',
  crmApprovalApprove: 'crm.approval.approve',
  crmApprovalReject: 'crm.approval.reject',
  crmApprovalReturn: 'crm.approval.return',
  crmApprovalWorkflowRead: 'crm.approval.workflow.read',
  crmApprovalWorkflowCreate: 'crm.approval.workflow.create',
  crmApprovalWorkflowUpdate: 'crm.approval.workflow.update',
  crmApprovalWorkflowDelete: 'crm.approval.workflow.delete',
  crmDashboardRead: 'crm.dashboard.read',
  crmDashboardExport: 'crm.dashboard.export',
  crmDocumentTemplateRead: 'crm.document_template.read',
  crmDocumentTemplateCreate: 'crm.document_template.create',
  crmDocumentTemplateUpdate: 'crm.document_template.update',
  crmDocumentTemplateDelete: 'crm.document_template.delete',
  crmDocumentSequenceRead: 'crm.document_sequence.read',
  crmDocumentSequenceCreate: 'crm.document_sequence.create',
  crmDocumentSequenceUpdate: 'crm.document_sequence.update',
  crmDocumentSequenceReset: 'crm.document_sequence.reset',
  crmDocumentArtifactRead: 'crm.document_artifact.read',
  crmDocumentArtifactDownload: 'crm.document_artifact.download',
  crmDocumentArtifactVoid: 'crm.document_artifact.void',
  crmQuotationDocumentPreview: 'crm.quotation.document.preview',
  crmQuotationPdfPreview: 'crm.quotation.pdf.preview',
  crmQuotationPdfDownload: 'crm.quotation.pdf.download',
  crmQuotationPdfGenerateApproved: 'crm.quotation.pdf.generate_approved'
} as const;

export type Permission = (typeof PERMISSIONS)[keyof typeof PERMISSIONS];

const MANAGER_PERMISSIONS: Permission[] = [
  PERMISSIONS.crmLeadRead,
  PERMISSIONS.crmLeadCreate,
  PERMISSIONS.crmLeadUpdate,
  PERMISSIONS.crmLeadAssign,
  PERMISSIONS.crmLeadDelete,
  PERMISSIONS.crmCustomerRead,
  PERMISSIONS.crmCustomerCreate,
  PERMISSIONS.crmCustomerUpdate,
  PERMISSIONS.crmContactRead,
  PERMISSIONS.crmContactCreate,
  PERMISSIONS.crmContactUpdate,
  PERMISSIONS.crmEnquiryRead,
  PERMISSIONS.crmEnquiryCreate,
  PERMISSIONS.crmEnquiryUpdate,
  PERMISSIONS.crmEnquiryAssign,
  PERMISSIONS.crmEnquiryReassign,
  PERMISSIONS.crmEnquiryFollowupRead,
  PERMISSIONS.crmEnquiryFollowupCreate,
  PERMISSIONS.crmEnquiryFollowupUpdate,
  PERMISSIONS.crmQuotationRead,
  PERMISSIONS.crmQuotationCreate,
  PERMISSIONS.crmQuotationUpdate,
  PERMISSIONS.crmQuotationItemManage,
  PERMISSIONS.crmQuotationCustomerManage,
  PERMISSIONS.crmQuotationTopicManage,
  PERMISSIONS.crmQuotationFollowupManage,
  PERMISSIONS.crmQuotationAttachmentManage,
  PERMISSIONS.crmQuotationRevisionCreate,
  PERMISSIONS.crmApprovalRead,
  PERMISSIONS.crmApprovalSubmit,
  PERMISSIONS.crmApprovalWorkflowRead,
  PERMISSIONS.crmDashboardRead,
  PERMISSIONS.crmDashboardExport,
  PERMISSIONS.crmDocumentTemplateRead,
  PERMISSIONS.crmDocumentSequenceRead,
  PERMISSIONS.crmDocumentArtifactRead,
  PERMISSIONS.crmDocumentArtifactDownload,
  PERMISSIONS.crmDocumentArtifactVoid,
  PERMISSIONS.crmQuotationDocumentPreview,
  PERMISSIONS.crmQuotationPdfPreview,
  PERMISSIONS.crmQuotationPdfDownload
];

function getMembershipBasePermissions(role: MembershipRole): Permission[] {
  if (role === 'admin') {
    return [
      PERMISSIONS.productsRead,
      PERMISSIONS.productsWrite,
      PERMISSIONS.organizationManage,
      PERMISSIONS.usersManage,
      PERMISSIONS.crmLeadRead,
      PERMISSIONS.crmLeadCreate,
      PERMISSIONS.crmLeadUpdate,
      PERMISSIONS.crmLeadAssign,
      PERMISSIONS.crmLeadDelete,
      PERMISSIONS.crmCustomerRead,
      PERMISSIONS.crmCustomerCreate,
      PERMISSIONS.crmCustomerUpdate,
      PERMISSIONS.crmCustomerDelete,
      PERMISSIONS.crmContactRead,
      PERMISSIONS.crmContactCreate,
      PERMISSIONS.crmContactUpdate,
      PERMISSIONS.crmContactDelete,
      PERMISSIONS.crmEnquiryRead,
      PERMISSIONS.crmEnquiryCreate,
      PERMISSIONS.crmEnquiryUpdate,
      PERMISSIONS.crmEnquiryDelete,
      PERMISSIONS.crmEnquiryAssign,
      PERMISSIONS.crmEnquiryReassign,
      PERMISSIONS.crmEnquiryFollowupRead,
      PERMISSIONS.crmEnquiryFollowupCreate,
      PERMISSIONS.crmEnquiryFollowupUpdate,
      PERMISSIONS.crmEnquiryFollowupDelete,
      PERMISSIONS.crmQuotationRead,
      PERMISSIONS.crmQuotationCreate,
      PERMISSIONS.crmQuotationUpdate,
      PERMISSIONS.crmQuotationDelete,
      PERMISSIONS.crmQuotationItemManage,
      PERMISSIONS.crmQuotationCustomerManage,
      PERMISSIONS.crmQuotationTopicManage,
      PERMISSIONS.crmQuotationFollowupManage,
      PERMISSIONS.crmQuotationAttachmentManage,
      PERMISSIONS.crmQuotationRevisionCreate,
      PERMISSIONS.crmApprovalRead,
      PERMISSIONS.crmApprovalSubmit,
      PERMISSIONS.crmApprovalApprove,
      PERMISSIONS.crmApprovalReject,
      PERMISSIONS.crmApprovalReturn,
      PERMISSIONS.crmApprovalWorkflowRead,
      PERMISSIONS.crmApprovalWorkflowCreate,
      PERMISSIONS.crmApprovalWorkflowUpdate,
      PERMISSIONS.crmApprovalWorkflowDelete,
      PERMISSIONS.crmDashboardRead,
      PERMISSIONS.crmDashboardExport,
      PERMISSIONS.crmDocumentTemplateRead,
      PERMISSIONS.crmDocumentTemplateCreate,
      PERMISSIONS.crmDocumentTemplateUpdate,
      PERMISSIONS.crmDocumentTemplateDelete,
      PERMISSIONS.crmDocumentSequenceRead,
      PERMISSIONS.crmDocumentSequenceCreate,
      PERMISSIONS.crmDocumentSequenceUpdate,
      PERMISSIONS.crmDocumentSequenceReset,
      PERMISSIONS.crmDocumentArtifactRead,
      PERMISSIONS.crmDocumentArtifactDownload,
      PERMISSIONS.crmDocumentArtifactVoid,
      PERMISSIONS.crmQuotationDocumentPreview,
      PERMISSIONS.crmQuotationPdfPreview,
      PERMISSIONS.crmQuotationPdfDownload,
      PERMISSIONS.crmQuotationPdfGenerateApproved
    ];
  }

  return [
      PERMISSIONS.productsRead,
      PERMISSIONS.crmLeadRead,
      PERMISSIONS.crmCustomerRead,
    PERMISSIONS.crmContactRead,
    PERMISSIONS.crmEnquiryRead,
    PERMISSIONS.crmEnquiryFollowupRead,
    PERMISSIONS.crmQuotationRead,
    PERMISSIONS.crmApprovalRead,
    PERMISSIONS.crmApprovalWorkflowRead,
    PERMISSIONS.crmDashboardRead,
    PERMISSIONS.crmDocumentTemplateRead,
    PERMISSIONS.crmDocumentSequenceRead,
    PERMISSIONS.crmDocumentArtifactRead,
    PERMISSIONS.crmDocumentArtifactDownload,
    PERMISSIONS.crmQuotationDocumentPreview,
    PERMISSIONS.crmQuotationPdfPreview,
    PERMISSIONS.crmQuotationPdfDownload
  ];
}

export function getBusinessRolePermissions(role: BusinessRole): Permission[] {
  switch (role) {
    case 'marketing':
      return [
        PERMISSIONS.crmLeadRead,
        PERMISSIONS.crmLeadCreate,
        PERMISSIONS.crmLeadUpdate,
        PERMISSIONS.crmLeadAssign,
        PERMISSIONS.crmLeadDelete,
        PERMISSIONS.crmCustomerRead,
        PERMISSIONS.crmContactRead,
        PERMISSIONS.crmEnquiryRead,
        PERMISSIONS.crmEnquiryCreate,
        PERMISSIONS.crmEnquiryAssign,
        PERMISSIONS.crmEnquiryFollowupRead,
        PERMISSIONS.crmDashboardRead
      ];
    case 'sales_manager':
      return MANAGER_PERMISSIONS;
    case 'sales':
      return [
        PERMISSIONS.crmCustomerRead,
        PERMISSIONS.crmContactRead,
        PERMISSIONS.crmEnquiryRead,
        PERMISSIONS.crmEnquiryCreate,
        PERMISSIONS.crmEnquiryUpdate,
        PERMISSIONS.crmEnquiryFollowupRead,
        PERMISSIONS.crmEnquiryFollowupCreate,
        PERMISSIONS.crmEnquiryFollowupUpdate,
        PERMISSIONS.crmQuotationRead,
        PERMISSIONS.crmQuotationCreate,
        PERMISSIONS.crmQuotationUpdate,
        PERMISSIONS.crmQuotationItemManage,
        PERMISSIONS.crmQuotationCustomerManage,
        PERMISSIONS.crmQuotationTopicManage,
        PERMISSIONS.crmQuotationFollowupManage,
        PERMISSIONS.crmQuotationAttachmentManage,
        PERMISSIONS.crmQuotationRevisionCreate,
        PERMISSIONS.crmApprovalRead,
        PERMISSIONS.crmApprovalSubmit,
        PERMISSIONS.crmApprovalWorkflowRead,
        PERMISSIONS.crmDashboardRead,
        PERMISSIONS.crmDocumentTemplateRead,
        PERMISSIONS.crmDocumentSequenceRead,
        PERMISSIONS.crmDocumentArtifactRead,
        PERMISSIONS.crmDocumentArtifactDownload,
        PERMISSIONS.crmQuotationDocumentPreview,
        PERMISSIONS.crmQuotationPdfPreview,
        PERMISSIONS.crmQuotationPdfDownload
      ];
    case 'sales_support':
      return [
        PERMISSIONS.crmCustomerRead,
        PERMISSIONS.crmContactRead,
        PERMISSIONS.crmEnquiryRead,
        PERMISSIONS.crmEnquiryCreate,
        PERMISSIONS.crmEnquiryUpdate,
        PERMISSIONS.crmEnquiryFollowupRead,
        PERMISSIONS.crmEnquiryFollowupCreate,
        PERMISSIONS.crmEnquiryFollowupUpdate,
        PERMISSIONS.crmQuotationRead,
        PERMISSIONS.crmQuotationCreate,
        PERMISSIONS.crmQuotationUpdate,
        PERMISSIONS.crmQuotationItemManage,
        PERMISSIONS.crmQuotationCustomerManage,
        PERMISSIONS.crmQuotationTopicManage,
        PERMISSIONS.crmQuotationFollowupManage,
        PERMISSIONS.crmQuotationAttachmentManage,
        PERMISSIONS.crmQuotationRevisionCreate,
        PERMISSIONS.crmApprovalRead,
        PERMISSIONS.crmApprovalWorkflowRead,
        PERMISSIONS.crmDashboardRead,
        PERMISSIONS.crmDocumentTemplateRead,
        PERMISSIONS.crmDocumentSequenceRead,
        PERMISSIONS.crmDocumentArtifactRead,
        PERMISSIONS.crmDocumentArtifactDownload,
        PERMISSIONS.crmQuotationDocumentPreview,
        PERMISSIONS.crmQuotationPdfPreview,
        PERMISSIONS.crmQuotationPdfDownload
      ];
    case 'department_manager':
    case 'top_manager':
      return MANAGER_PERMISSIONS;
    default:
      return [];
  }
}

export function getDefaultBusinessRole(role: MembershipRole): BusinessRole {
  return role === 'admin' ? 'sales_manager' : 'sales_support';
}

export function getDefaultPermissions(
  role: MembershipRole,
  businessRole: BusinessRole = getDefaultBusinessRole(role)
): Permission[] {
  return [
    ...new Set([...getMembershipBasePermissions(role), ...getBusinessRolePermissions(businessRole)])
  ];
}

export function isSystemRole(value: string): value is SystemRole {
  return SYSTEM_ROLES.includes(value as SystemRole);
}

export function isMembershipRole(value: string): value is MembershipRole {
  return MEMBERSHIP_ROLES.includes(value as MembershipRole);
}

export function isBusinessRole(value: string): value is BusinessRole {
  return BUSINESS_ROLES.includes(value as BusinessRole);
}
