/**
 * BulkActionBar — unit tests (R6UXS-002)
 */
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { describe, expect, it, vi } from 'vitest';

import type { BulkActionBarProps } from './BulkActionBar';
import { BulkActionBar } from './BulkActionBar';

describe('BulkActionBar', () => {
  const defaultProps: BulkActionBarProps = {
    selectedCount: 3,
    totalCount: 42,
    allMatchingSelected: false,
    onSelectAllMatching: vi.fn(),
    onClear: vi.fn(),
  };

  function setup(props?: Partial<BulkActionBarProps>) {
    return render(<BulkActionBar {...defaultProps} {...props} />);
  }

  it('renders the selected count label', () => {
    setup();
    expect(screen.getByText('3 selected')).toBeInTheDocument();
  });

  it('shows "select all N matching" shortcut when applicable', () => {
    setup();
    expect(screen.getByText('Select all 42 matching')).toBeInTheDocument();
  });

  it('hides "select all matching" when allMatchingSelected is true', () => {
    setup({ allMatchingSelected: true });
    expect(screen.queryByText(/Select all/)).not.toBeInTheDocument();
  });

  it('hides "select all matching" when selectedCount equals totalCount', () => {
    setup({ selectedCount: 42, totalCount: 42 });
    expect(screen.queryByText(/Select all/)).not.toBeInTheDocument();
  });

  it('hides "select all matching" when totalCount is null', () => {
    setup({ totalCount: null });
    expect(screen.queryByText(/Select all/)).not.toBeInTheDocument();
  });

  it('shows "All N matching selected" label when allMatchingSelected is true', () => {
    setup({ allMatchingSelected: true });
    expect(screen.getByText('All 42 matching selected')).toBeInTheDocument();
  });

  it('calls onClear when Clear button is clicked', async () => {
    const onClear = vi.fn();
    setup({ onClear });
    await userEvent.click(screen.getByRole('button', { name: /clear/i }));
    expect(onClear).toHaveBeenCalledTimes(1);
  });

  it('calls onSelectAllMatching when shortcut is clicked', async () => {
    const onSelectAllMatching = vi.fn();
    setup({ onSelectAllMatching });
    await userEvent.click(screen.getByText('Select all 42 matching'));
    expect(onSelectAllMatching).toHaveBeenCalledTimes(1);
  });

  it('renders children in the right slot', () => {
    render(
      <BulkActionBar {...defaultProps}>
        <button type="button">Send to Recovery Run</button>
      </BulkActionBar>,
    );
    expect(screen.getByRole('button', { name: /Send to Recovery Run/i })).toBeInTheDocument();
  });

  it('uses custom itemLabel', () => {
    setup({ itemLabel: 'posts' });
    expect(screen.getByText('3 posts')).toBeInTheDocument();
  });

  it('has correct toolbar role and aria-label', () => {
    setup();
    expect(screen.getByRole('toolbar', { name: /bulk actions/i })).toBeInTheDocument();
  });

  it('aria-live region announces selection count', () => {
    const { container } = setup();
    const liveRegion = container.querySelector('[aria-live="polite"]');
    expect(liveRegion).toBeInTheDocument();
    expect(liveRegion?.textContent).toMatch(/3 selected/);
  });

  it('renders page checkbox when showPageCheckbox=true', () => {
    setup({
      showPageCheckbox: true,
      onTogglePageAll: vi.fn(),
    });
    expect(screen.getByRole('checkbox')).toBeInTheDocument();
  });

  it('calls onTogglePageAll when page checkbox is clicked', async () => {
    const onTogglePageAll = vi.fn();
    setup({ showPageCheckbox: true, onTogglePageAll });
    await userEvent.click(screen.getByRole('checkbox'));
    expect(onTogglePageAll).toHaveBeenCalledTimes(1);
  });
});
