import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { ThemeProvider } from '../../../theme';

const useUserSearchMock = jest.fn();
jest.mock('@/services/admin', () => ({ useUserSearch: (...args: unknown[]) => useUserSearchMock(...args) }));
jest.mock('@/services/search', () => ({ useDebouncedValue: (value: unknown) => value }));

import NursePicker from './NursePicker';
import type { AdminUserSummary } from '@/services/admin/types';

const ZAHRA: AdminUserSummary = {
  id: 101,
  displayName: 'زهرا رضایی',
  maskedPhone: '0912•••0001',
  roles: ['nurse'],
  nurseProfileId: 15,
};

describe('<NursePicker/>', () => {
  const wrap = (ui: React.ReactNode) => render(<ThemeProvider>{ui}</ThemeProvider>);

  beforeEach(() => {
    useUserSearchMock.mockReset();
    useUserSearchMock.mockReturnValue({ data: [ZAHRA], isFetching: false });
  });

  it('always searches with roleFilter="nurse"', async () => {
    const user = userEvent.setup();
    wrap(<NursePicker value={null} onChange={jest.fn()} label="پرستار" />);
    await user.type(screen.getByLabelText('پرستار'), 'زهرا');
    expect(useUserSearchMock).toHaveBeenLastCalledWith('زهرا', 'nurse');
  });

  it('returns the picked option, carrying nurseProfileId', async () => {
    const onChange = jest.fn();
    const user = userEvent.setup();
    wrap(<NursePicker value={null} onChange={onChange} label="پرستار" />);
    await user.click(screen.getByLabelText('پرستار'));
    await user.click(await screen.findByText('زهرا رضایی'));
    expect(onChange).toHaveBeenCalledWith(ZAHRA);
  });
});
