/**
 * QuickActionsMenu Component Tests
 *
 * Tests the Reindex with Google menu item, covering:
 * - Rendering when menu is open
 * - Disabled states (canReindexGSC=false, isReindexingGSC=true)
 * - Enabled state calls onReindexGSC callback
 */

import { render, screen, fireEvent } from '@testing-library/react';
import { QuickActionsMenu } from '../QuickActionsMenu';

describe('QuickActionsMenu — Reindex with Google item', () => {
  const baseProps = {
    onStartSupport: jest.fn(),
    onEditDNS: jest.fn(),
    onRegeneratePages: jest.fn(),
    onReindexGSC: jest.fn(),
    stripeCustomerId: 'cus_x',
    hasSubdomain: true,
    canReindexGSC: true,
    isReindexingGSC: false,
  };

  beforeEach(() => jest.clearAllMocks());

  it('renders the "Reindex with Google" item when menu opens', () => {
    render(<QuickActionsMenu {...baseProps} />);
    fireEvent.click(screen.getByRole('button', { name: /Actions menu/i }));
    expect(screen.getByRole('menuitem', { name: /Reindex with Google/i })).toBeInTheDocument();
  });

  it('disables the item when canReindexGSC=false (e.g. pending job exists)', () => {
    render(<QuickActionsMenu {...baseProps} canReindexGSC={false} />);
    fireEvent.click(screen.getByRole('button', { name: /Actions menu/i }));
    expect(screen.getByRole('menuitem', { name: /Reindex with Google/i })).toBeDisabled();
  });

  it('disables the item when isReindexingGSC=true (just-clicked)', () => {
    render(<QuickActionsMenu {...baseProps} isReindexingGSC={true} />);
    fireEvent.click(screen.getByRole('button', { name: /Actions menu/i }));
    // Label changes to "Reindexing..." while in progress; query by id to be label-agnostic
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    const item = document.getElementById('menu-reindex-gsc')!;
    expect(item).toBeInTheDocument();
    expect(item).toBeDisabled();
  });

  it('calls onReindexGSC when clicked and enabled', () => {
    render(<QuickActionsMenu {...baseProps} />);
    fireEvent.click(screen.getByRole('button', { name: /Actions menu/i }));
    fireEvent.click(screen.getByRole('menuitem', { name: /Reindex with Google/i }));
    expect(baseProps.onReindexGSC).toHaveBeenCalledTimes(1);
  });
});
