import { renderHook, act, waitFor } from '@testing-library/react';
import { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach, vi } from 'vitest';
import { useNotifications } from './useNotifications';
import type { Node } from '@/context/NodeContext';
import type { NotificationItem } from '../../dashboard/types';

vi.mock('@/lib/api', () => ({
  apiFetch: vi.fn(),
  fetchForNode: vi.fn(),
}));
vi.mock('@/components/ui/toast-store', () => ({ toast: { error: vi.fn() } }));

import { apiFetch, fetchForNode } from '@/lib/api';

const localNode: Node = { id: 1, name: 'Local', type: 'local', api_url: '', compose_dir: '', is_default: true, status: 'online', created_at: 0 };

const makeRemoteNode = (status: Node['status'], overrides: Partial<Node> = {}): Node => ({
  id: 2, name: 'Remote', type: 'remote', mode: 'proxy', api_url: '', compose_dir: '', is_default: false, status, created_at: 0, ...overrides,
});

const makeNotif = (overrides: Partial<NotificationItem> = {}): NotificationItem => ({
  id: 1, level: 'info', message: 'test', timestamp: 1000, is_read: 0, ...overrides,
});

class MockWS {
  static instances: MockWS[] = [];
  static readonly CONNECTING = 0;
  static readonly OPEN = 1;
  static readonly CLOSING = 2;
  static readonly CLOSED = 3;
  onopen: (() => void) | null = null;
  onmessage: ((e: { data: string }) => void) | null = null;
  onclose: ((e: { code: number }) => void) | null = null;
  onerror: ((e: unknown) => void) | null = null;
  readyState = 1; // OPEN
  send = vi.fn();
  close = vi.fn();
  constructor() { MockWS.instances.push(this); }
  static reset() { MockWS.instances = []; }
}

beforeEach(() => {
  MockWS.reset();
  vi.stubGlobal('WebSocket', MockWS);
  (apiFetch as ReturnType<typeof vi.fn>).mockResolvedValue({ ok: false, json: async () => [] });
  (fetchForNode as ReturnType<typeof vi.fn>).mockResolvedValue({ ok: false, json: async () => [] });
});
afterEach(() => { 
  vi.unstubAllGlobals(); 
  vi.clearAllMocks(); 
});

