import { useMemo } from "react";

export default function useCriticalEvents({ accountEvents, accounts }) {
  const criticalEvents = useMemo(() => {
    const criticalTypes = new Set([
      "connect_failed",
      "invite_failed",
      "invite_user_invalid",
      "monitor_handler_error",
      "flood"
    ]);
    const now = Date.now();
    return (accountEvents || []).filter((event) => {
      const ts = new Date(event.createdAt).getTime();
      const recent = Number.isFinite(ts) ? (now - ts) < 24 * 60 * 60 * 1000 : true;
      return criticalTypes.has(event.eventType) && recent;
    });
  }, [accountEvents]);

  const criticalErrorAccounts = useMemo(() => {
    return accounts.filter((account) => account.status && account.status !== "ok");
  }, [accounts]);

  return { criticalEvents, criticalErrorAccounts };
}
