import * as React from 'react';
import {
  describe,
  it,
  expect,
  jest,
  beforeAll,
  afterEach,
  afterAll,
} from '@jest/globals';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
import { ChakraProvider, theme as chakraTheme } from '@chakra-ui/react';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';

import ErrorBoundary from '../../components/ErrorBoundary';
import { store } from '../../__data__/store';
import Page from '../arm';

const server = setupServer(
  http.get('/api/arm/masters', () => {
    return HttpResponse.json({
      success: true,
      body: [
        {
          id: '4545423234',
          name: 'Иван Иванов',
          phone: '+7 900 123 45 67',
        },
        {
          name: 'Олег Макаров',
          phone: '79001234567',
          id: '23423442',
        },
        {
          id: '345354234',
          name: 'Иван Галкин',
          schedule: [
            {
              id: 'order1',
              startWashTime: '2024-11-24T10:30:00.000Z',
              endWashTime: '2024-11-24T16:30:00.000Z',
            },
            {
              id: 'order2',
              startWashTime: '2024-11-24T11:30:00.000Z',
              endWashTime: '2024-11-24T17:30:00.000Z',
            },
          ],
          phone: '+7 900 123 45 67',
        },
      ],
    });
  }),
  http.post('/api/arm/orders', () => {
    return HttpResponse.json({
      success: true,
      body: [
        {
          id: 'order1',
          carNumber: 'A123BC',
          startWashTime: '2024-11-24T10:30:00.000Z',
          endWashTime: '2024-11-24T16:30:00.000Z',
          orderDate: '2024-11-24T08:41:46.366Z',
          status: 'pending',
          phone: '79001234563',
          location: 'Казань, ул. Баумана, 1',
          master: {
            name: 'Олег Макаров',
            phone: '79001234567',
            id: '23423442',
          },
          notes: '',
        },
        {
          id: 'order2',
          carNumber: 'A245BC',
          startWashTime: '2024-11-24T11:30:00.000Z',
          endWashTime: '2024-11-24T17:30:00.000Z',
          orderDate: '2024-11-24T07:40:46.366Z',
          status: 'progress',
          phone: '79001234567',
          location: 'Казань, ул. Баумана, 43',
          master: [],
          notes: '',
        },
      ],
    });
  }),
);

jest.mock('@brojs/cli', () => {
  return {
    getNavigationValue: (key: string) =>
      // eslint-disable-next-line @typescript-eslint/no-require-imports
      require('../../../bro.config').navigations[key],
    getConfigValue: () => '/api',
  };
});

describe('Master Page', () => {
  beforeAll(() => server.listen());
  afterEach(() => server.resetHandlers());
  afterAll(() => server.close());

  it('should display master list and show details when master button is clicked', async () => {
    const { container } = render(
      <Provider store={store}>
        <ChakraProvider theme={chakraTheme}>
          <ErrorBoundary>
            <BrowserRouter>
              <Page mockUser={{ name: 'ilnaz' }} />
            </BrowserRouter>
          </ErrorBoundary>
        </ChakraProvider>
      </Provider>,
    );

    const button = await waitFor(() => screen.getByTestId('master-button'));
    fireEvent.click(button);

    // Проверяем отображение всех мастеров
    await waitFor(() => {
      expect(screen.getByText('Иван Иванов')).toBeInTheDocument();
      expect(screen.getByText('Олег Макаров')).toBeInTheDocument();
      expect(screen.getByText('Иван Галкин')).toBeInTheDocument();
    });

    expect(container).toMatchSnapshot();
  });
});
