import { useAuth } from '@/contexts/AuthContext';

export type Role = 'user' | 'provider' | 'admin';

export const ROLE_PERMISSIONS: Record<Role, string[]> = {
  user: [
    'trip:create',
    'trip:read:own',
    'trip:update:own',
    'trip:delete:own',
    'journal:read:own',
    'journal:write:own'
  ],
  provider: [
    'trip:create',
    'trip:read:own',
    'trip:update:own',
    'trip:delete:own',
    'lead:read',
    'lead:purchase',
    'provider:dashboard',
    'provider:wallet'
  ],
  admin: [
    'trip:read:all',
    'trip:update:all',
    'trip:delete:all',
    'user:read:all',
    'user:update:all',
    'user:delete:all',
    'provider:read:all',
    'provider:update:all',
    'lead:read:all',
    'lead:update:all',
    'admin:dashboard',
    'admin:settings',
    'admin:pricing',
    'admin:rate-limits'
  ]
};

export function usePermissions() {
  const { profile } = useAuth();

  const hasPermission = (permission: string): boolean => {
    if (!profile?.role) return false;
    const role = profile.role as Role;
    return ROLE_PERMISSIONS[role]?.includes(permission) || role === 'admin';
  };

  const hasAnyPermission = (permissions: string[]): boolean => {
    return permissions.some(permission => hasPermission(permission));
  };

  const hasAllPermissions = (permissions: string[]): boolean => {
    return permissions.every(permission => hasPermission(permission));
  };

  return {
    hasPermission,
    hasAnyPermission,
    hasAllPermissions
  };
}
