import { usePage } from '@inertiajs/vue3';
import { PageProps } from '@/types';

export function useAuth() {
    const page = usePage<PageProps>();

    const user = () => page.props.auth?.user;
    const settings = () => page.props.settings;

    const hasRole = (role: string): boolean => {
        const currentUser = user();
        if (!currentUser) return false;
        
        // Super Admin bypasses all checks
        if (currentUser.roles.includes('Super Admin')) return true;
        
        return currentUser.roles.includes(role);
    };

    const hasPermission = (permission: string): boolean => {
        const currentUser = user();
        if (!currentUser) return false;

        // Super Admin bypasses all checks
        if (currentUser.roles.includes('Super Admin')) return true;

        return currentUser.permissions.includes(permission);
    };

    return {
        user,
        settings,
        hasRole,
        hasPermission,
    };
}
