import { UserRole } from './types'

export const ROLE_HIERARCHY: Record<UserRole, number> = {
  [UserRole.USER]: 0,
  [UserRole.GROWER]: 1,
  [UserRole.FARM_MANAGER]: 2,
  [UserRole.ADMIN]: 3,
}

export const ROLE_PERMISSIONS: Record<UserRole, string[]> = {
  [UserRole.USER]: [
    'plants:read',
    'plants:register',
    'transport:read',
    'demand:read',
    'demand:signal',
    'transparency:read',
  ],
  [UserRole.GROWER]: [
    'plants:read',
    'plants:write',
    'plants:register',
    'plants:clone',
    'transport:read',
    'transport:write',
    'demand:read',
    'demand:signal',
    'demand:supply',
    'transparency:read',
    'environment:read',
    'environment:write',
  ],
  [UserRole.FARM_MANAGER]: [
    'plants:read',
    'plants:write',
    'plants:register',
    'plants:clone',
    'plants:delete',
    'transport:read',
    'transport:write',
    'demand:read',
    'demand:signal',
    'demand:supply',
    'demand:forecast',
    'transparency:read',
    'transparency:write',
    'environment:read',
    'environment:write',
    'vertical-farm:read',
    'vertical-farm:write',
    'vertical-farm:manage',
  ],
  [UserRole.ADMIN]: [
    'plants:*',
    'transport:*',
    'demand:*',
    'transparency:*',
    'environment:*',
    'vertical-farm:*',
    'users:*',
    'system:*',
  ],
}

export function hasPermission(role: UserRole, permission: string): boolean {
  const permissions = ROLE_PERMISSIONS[role] || []

  // Check for exact match
  if (permissions.includes(permission)) {
    return true
  }

  // Check for wildcard permissions (e.g., 'plants:*' matches 'plants:read')
  const [resource] = permission.split(':')
  const wildcardPermission = `${resource}:*`
  if (permissions.includes(wildcardPermission)) {
    return true
  }

  return false
}

export function hasRole(userRole: UserRole, requiredRole: UserRole): boolean {
  return ROLE_HIERARCHY[userRole] >= ROLE_HIERARCHY[requiredRole]
}

export function hasAnyPermission(role: UserRole, permissions: string[]): boolean {
  return permissions.some(permission => hasPermission(role, permission))
}

export function hasAllPermissions(role: UserRole, permissions: string[]): boolean {
  return permissions.every(permission => hasPermission(role, permission))
}

export function getRoleLabel(role: UserRole): string {
  const labels: Record<UserRole, string> = {
    [UserRole.USER]: 'User',
    [UserRole.GROWER]: 'Grower',
    [UserRole.FARM_MANAGER]: 'Farm Manager',
    [UserRole.ADMIN]: 'Administrator',
  }
  return labels[role] || 'Unknown'
}

export function getAvailableRoles(): UserRole[] {
  return Object.values(UserRole)
}
