import { useMemo } from 'react';
import { useAuth } from '@/contexts/useAuth';
import {
  hasPermission,
  hasAnyPermission,
  hasAllPermissions,
} from '@/business/auth/permissions';
import type { PermissionName } from '@/shared/auth/permissions';

export interface UsePermissions {
  readonly permissions: readonly string[];
  readonly has: (permission: PermissionName) => boolean;
  readonly hasAny: (permissions: readonly PermissionName[]) => boolean;
  readonly hasAll: (permissions: readonly PermissionName[]) => boolean;
}

export function usePermissions(): UsePermissions {
  const { user } = useAuth();

  return useMemo(
    () => ({
      permissions: user?.permissions ?? [],
      has: (permission: PermissionName) => hasPermission(user, permission),
      hasAny: (permissions: readonly PermissionName[]) =>
        hasAnyPermission(user, permissions),
      hasAll: (permissions: readonly PermissionName[]) =>
        hasAllPermissions(user, permissions),
    }),
    [user],
  );
}
