﻿/* global window */

const USER_ROLES = Object.freeze({
  ADMIN: 'ADMIN',
  CORPORATE: 'CORPORATE',
  RGM: 'RGM',
  INTERIM_MANAGER: 'INTERIM_MANAGER',
  STAFF: 'STAFF',
});

const ROLE_ORDER = Object.freeze([
  USER_ROLES.ADMIN,
  USER_ROLES.CORPORATE,
  USER_ROLES.RGM,
  USER_ROLES.INTERIM_MANAGER,
  USER_ROLES.STAFF,
]);

const ACCOUNT_STATUS = Object.freeze({
  PENDING: 'pending',
  APPROVED: 'approved',
  REJECTED: 'rejected',
  DISABLED: 'disabled',
});

const STORES = Object.freeze([
  { storeId: 'elan_mercado', displayName: 'Elan Mercado' },
]);

const DEFAULT_STORE = STORES[0];
const ACTIVE_ROLE_VALUES = Object.values(USER_ROLES);

function normalizeRole(role) {
  const key = String(role || '').trim().toUpperCase().replace(/[\s-]+/g, '_');
  const aliases = {
    ADMIN: USER_ROLES.ADMIN,
    SUPREME_ADMIN: USER_ROLES.ADMIN,
    CORPORATE: USER_ROLES.CORPORATE,
    RGM: USER_ROLES.RGM,
    RESTAURANT_GENERAL_MANAGER: USER_ROLES.RGM,
    INTERIM: USER_ROLES.INTERIM_MANAGER,
    INTERIM_GM: USER_ROLES.INTERIM_MANAGER,
    INTERIM_MANAGER: USER_ROLES.INTERIM_MANAGER,
    STAFF: USER_ROLES.STAFF,
  };
  return aliases[key] || (ACTIVE_ROLE_VALUES.includes(key) ? key : USER_ROLES.STAFF);
}

function roleLabel(role) {
  const normalized = normalizeRole(role);
  return {
    ADMIN: 'Admin',
    CORPORATE: 'Corporate',
    RGM: 'RGM',
    INTERIM_MANAGER: 'Interim Manager',
    STAFF: 'Staff',
  }[normalized] || 'Staff';
}

function profileHasRole(profile, allowedRoles) {
  if (!profile) return false;
  const status = String(profile.status || '').trim().toLowerCase();
  if (status !== ACCOUNT_STATUS.APPROVED) return false;
  if (!allowedRoles || !allowedRoles.length) return true;
  return allowedRoles.includes(normalizeRole(profile.role));
}