describe('useNotifications', () => {
  let originalError: typeof console.error;

  beforeAll(() => {
    originalError = console.error;
    console.error = (...args: unknown[]) => {
      if (typeof args[0] === 'string' && args[0].includes('was not wrapped in act')) {
        return;
      }
      originalError.call(console, ...args);
    };
  });

  afterAll(() => {
    console.error = originalError;
  });

  it('starts with empty notifications and disconnected state', () => {
    const { result } = renderHook(() =>
      useNotifications({ nodes: [localNode], onStateInvalidate: vi.fn(), onImageUpdatesChange: vi.fn() }),
    );
    expect(result.current.notifications).toEqual([]);
    expect(result.current.tickerConnected).toBe(false);
  });

  it('opens a local notification WebSocket on mount', () => {
    renderHook(() =>
      useNotifications({ nodes: [localNode], onStateInvalidate: vi.fn(), onImageUpdatesChange: vi.fn() }),
    );
    expect(MockWS.instances.length).toBeGreaterThanOrEqual(1);
    expect(MockWS.instances[0]).toBeDefined();
  });

  it('sets tickerConnected true when local WS opens', () => {
    const { result } = renderHook(() =>
      useNotifications({ nodes: [localNode], onStateInvalidate: vi.fn(), onImageUpdatesChange: vi.fn() }),
    );
    act(() => { MockWS.instances[0]?.onopen?.(); });
    expect(result.current.tickerConnected).toBe(true);
  });

  it('adds notification when local WS receives notification message', () => {
    const { result } = renderHook(() =>
      useNotifications({ nodes: [localNode], onStateInvalidate: vi.fn(), onImageUpdatesChange: vi.fn() }),
    );
    act(() => { MockWS.instances[0]?.onopen?.(); });
    act(() => {
      MockWS.instances[0]?.onmessage?.({
        data: JSON.stringify({ type: 'notification', payload: makeNotif({ id: 42, message: 'hello' }) }),
      });
    });
    expect(result.current.notifications).toHaveLength(1);
    expect(result.current.notifications[0].message).toBe('hello');
  });

  it('clearAllNotifications empties the local state', async () => {
    const { result } = renderHook(() =>
      useNotifications({ nodes: [localNode], onStateInvalidate: vi.fn(), onImageUpdatesChange: vi.fn() }),
    );
    act(() => { MockWS.instances[0]?.onopen?.(); });
    act(() => {
      MockWS.instances[0]?.onmessage?.({
        data: JSON.stringify({ type: 'notification', payload: makeNotif({ id: 1 }) }),
      });
    });
    expect(result.current.notifications).toHaveLength(1);
    act(() => { result.current.clearAllNotifications(); });
    await waitFor(() => expect(result.current.notifications).toHaveLength(0));
  });

  it('fires onImageUpdatesChange on state-invalidate with action="stack-updated"', () => {
    const onStateInvalidate = vi.fn();
    const onImageUpdatesChange = vi.fn();
    renderHook(() =>
      useNotifications({ nodes: [localNode], onStateInvalidate, onImageUpdatesChange }),
    );
    act(() => { MockWS.instances[0]?.onopen?.(); });
    act(() => {
      MockWS.instances[0]?.onmessage?.({
        data: JSON.stringify({
          type: 'state-invalidate', scope: 'image-updates', nodeId: 1,
          stackName: 'foo', action: 'stack-updated', ts: 1000,
        }),
      });
    });
    expect(onImageUpdatesChange).toHaveBeenCalledTimes(1);
    expect(onStateInvalidate).toHaveBeenCalledTimes(1);
  });

  it('does not fire onImageUpdatesChange on a generic state-invalidate', () => {
    const onStateInvalidate = vi.fn();
    const onImageUpdatesChange = vi.fn();
    renderHook(() =>
      useNotifications({ nodes: [localNode], onStateInvalidate, onImageUpdatesChange }),
    );
    act(() => { MockWS.instances[0]?.onopen?.(); });
    act(() => {
      MockWS.instances[0]?.onmessage?.({
        data: JSON.stringify({
          type: 'state-invalidate', scope: 'stack', nodeId: 1,
          stackName: 'foo', action: 'start', ts: 1000,
        }),
      });
    });
    expect(onStateInvalidate).toHaveBeenCalledTimes(1);
    expect(onImageUpdatesChange).not.toHaveBeenCalled();
  });

  it('deleteNotification removes the matching item', async () => {
    const { result } = renderHook(() =>
      useNotifications({ nodes: [localNode], onStateInvalidate: vi.fn(), onImageUpdatesChange: vi.fn() }),
    );
    act(() => { MockWS.instances[0]?.onopen?.(); });
    const notif = makeNotif({ id: 5, nodeId: localNode.id });
    act(() => {
      MockWS.instances[0]?.onmessage?.({
        data: JSON.stringify({ type: 'notification', payload: notif }),
      });
    });
    act(() => { result.current.deleteNotification({ ...notif, nodeId: localNode.id }); });
    await waitFor(() => expect(result.current.notifications).toHaveLength(0));
  });

  it('does not open a WS or poll an offline remote node', async () => {
    renderHook(() =>
      useNotifications({ nodes: [localNode, makeRemoteNode('offline')], onStateInvalidate: vi.fn(), onImageUpdatesChange: vi.fn() }),
    );
    // Only the local notification socket is created; the offline node is skipped.
    expect(MockWS.instances).toHaveLength(1);
    // The mount poll never targets the offline node either.
    await waitFor(() => expect(apiFetch).toHaveBeenCalled());
    expect(fetchForNode).not.toHaveBeenCalled();
  });

  it('opens a WS and polls an online remote node', async () => {
    renderHook(() =>
      useNotifications({ nodes: [localNode, makeRemoteNode('online')], onStateInvalidate: vi.fn(), onImageUpdatesChange: vi.fn() }),
    );
    // Local socket plus a per-node socket for the online remote.
    expect(MockWS.instances).toHaveLength(2);
    await waitFor(() => expect(fetchForNode).toHaveBeenCalledWith('/notifications', 2));
  });

  it('subscribes to the online node and skips the offline one in a mixed fleet', async () => {
    renderHook(() =>
      useNotifications({
        nodes: [localNode, makeRemoteNode('online', { id: 2 }), makeRemoteNode('offline', { id: 3, name: 'Dead' })],
        onStateInvalidate: vi.fn(), onImageUpdatesChange: vi.fn(),
      }),
    );
    // Local + online remote only; the offline node gets no socket.
    expect(MockWS.instances).toHaveLength(2);
    await waitFor(() => expect(fetchForNode).toHaveBeenCalledWith('/notifications', 2));
    expect(fetchForNode).not.toHaveBeenCalledWith('/notifications', 3);
  });

  it('closes the socket when a subscribed node transitions to offline', () => {
    const { rerender } = renderHook(
      ({ nodes }) => useNotifications({ nodes, onStateInvalidate: vi.fn(), onImageUpdatesChange: vi.fn() }),
      { initialProps: { nodes: [localNode, makeRemoteNode('online')] } },
    );
    // instances[0] is the local socket; instances[1] is the online remote's socket.
    expect(MockWS.instances).toHaveLength(2);
    const remoteWs = MockWS.instances[1];

    // Flip the node to offline: it leaves the active set, so the cleanup loop
    // must close its socket rather than reconnect to a dead node forever.
    act(() => { rerender({ nodes: [localNode, makeRemoteNode('offline')] }); });
    expect(remoteWs.close).toHaveBeenCalled();
  });

  it('still subscribes to and polls a remote node with unknown status', async () => {
    renderHook(() =>
      useNotifications({ nodes: [localNode, makeRemoteNode('unknown')], onStateInvalidate: vi.fn(), onImageUpdatesChange: vi.fn() }),
    );
    // 'unknown' is not yet probed, so it is treated as reachable (not filtered out)
    // on both the WS and the REST-poll surfaces.
    expect(MockWS.instances).toHaveLength(2);
    await waitFor(() => expect(fetchForNode).toHaveBeenCalledWith('/notifications', 2));
  });
});
