import type { SharedData } from '@/types';
import { usePage } from '@inertiajs/react';

/**
 * Hook for checking user permissions and roles.
 *
 * Uses the shared auth data from Inertia to provide
 * helper functions for authorization checks in components.
 */
export function usePermissions() {
    const { auth } = usePage<SharedData>().props;

    /** Check if user has a specific permission */
    const can = (permission: string): boolean =>
        auth.permissions.includes(permission);

    /** Check if user has a specific role */
    const hasRole = (role: string): boolean => auth.user.role === role;

    /** Check if user has any of the given roles */
    const hasAnyRole = (...roles: string[]): boolean =>
        roles.includes(auth.user.role);

    /** Shorthand: is the user an Administrator? */
    const isAdmin = (): boolean => hasRole('Administrator');

    return { can, hasRole, hasAnyRole, isAdmin, user: auth.user };
}