const APP_FEATURES = Object.freeze([
  {
    id: 'home',
    label: 'Home',
    route: 'home',
    icon: 'home',
    enabled: true,
    visibleInHome: false,
    visibleInNav: true,
    allowedRoles: ACTIVE_ROLE_VALUES,
  },
  {
    id: 'scan-mrd',
    label: 'Scan MRD',
    route: 'scan-mrd',
    icon: 'qr_code_scanner',
    enabled: true,
    visibleInHome: true,
    visibleInNav: true,
    allowedRoles: ACTIVE_ROLE_VALUES,
    description: 'Scan packet QR codes into inventory, marination, and cooking flows.',
    badge: 'QR SCAN',
  },
  {
    id: 'create-mrd',
    label: 'Create MRD',
    route: 'create-mrd',
    icon: 'print',
    enabled: true,
    visibleInHome: true,
    visibleInNav: true,
    allowedRoles: [USER_ROLES.ADMIN, USER_ROLES.CORPORATE, USER_ROLES.RGM, USER_ROLES.INTERIM_MANAGER, USER_ROLES.STAFF],
    description: 'Create and print packet MRD labels.',
    badge: 'PRINT',
  },
  {
    id: 'inventory',
    label: 'Real-Time Inventory',
    navLabel: 'Stock',
    route: 'inventory',
    icon: 'inventory_2',
    enabled: true,
    visibleInHome: true,
    visibleInNav: true,
    allowedRoles: [],
    description: 'Live store stock and FEFO order.',
    badge: 'LIVE',
  },
  {
    id: 'devices',
    label: 'Devices',
    route: 'devices',
    icon: 'router',
    enabled: true,
    visibleInHome: true,
    visibleInNav: true,
    allowedRoles: ACTIVE_ROLE_VALUES,
    description: 'Printer, station, and device health.',
    badge: 'ONLINE',
  },
  {
    id: 'approve-accounts',
    label: 'Approve Accounts',
    navLabel: 'Approvals',
    route: 'approve-accounts',
    icon: 'admin_panel_settings',
    enabled: true,
    visibleInHome: false,
    visibleInNav: true,
    allowedRoles: [USER_ROLES.ADMIN],
    description: 'Approve and manage user access requests.',
    badge: 'ADMIN',
  },

  // Hidden legacy features. Code remains in the repo and can be re-enabled here later.
  { id: 'billing', label: 'Billing', route: 'billing', icon: 'receipt_long', enabled: false, visibleInHome: false, visibleInNav: false, allowedRoles: [USER_ROLES.ADMIN, USER_ROLES.CORPORATE, USER_ROLES.RGM] },
  { id: 'kitchen', label: 'Kitchen', route: 'kitchen', icon: 'soup_kitchen', enabled: false, visibleInHome: false, visibleInNav: false, allowedRoles: [USER_ROLES.ADMIN, USER_ROLES.CORPORATE, USER_ROLES.RGM, USER_ROLES.INTERIM_MANAGER, USER_ROLES.STAFF] },
  { id: 'distribution', label: 'Distribution', navLabel: 'Dist', route: 'distribution', icon: 'restaurant', enabled: false, visibleInHome: false, visibleInNav: false, allowedRoles: [USER_ROLES.ADMIN, USER_ROLES.CORPORATE, USER_ROLES.RGM] },
  { id: 'forecast', label: 'Forecast', route: 'forecast', icon: 'auto_graph', enabled: false, visibleInHome: false, visibleInNav: false, allowedRoles: [USER_ROLES.ADMIN, USER_ROLES.CORPORATE, USER_ROLES.RGM] },
  { id: 'shift', label: 'Shift', route: 'shift', icon: 'badge', enabled: false, visibleInHome: false, visibleInNav: false, allowedRoles: [USER_ROLES.ADMIN, USER_ROLES.RGM, USER_ROLES.INTERIM_MANAGER] },
  { id: 'waste', label: 'Waste', route: 'waste', icon: 'delete_sweep', enabled: false, visibleInHome: false, visibleInNav: false, allowedRoles: [USER_ROLES.ADMIN, USER_ROLES.CORPORATE, USER_ROLES.RGM] },
  { id: 'temp', label: 'Temperature', navLabel: 'Temp', route: 'temp', icon: 'thermostat', enabled: false, visibleInHome: false, visibleInNav: false, allowedRoles: [USER_ROLES.ADMIN, USER_ROLES.RGM, USER_ROLES.INTERIM_MANAGER, USER_ROLES.STAFF] },
  { id: 'kds', label: 'KDS', route: 'kds', icon: 'desktop_windows', enabled: false, visibleInHome: false, visibleInNav: false, allowedRoles: [USER_ROLES.ADMIN, USER_ROLES.RGM, USER_ROLES.INTERIM_MANAGER, USER_ROLES.STAFF] },
  { id: 'mrd', label: 'MRD', route: 'mrd', icon: 'sell', enabled: false, visibleInHome: false, visibleInNav: false, allowedRoles: [USER_ROLES.ADMIN, USER_ROLES.RGM, USER_ROLES.INTERIM_MANAGER, USER_ROLES.STAFF] },
  { id: 'packet-mrd', label: 'Packet MRD', route: 'packet-mrd', icon: 'qr_code_2', enabled: false, visibleInHome: false, visibleInNav: false, allowedRoles: ACTIVE_ROLE_VALUES },
]);

const FEATURE_BY_ROUTE = APP_FEATURES.reduce((map, feature) => {
  map[feature.route] = feature;
  return map;
}, {});

function isFeatureAllowed(featureOrRoute, profile) {
  const feature = typeof featureOrRoute === 'string' ? FEATURE_BY_ROUTE[featureOrRoute] : featureOrRoute;
  if (!feature || !feature.enabled) return false;
  return profileHasRole(profile, feature.allowedRoles);
}

function visibleFeaturesFor(profile, placement) {
  return APP_FEATURES.filter((feature) => {
    if (!feature.enabled) return false;
    if (placement === 'home' && !feature.visibleInHome) return false;
    if (placement === 'nav' && !feature.visibleInNav) return false;
    return profileHasRole(profile, feature.allowedRoles);
  });
}

Object.assign(window, {
  USER_ROLES,
  ROLE_ORDER,
  ACCOUNT_STATUS,
  STORES,
  DEFAULT_STORE,
  APP_FEATURES,
  FEATURE_BY_ROUTE,
  normalizeRole,
  roleLabel,
  isFeatureAllowed,
  visibleFeaturesFor,
});
