import {
  IModulePermission,
  NewCreatedPermission,
} from '@/modules/permissions/permissions.interface';
import { ACTIONS } from '@/shared/constants';

const READ_ONLY = [ACTIONS.READ];
const FULL_ACCESS = 'Full Access';
const PARTIAL_ACCESS = 'Partial Access';

const withAccess = (
  permissions: IModulePermission[],
  accessLevel: string[] | undefined,
) =>
  permissions.map((perm) =>
    accessLevel ? { ...perm, access: accessLevel } : perm,
  );

// Base permissions - reusable across roles
const BASE_PERMISSIONS = {
  // Lead Management
  leads: { id: 'leads', key: 'Leads', moduleName: 'Leads' },
  customers: { id: 'customers', key: 'Customers', moduleName: 'Customer' },
  campaigns: {
    id: 'campaigns',
    key: 'Marketing Campaigns',
    moduleName: 'Campaign',
  },
  contacts: {
    id: 'contacts',
    key: 'Contacts Management',
    moduleName: 'Contacts',
  },
  leadStages: { id: 'leadStages', key: 'Lead Stages', moduleName: 'LeadStage' },
  captureLeads: {
    id: 'captureLeads',
    key: 'Capture Leads',
    moduleName: 'CaptureLeads',
  },
  reports: { id: 'reports', key: 'Reports', moduleName: 'Reports' },

  // Projects
  projects: { id: 'projects', key: 'Projects', moduleName: 'Project' },
  projectUnits: {
    id: 'projectUnits',
    key: 'Project Units',
    moduleName: 'Unit',
  },
  constructionProgress: {
    id: 'constructionProgress',
    key: 'Construction Progress',
    moduleName: 'Progress',
  },
  projectFiles: {
    id: 'projectFiles',
    key: 'Project Files',
    moduleName: 'ProjectFiles',
  },

  // Properties
  properties: {
    id: 'properties',
    key: 'Individual Properties',
    moduleName: 'Property',
  },

  // Master Data
  propertyUsageTypes: {
    id: 'propertyUsageTypes',
    key: 'Property Usage Types',
    moduleName: 'PropertyUsage',
  },
  subCategories: {
    id: 'subCategories',
    key: 'Sub Categories',
    moduleName: 'SubCategory',
  },
  categories: { id: 'categories', key: 'Categories', moduleName: 'Category' },
  configuration: {
    id: 'configuration',
    key: 'Configuration',
    moduleName: 'Configuration',
  },
  propertySize: {
    id: 'propertySize',
    key: 'Property Size',
    moduleName: 'PropertySize',
  },
  propertyAge: {
    id: 'propertyAge',
    key: 'Property Age',
    moduleName: 'PropertyAge',
  },

  // Construction
  amenities: { id: 'amenities', key: 'Amenities', moduleName: 'Amenities' },
  tags: { id: 'tags', key: 'Tags', moduleName: 'Tags' },
  sources: { id: 'sources', key: 'Sources', moduleName: 'Source' },

  // Location
  countries: { id: 'countries', key: 'Countries', moduleName: 'Country' },
  states: { id: 'states', key: 'States', moduleName: 'State' },
  cities: { id: 'cities', key: 'Cities', moduleName: 'City' },
  areas: { id: 'areas', key: 'Areas', moduleName: 'Area' },

  // System
  company: { id: 'company', key: 'Companies', moduleName: 'Company' },
  user: { id: 'user', key: 'User Management', moduleName: 'User' },
  notification: {
    id: 'notification',
    key: 'Notifications',
    moduleName: 'Notification',
  },
};

