/* eslint-disable testing-library/no-node-access */
import { vi, describe, it, expect, beforeEach } from 'vitest';
import React from 'react';
import userEvent from '@testing-library/user-event';
import { render, screen } from '@testing-library/react';
import {
  type FetchResponse,
  getDefaultsFromConfigSchema,
  useConfig,
  useLayoutType,
  useSession,
  type Visit,
} from '@openmrs/esm-framework';
import { configSchema, type ConfigObject } from '../../config-schema';
import { mockSession, mockVisitAlice } from '__mocks__';
import { postQueueEntry } from './queue-fields.resource';
import QueueFields from './queue-fields.component';

const mockUseConfig = vi.mocked(useConfig<ConfigObject>);
const mockUseLayoutType = vi.mocked(useLayoutType);
const mockUseSession = vi.mocked(useSession);

vi.mock('../hooks/useQueueLocations', () => ({
  useQueueLocations: vi.fn(() => ({ queueLocations: [{ id: '1', name: 'Location 1' }] })),
}));

vi.mock('../../hooks/useQueues', () => {
  return {
    useQueues: vi.fn().mockReturnValue({
      queues: [
        {
          uuid: 'e2ec9cf0-ec38-4d2b-af6c-59c82fa30b90',
          name: 'Service 1',
          allowedPriorities: [{ uuid: '197852c7-5fd4-4b33-89cc-7bae6848c65a', display: 'High' }],
          allowedStatuses: [{ uuid: '176052c7-5fd4-4b33-89cc-7bae6848c65a', display: 'In Progress' }],
        },
      ],
    }),
  };
});

vi.mock('./queue-fields.resource', () => {
  return { postQueueEntry: vi.fn() };
});

const mockPostQueueEntry = vi.mocked(postQueueEntry).mockResolvedValue({} as FetchResponse);

describe('QueueFields', () => {
  beforeEach(() => {
    mockUseLayoutType.mockReturnValue('small-desktop');
    mockUseSession.mockReturnValue(mockSession.data);
    mockUseConfig.mockReturnValue({ ...getDefaultsFromConfigSchema(configSchema) });
  });

  it('renders the form fields and returns the set values', async () => {
    const user = userEvent.setup();
    let onSubmit: (visit: Visit) => Promise<any> = null;
    const setOnSubmit = (callback) => {
      onSubmit = callback;
    };

    render(<QueueFields setOnSubmit={setOnSubmit} />);

    expect(screen.getByRole('group', { name: /queue location/i })).toBeInTheDocument();

    const locationSelect = screen.getByTitle(/select a queue location/i);
    await user.selectOptions(locationSelect, '1');

    expect(screen.getByRole('group', { name: /service/i })).toBeInTheDocument();

    const queueUuid = 'e2ec9cf0-ec38-4d2b-af6c-59c82fa30b90';
    const serviceSelect = screen.getByTitle(/select a queue service/i);
    await user.selectOptions(serviceSelect, queueUuid);

    expect(screen.getByText('Priority')).toBeInTheDocument();
    expect(screen.getByText('High')).toBeInTheDocument();

    await onSubmit(mockVisitAlice);
    expect(mockPostQueueEntry).toHaveBeenCalledWith(
      mockVisitAlice.uuid,
      queueUuid,
      mockVisitAlice.patient.uuid,
      '197852c7-5fd4-4b33-89cc-7bae6848c65a',
      '51ae5e4d-b72b-4912-bf31-a17efb690aeb',
      0,
      '1',
      'c0c579b0-8e59-401d-8a4a-976a0b183519',
    );
  });
});
