'use client';

/**
 * No-op realtime client.
 *
 * Background: this app's installed `socket.io-client` package ships an
 * `engine.io-client` peer whose `exports` field picks an `esm-debug` build
 * with a `ws` (Node-only) import. Next.js dev-mode webpack resolves it for
 * the client bundle and the compile fails ("'WebSocket' is not exported
 * from 'ws'"). Until the package install is fixed (or pinned to a
 * compatible pair), we keep the API surface but skip the socket entirely.
 *
 * Practical impact: the patient portal does not receive live notification
 * pushes — they show up when the user navigates and the bell badge GET
 * runs. The call page polls /api/appointments/:id every 3 s for status
 * changes (see `app/call/[appointmentId]/page.tsx`), so auto-leave on
 * doctor hangup still works reliably without WS.
 */

import { useEffect, useState } from 'react';

export interface NotificationPayload {
  id: string;
  kind: string;
  title: string;
  body: string;
  deepLink: string | null;
  metadata: Record<string, unknown> | null;
  createdAt: string;
}

type Listener = (event: NotificationPayload) => void;

/** No-op — returns an unsubscribe that does nothing. */
export function subscribeToNotifications(_listener: Listener): () => void {
  return () => {
    // intentionally empty
  };
}

/** Always-disconnected state. Components render fine without the badge. */
export function useRealtimeNotifications(): {
  connected: boolean;
  lastEvent: NotificationPayload | null;
  eventsSince: (
    predicate: (e: NotificationPayload) => boolean,
  ) => NotificationPayload[];
} {
  // useEffect kept just so the hook order matches if a real impl is dropped in.
  useEffect(() => {
    // intentionally empty
  }, []);
  const [state] = useState({
    connected: false,
    lastEvent: null as NotificationPayload | null,
  });
  return {
    connected: state.connected,
    lastEvent: state.lastEvent,
    eventsSince: () => [],
  };
}

/** No-op subscriber. */
export function useSocketEvent<T = unknown>(
  _eventName: string,
  _handler: (payload: T) => void,
): void {
  // intentionally empty
}

/** No-op tear-down. */
export function disconnectRealtime(): void {
  // intentionally empty
}