export const SUPERADMIN_SEED_PERMISSIONS: NewCreatedPermission = [
  {
    group: 'Leads Management',
    groupAccess: PARTIAL_ACCESS,
    permissions: withAccess(
      [
        BASE_PERMISSIONS.leads,
        BASE_PERMISSIONS.customers,
        BASE_PERMISSIONS.campaigns,
        BASE_PERMISSIONS.contacts,
        BASE_PERMISSIONS.leadStages,
        BASE_PERMISSIONS.reports,
      ],
      READ_ONLY,
    ),
  },
  {
    group: 'Projects',
    groupAccess: PARTIAL_ACCESS,
    permissions: withAccess(
      [
        BASE_PERMISSIONS.projects,
        BASE_PERMISSIONS.projectUnits,
        BASE_PERMISSIONS.constructionProgress,
        BASE_PERMISSIONS.projectFiles,
      ],
      READ_ONLY,
    ),
  },
  {
    group: 'Properties',
    groupAccess: PARTIAL_ACCESS,
    permissions: withAccess([BASE_PERMISSIONS.properties], READ_ONLY),
  },
  {
    group: 'Property Master',
    groupAccess: FULL_ACCESS,
    permissions: [
      BASE_PERMISSIONS.propertyUsageTypes,
      BASE_PERMISSIONS.subCategories,
      BASE_PERMISSIONS.categories,
      BASE_PERMISSIONS.configuration,
      BASE_PERMISSIONS.propertySize,
      BASE_PERMISSIONS.propertyAge,
    ],
  },
  {
    group: 'Construction',
    groupAccess: FULL_ACCESS,
    permissions: [
      BASE_PERMISSIONS.amenities,
      BASE_PERMISSIONS.tags,
      BASE_PERMISSIONS.sources,
    ],
  },

  {
    group: 'Super Admin User & Company Management',
    groupAccess: FULL_ACCESS,
    permissions: [
      BASE_PERMISSIONS.company,
      ...withAccess([BASE_PERMISSIONS.user], READ_ONLY),
    ],
  },
  {
    group: 'Location Master',
    groupAccess: FULL_ACCESS,
    permissions: [
      BASE_PERMISSIONS.countries,
      BASE_PERMISSIONS.states,
      BASE_PERMISSIONS.cities,
      BASE_PERMISSIONS.areas,
    ],
  },
  {
    group: 'Super Admin Communication',
    groupAccess: PARTIAL_ACCESS,
    permissions: withAccess([BASE_PERMISSIONS.notification], READ_ONLY),
  },
];

export const COMPANY_SUPPORT_PERMISSIONS: NewCreatedPermission = [
  {
    group: 'Support',
    groupAccess: FULL_ACCESS,
    permissions: [{ id: 'support', key: 'Support', moduleName: 'Support' }],
  },
];

export const COMPANY_SETTINGS_PERMISSIONS: NewCreatedPermission = [
  {
    group: 'Settings',
    groupAccess: FULL_ACCESS,
    permissions: [{ id: 'settings', key: 'Settings', moduleName: 'Settings' }],
  },
];

export const DASHBOARD_PERMISSIONS: NewCreatedPermission = [
  {
    group: 'Dashboard',
    groupAccess: FULL_ACCESS,
    permissions: [
      { id: 'dashboard', key: 'Dashboard', moduleName: 'Dashboard' },
    ],
  },
];

export const COMPANY_SEED_PERMISSIONS: NewCreatedPermission = [
  {
    group: 'Company Navigation',
    groupAccess: FULL_ACCESS,
    permissions: [
      BASE_PERMISSIONS.leads,
      { id: 'campaigns', key: 'Campaigns', moduleName: 'Campaign' },
      { id: 'contacts', key: 'Contacts', moduleName: 'Contacts' },
      { id: 'tasks', key: 'Tasks', moduleName: 'Tasks' },
      { id: 'customers', key: 'Customers', moduleName: 'Customer' },
      { id: 'captureLeads', key: 'Capture Leads', moduleName: 'CaptureLeads' },
      { id: 'reports', key: 'Reports', moduleName: 'Reports' },
      { id: 'targets', key: 'Targets', moduleName: 'Targets' },
    ],
  },
];

export const BUILDER_SEED_PERMISSIONS: NewCreatedPermission = [
  {
    group: 'Projects',
    groupAccess: FULL_ACCESS,
    permissions: [
      BASE_PERMISSIONS.projects,
      BASE_PERMISSIONS.projectUnits,
      BASE_PERMISSIONS.constructionProgress,
      BASE_PERMISSIONS.projectFiles,
    ],
  },
];

