/**
 * @ai-summary Tests for BulkActionDialog component
 */

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

describe('BulkActionDialog', () => {
  const defaultProps = {
    open: true,
    title: 'Delete Items?',
    message: 'This action cannot be undone.',
    items: ['Item 1', 'Item 2', 'Item 3'],
    onConfirm: jest.fn(),
    onCancel: jest.fn(),
  };

  it('should render dialog when open', () => {
    const { container } = render(<BulkActionDialog {...defaultProps} />);

    expect(container).toMatchSnapshot();
    expect(screen.getByText('Delete Items?')).toBeInTheDocument();
    expect(screen.getByText('This action cannot be undone.')).toBeInTheDocument();
  });

  it('should display list of items', () => {
    render(<BulkActionDialog {...defaultProps} />);

    expect(screen.getByText('Item 1')).toBeInTheDocument();
    expect(screen.getByText('Item 2')).toBeInTheDocument();
    expect(screen.getByText('Item 3')).toBeInTheDocument();
  });

  it('should call onConfirm when confirm button clicked', () => {
    const handleConfirm = jest.fn();

    render(<BulkActionDialog {...defaultProps} onConfirm={handleConfirm} />);

    fireEvent.click(screen.getByText('Confirm'));
    expect(handleConfirm).toHaveBeenCalledTimes(1);
  });

  it('should call onCancel when cancel button clicked', () => {
    const handleCancel = jest.fn();

    render(<BulkActionDialog {...defaultProps} onCancel={handleCancel} />);

    fireEvent.click(screen.getByText('Cancel'));
    expect(handleCancel).toHaveBeenCalledTimes(1);
  });

  it('should disable buttons when loading', () => {
    render(<BulkActionDialog {...defaultProps} loading={true} />);

    const confirmButton = screen.getByRole('button', { name: /confirm/i });
    const cancelButton = screen.getByRole('button', { name: /cancel/i });

    expect(confirmButton).toBeDisabled();
    expect(cancelButton).toBeDisabled();
  });

  it('should show loading spinner when loading', () => {
    const { container } = render(
      <BulkActionDialog {...defaultProps} loading={true} />
    );

    expect(container.querySelector('.MuiCircularProgress-root')).toBeInTheDocument();
  });

  it('should support custom button text', () => {
    render(
      <BulkActionDialog
        {...defaultProps}
        confirmText="Delete Now"
        cancelText="Go Back"
      />
    );

    expect(screen.getByText('Delete Now')).toBeInTheDocument();
    expect(screen.getByText('Go Back')).toBeInTheDocument();
  });
});
