/**
 * Permission helper utilities
 * Mirror backend permission structure for frontend UI/UX
 * 
 * NOTE: These are for UI purposes only. Backend enforces actual permissions.
 */

export type Permission = 
  // Financial permissions
  | 'financial.view_reports'
  | 'financial.manage_invoices'
  | 'financial.manage_payments'
  | 'financial.manage_settings'
  | 'financial.high_risk_approve'
  | 'financial.view_invoices'
  | 'financial.view_payments'
  | 'financial.view_audit_trail'
  
  // User management permissions
  | 'users.manage'
  
  // Housekeeping permissions
  | 'housekeeping.view_tasks'
  | 'housekeeping.manage_tasks'
  | 'housekeeping.create_tasks'
  | 'housekeeping.view_rooms'
  | 'housekeeping.upload_photos'
  | 'housekeeping.report_issues'
  | 'housekeeping.view_inspections'
  | 'housekeeping.manage_inspections'
  
  // Booking permissions
  | 'bookings.manage'
  | 'bookings.view_own'
  | 'bookings.create'
  
  // Room permissions
  | 'rooms.manage'
  | 'rooms.view'
  
  // Profile permissions
  | 'profile.manage';

/**
 * Check if a role has a specific permission
 * This is a pure function that can be used outside of React components
 */
export const roleHasPermission = (
  roleName: string | undefined | null,
  permission: Permission
): boolean => {
  if (!roleName) return false;

  // Import permissions from hook (circular dependency issue if we import directly)
  // Instead, we'll define a simplified check here
  const rolePermissions: Record<string, Permission[]> = {
    admin: [
      'financial.view_reports',
      'financial.manage_invoices',
      'financial.manage_payments',
      'financial.manage_settings',
      'financial.high_risk_approve',
      'users.manage',
      'housekeeping.view_tasks',
      'housekeeping.manage_tasks',
      'housekeeping.view_rooms',
      'bookings.manage',
      'rooms.manage',
    ],
    accountant: [
      'financial.view_reports',
      'financial.manage_invoices',
      'financial.manage_payments',
      'financial.manage_settings',
      'financial.high_risk_approve',
    ],
    accountant_readonly: [
      'financial.view_reports',
      'financial.view_invoices',
      'financial.view_payments',
      'financial.view_audit_trail',
    ],
    accountant_operator: [
      'financial.view_reports',
      'financial.view_invoices',
      'financial.view_payments',
      'financial.manage_invoices',
      'financial.manage_payments',
    ],
    accountant_approver: [
      'financial.view_reports',
      'financial.view_invoices',
      'financial.view_payments',
      'financial.high_risk_approve',
    ],
    staff: [
      'financial.view_invoices',
      'financial.manage_invoices',
      'financial.view_payments',
      'housekeeping.view_tasks',
      'housekeeping.manage_tasks',
      'housekeeping.view_rooms',
      'bookings.manage',
      'rooms.manage',
    ],
    housekeeping: [
      'housekeeping.view_tasks',
      'housekeeping.manage_tasks',
      'housekeeping.create_tasks',
      'housekeeping.view_rooms',
      'housekeeping.upload_photos',
      'housekeeping.report_issues',
      'housekeeping.view_inspections',
      'housekeeping.manage_inspections',
    ],
    customer: [
      'bookings.view_own',
      'bookings.create',
      'rooms.view',
      'profile.manage',
    ],
  };

  const permissions = rolePermissions[roleName] || [];
  return permissions.includes(permission);
};

/**
 * Check if role is in accountant family
 */
export const isAccountantRole = (roleName: string | undefined | null): boolean => {
  if (!roleName) return false;
  return roleName === 'accountant' || roleName.startsWith('accountant_');
};

/**
 * Check if role can access financial features
 */
export const canAccessFinancial = (roleName: string | undefined | null): boolean => {
  if (!roleName) return false;
  return ['admin', 'staff', 'accountant', 'accountant_readonly', 'accountant_operator', 'accountant_approver']
    .includes(roleName);
};

