import React from 'react';
import { useQuery } from '@tanstack/react-query';
import { listNotificationLogs } from '../../api';

/**
 * Badge count for notifications bell in the mobile shell.
 * Uses tenant notification logs so the badge matches the notifications screen.
 */
export function useNotificationsBadge() {
  const { data: count = 0 } = useQuery<number>({
    queryKey: ['mobile', 'notifications', 'badge', 'tenant'],
    staleTime: 60_000,
    queryFn: async () => {
      const logs = await listNotificationLogs({ perPage: 1 });
      const meta: any = logs.meta ?? {};
      if (typeof meta.unread_count === 'number') {
        return meta.unread_count;
      }
      return Array.isArray(logs.data) ? logs.data.filter((log) => log.is_read === false).length : 0;
    },
    retry: 1,
  });

  return React.useMemo(() => ({ count }), [count]);
}
