import { useAuth } from './useAuth'
import type { UserRole, RolePermissions } from '../types'

export function usePermissions() {
  const { hasPermission, hasRole } = useAuth()
  
  return {
    hasPermission,
    hasRole,
    canManageEvents: hasPermission('canManageEvents'),
    canManageUsers: hasPermission('canManageUsers'),
    canManageOrganization: hasPermission('canManageOrganization'),
    canAccessAdminPanel: hasPermission('canAccessAdminPanel'),
    canManageTerritories: hasPermission('canManageTerritories'),
    canViewAnalytics: hasPermission('canViewAnalytics'),
    canManagePayments: hasPermission('canManagePayments'),
    isUser: hasRole('user'),
    isAdmin: hasRole('admin'),
    isSuperAdmin: hasRole('super_admin'),
    isTerritoryManager: hasRole('territory_manager'),
  }
}

export function useRequireAuth() {
  const { requireAuth } = useAuth()
  return requireAuth
}

export function useRequireRole() {
  const { requireRole } = useAuth()
  return requireRole
}

export function useHasPermission() {
  const { hasPermission } = useAuth()
  return hasPermission
}

export function useHasRole() {
  const { hasRole } = useAuth()
  return hasRole
}

export function useCanAccess(permissions: (keyof RolePermissions)[], roles?: UserRole[]) {
  const { hasPermission, hasRole } = useAuth()
  
  const hasAllPermissions = permissions.every(permission => hasPermission(permission))
  const hasRequiredRole = roles ? roles.some(role => hasRole(role)) : true
  
  return hasAllPermissions && hasRequiredRole
}