export const ROLES = ['SUPER_ADMIN', 'ADMIN', 'MANAGER', 'CASHIER', 'INVENTORY_STAFF'];

// Granular permission actions. SUPER_ADMIN implicitly has every action.
export const ACTIONS = {
  PRODUCT_VIEW: 'product.view',
  PRODUCT_CREATE: 'product.create',
  PRODUCT_UPDATE: 'product.update',
  PRODUCT_DELETE: 'product.delete',
  PRICE_EDIT: 'price.edit',
  INVENTORY_MANAGE: 'inventory.manage',
  PURCHASE_MANAGE: 'purchase.manage',
  POS_ACCESS: 'pos.access',
  SALE_CREATE: 'sale.create',
  SALE_VIEW_ALL: 'sale.view.all',
  RETURN_CREATE: 'return.create',
  REFUND_APPROVE_HIGH_VALUE: 'refund.approve.highvalue',
  CUSTOMER_MANAGE: 'customer.manage',
  HOLD_BILL: 'hold.bill',
  EXPENSE_MANAGE: 'expense.manage',
  REPORTS_VIEW_FINANCIAL: 'reports.view.financial',
  STAFF_MANAGE: 'staff.manage',
  SETTINGS_WRITE: 'settings.write',
  BACKUP_MANAGE: 'backup.manage',
  AUDIT_VIEW: 'audit.view',
  SHIFT_OPEN: 'shift.open',
  SHIFT_CLOSE_ANY: 'shift.close.any',
};

const ALL = Object.values(ACTIONS);

const PERMISSIONS = {
  SUPER_ADMIN: ALL,
  ADMIN: ALL,
  MANAGER: [
    ACTIONS.PRODUCT_VIEW,
    ACTIONS.PRODUCT_CREATE,
    ACTIONS.PRODUCT_UPDATE,
    ACTIONS.INVENTORY_MANAGE,
    ACTIONS.PURCHASE_MANAGE,
    ACTIONS.POS_ACCESS,
    ACTIONS.SALE_CREATE,
    ACTIONS.SALE_VIEW_ALL,
    ACTIONS.RETURN_CREATE,
    ACTIONS.REFUND_APPROVE_HIGH_VALUE,
    ACTIONS.CUSTOMER_MANAGE,
    ACTIONS.HOLD_BILL,
    ACTIONS.EXPENSE_MANAGE,
    ACTIONS.REPORTS_VIEW_FINANCIAL,
    ACTIONS.AUDIT_VIEW,
    ACTIONS.SHIFT_OPEN,
    ACTIONS.SHIFT_CLOSE_ANY,
  ],
  CASHIER: [
    ACTIONS.PRODUCT_VIEW,
    ACTIONS.POS_ACCESS,
    ACTIONS.SALE_CREATE,
    ACTIONS.CUSTOMER_MANAGE,
    ACTIONS.HOLD_BILL,
    ACTIONS.RETURN_CREATE,
    ACTIONS.SHIFT_OPEN,
  ],
  INVENTORY_STAFF: [
    ACTIONS.PRODUCT_VIEW,
    ACTIONS.PRODUCT_CREATE,
    ACTIONS.PRODUCT_UPDATE,
    ACTIONS.INVENTORY_MANAGE,
    ACTIONS.PURCHASE_MANAGE,
    ACTIONS.SHIFT_OPEN,
  ],
};

export const roleCan = (role, action) => {
  if (!role) return false;
  if (role === 'SUPER_ADMIN') return true;
  return (PERMISSIONS[role] || []).includes(action);
};

export const permissionsFor = (role) => (role === 'SUPER_ADMIN' ? [...ALL] : [...(PERMISSIONS[role] || [])]);
