import React from 'react';
import {
  describe,
  it,
  expect,
  jest,
  beforeAll,
  afterEach,
  afterAll,
} from '@jest/globals';
import { render, screen, waitFor } from '@testing-library/react';
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
import { BrowserRouter } from 'react-router-dom';

import Page from '../arm';

const server = setupServer(
  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: '',
        },
      ],
    });
  }),
  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',
        },
      ],
    });
  }),
);

jest.mock('react-i18next', () => {
  return {
    useTranslation: () => {
      return {
        t: (key: never) => `${key}`,
        i18n: {},
      };
    },
  };
});

jest.mock('@brojs/cli', () => {
  return {
    getNavigationValue: () => '/auth/login',
    getConfigValue: () => '/api',
  };
});

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

  it('render ', async () => {
    server.events.on('request:start', ({ request }) => {
      console.log('Outgoing:', request.method, request.url);
    });

    const { container } = render(
      <BrowserRouter>
        <Page mockUser={{ name: 'ilnaz' }} />
      </BrowserRouter>,
    );

    expect(container).toMatchSnapshot();

    await waitFor(() => screen.getByText('A123BC'));

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