import useAuthStore from '../store/authStore'

/**
 * Returns a permissions object derived from the logged-in user.
 * Superadmin always has every permission regardless of stored flags.
 */
export default function usePermissions() {
  const user = useAuthStore(s => s.user)

  if (!user) {
    return {
      isSuperadmin: false,
      accessDashboard: false,
      accessWaiterApp: false,
      accessKds: false,
      cancelOrders: false,
      applyDiscounts: false,
      modifyPrices: false,
      openOrders: false,
      closeOrders: false,
      viewReports: false,
      manageStaff: false,
      manageTables: false,
      manageMenu: false,
      manageSettings: false,
    }
  }

  const superadmin = user.role === 'superadmin'

  const p = (flag) => superadmin || !!user[flag]

  return {
    isSuperadmin: superadmin,
    accessDashboard: p('perm_access_dashboard'),
    accessWaiterApp: p('perm_access_waiter_app'),
    accessKds:       p('perm_access_kds'),
    cancelOrders:    p('perm_cancel_orders'),
    applyDiscounts:  p('perm_apply_discounts'),
    modifyPrices:    p('perm_modify_prices'),
    openOrders:      p('perm_open_orders'),
    closeOrders:     p('perm_close_orders'),
    viewReports:     p('perm_view_reports'),
    manageStaff:     p('perm_manage_staff'),
    manageTables:    p('perm_manage_tables'),
    manageMenu:      p('perm_manage_menu'),
    manageSettings:  p('perm_manage_settings'),
  }
}
