// @vitest-environment happy-dom
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { act } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { useCommandPalette } from '@bytelyst/command-palette';
import { buildAdminCommands, NAV_COMMANDS } from '@/lib/admin-commands';

/**
 * UX-3 — command palette guard.
 *
 *  1. `buildAdminCommands` produces the expected navigate + action set, and the
 *     action callbacks are correctly wired.
 *  2. The ⌘K / Ctrl-K hotkey (via `useCommandPalette`) toggles the palette open.
 */

describe('buildAdminCommands', () => {
  const base = () => buildAdminCommands({ resolvedTheme: 'light', toggleTheme() {}, signOut() {} });

  it('includes navigate commands for the major surfaces', () => {
    const cmds = base();
    const navHrefs = cmds.filter(c => c.mode === 'navigate').map(c => c.href);
    for (const href of [
      '/users',
      '/billing',
      '/flags',
      '/broadcasts',
      '/audit',
      '/experiments',
      '/subscriptions',
      '/licenses',
      '/ops',
    ]) {
      expect(navHrefs).toContain(href);
    }
    expect(cmds.filter(c => c.mode === 'navigate')).toHaveLength(NAV_COMMANDS.length);
  });

  it('exposes theme-toggle + sign-out actions whose run callbacks fire', () => {
    const toggleTheme = vi.fn();
    const signOut = vi.fn();
    const cmds = buildAdminCommands({ resolvedTheme: 'dark', toggleTheme, signOut });

    const theme = cmds.find(c => c.id === 'action-toggle-theme');
    const out = cmds.find(c => c.id === 'action-sign-out');
    expect(theme?.label).toBe('Switch to light mode'); // dark -> offer light
    expect(out?.mode).toBe('actions');

    theme?.run?.();
    out?.run?.();
    expect(toggleTheme).toHaveBeenCalledTimes(1);
    expect(signOut).toHaveBeenCalledTimes(1);
  });

  it('flips the theme label based on the resolved theme', () => {
    const light = buildAdminCommands({ resolvedTheme: 'light', toggleTheme() {}, signOut() {} });
    expect(light.find(c => c.id === 'action-toggle-theme')?.label).toBe('Switch to dark mode');
  });
});

describe('⌘K hotkey opens the palette', () => {
  let container: HTMLDivElement;
  let root: Root;

  function Harness() {
    const cmdk = useCommandPalette();
    return <span data-testid="state">{cmdk.open ? 'open' : 'closed'}</span>;
  }

  beforeEach(() => {
    (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT =
      true;
    container = document.createElement('div');
    document.body.appendChild(container);
    root = createRoot(container);
  });

  afterEach(() => {
    act(() => root.unmount());
    container.remove();
  });

  function press(init: KeyboardEventInit) {
    act(() => {
      window.dispatchEvent(new KeyboardEvent('keydown', { bubbles: true, ...init }));
    });
  }

  it('toggles open on Cmd-K and closed again', () => {
    act(() => {
      root.render(<Harness />);
    });
    expect(container.textContent).toBe('closed');

    press({ key: 'k', metaKey: true });
    expect(container.textContent).toBe('open');

    press({ key: 'k', metaKey: true });
    expect(container.textContent).toBe('closed');
  });

  it('also opens on Ctrl-K', () => {
    act(() => {
      root.render(<Harness />);
    });
    press({ key: 'k', ctrlKey: true });
    expect(container.textContent).toBe('open');
  });

  it('ignores unmodified k and other keys', () => {
    act(() => {
      root.render(<Harness />);
    });
    press({ key: 'k' });
    press({ key: 'j', metaKey: true });
    expect(container.textContent).toBe('closed');
  });
});