export const BROKER_SEED_PERMISSIONS: NewCreatedPermission = [
  {
    group: 'Properties',
    groupAccess: FULL_ACCESS,
    permissions: withAccess([BASE_PERMISSIONS.properties], READ_ONLY),
  },
  {
    group: 'Partner Network',
    groupAccess: FULL_ACCESS,
    permissions: [
      { id: 'partnerNetwork', key: 'Partners', moduleName: 'PartnerNetwork' },
    ],
  },
  {
    group: 'Makanify Website',
    groupAccess: FULL_ACCESS,
    permissions: [
      { id: 'websites', key: 'Makanify Websites', moduleName: 'Website' },
    ],
  },
];

export const COMPANY_SETTINGS_SEED_PERMISSIONS: NewCreatedPermission = [
  {
    group: 'Payment & Finance',
    groupAccess: FULL_ACCESS,
    permissions: [
      {
        id: 'settingbankAccounts',
        key: 'Bank Accounts',
        moduleName: 'BankAccount',
      },
      {
        id: 'settingpaymentPlans',
        key: 'Payment Plans',
        moduleName: 'PaymentPlan',
      },
      {
        id: 'settingprojectCharges',
        key: 'Project Charges',
        moduleName: 'ProjectCharge',
      },
    ],
  },
  {
    group: 'Company Settings',
    groupAccess: FULL_ACCESS,
    permissions: [
      {
        id: 'settingcompanyAccount',
        key: 'Company Accounting',
        moduleName: 'Company',
      },
      {
        id: 'settingcompanyRules',
        key: 'Company Rules',
        moduleName: 'CompanyRules',
      },
      {
        id: 'settingsaleSettings',
        key: 'Sale Settings',
        moduleName: 'SaleSettings',
      },
      {
        id: 'settingtermsConditions',
        key: 'Terms & Conditions',
        moduleName: 'TermsConditions',
      },
      {
        id: 'settingdocumentTemplates',
        key: 'Document Templates',
        moduleName: 'DocumentTemplate',
      },
    ],
  },
  {
    group: 'Communication',
    groupAccess: FULL_ACCESS,
    permissions: [
      {
        id: 'settingemailAlerts',
        key: 'Email',
        moduleName: 'Notification',
      },
      {
        id: 'settingsmsAlerts',
        key: 'SMS',
        moduleName: 'Notification',
      },
      {
        id: 'settingwhatsappAlerts',
        key: 'Whatsapp',
        moduleName: 'Notification',
      },
    ],
  },
  {
    group: 'Leads & Forms',
    groupAccess: FULL_ACCESS,
    permissions: [
      {
        id: 'settingcustomForms',
        key: 'Custom Forms',
        moduleName: 'CustomForm',
      },
      {
        id: 'settingtransferLeads',
        key: 'Transfer Leads',
        moduleName: 'Leads',
      },
    ],
  },
  {
    group: 'User & Access Management',
    groupAccess: FULL_ACCESS,
    permissions: [
      { id: 'settingUser', key: 'User', moduleName: 'User' },
      { id: 'settingTeams', key: 'Teams', moduleName: 'Teams' },
      { id: 'settingRoles', key: 'Roles', moduleName: 'Roles' },

      { id: 'settingOrganogram', key: 'Organogram', moduleName: 'OrgChart' },
    ],
  },
  {
    group: 'System',
    groupAccess: FULL_ACCESS,
    permissions: [
      {
        id: 'settingactivityLog',
        key: 'Activity Log',
        moduleName: 'ActivityLog',
      },
    ],
  },
  {
    group: 'CRM Settings',
    groupAccess: FULL_ACCESS,
    permissions: [
      {
        id: 'settingleadStages',
        key: 'Lead Stages',
        moduleName: 'LeadStage',
      },
      {
        id: 'settingleadScore',
        key: 'Lead Score',
        moduleName: 'LeadScore',
      },
      {
        id: 'settingleadSource',
        key: 'Lead Source',
        moduleName: 'Source',
      },
    ],
  },
];
