import '@testing-library/jest-dom';
import { DataSourcePickerProps } from '@grafana/runtime';
import { render, screen, waitFor } from '@testing-library/react';
import React from 'react';
import { PrometheusWriteTarget } from '../types';
import { DataSourceInstanceSettings, NavModel } from '@grafana/data';
import { WriteTargetConfigUnconnected } from './WriteTargetConfig';
import { of } from 'rxjs';
import userEvent from '@testing-library/user-event';

function MockPicker(props: DataSourcePickerProps) {
  return <></>;
}

const settingsMock = jest.fn().mockReturnValue({ uid: 'test-datasource-uid' } as DataSourceInstanceSettings);
const fetchMock = jest.fn().mockReturnValue(of({ data: { message: 'hello' } }));

jest.mock('@grafana/runtime', () => {
  const original = jest.requireActual('@grafana/runtime');
  const mockedRuntime = { ...original };

  mockedRuntime.getDataSourceSrv = () => {
    return {
      getInstanceSettings: settingsMock,
    };
  };

  mockedRuntime.getBackendSrv = () => ({
    fetch: fetchMock,
  });

  mockedRuntime.DataSourcePicker = MockPicker;

  return mockedRuntime;
});

describe('WriteTargetConfig', () => {
  it('renders the datasource picker and remote_write_target', async () => {
    const prometheusWriteTarget: PrometheusWriteTarget = {
      data_source_uid: 'test-uid',
      remote_write_path: 'test-path',
    };
    render(
      <WriteTargetConfigUnconnected
        navModel={{ node: {}, main: {} } as NavModel}
        prometheusWriteTarget={prometheusWriteTarget}
        getPrometheusWriteTarget={() => {}}
      />
    );

    expect(screen.getByDisplayValue('test-path')).toBeInTheDocument();

    await waitFor(() => {
      expect(settingsMock).toHaveBeenCalledWith('test-uid');
    });

    userEvent.click(screen.getByLabelText('Write target config save'));
    expect(screen.getByLabelText('Write target config save')).not.toBeDisabled();

    await waitFor(() => {
      expect(fetchMock).toHaveBeenCalledWith({
        method: 'POST',
        url: 'api/recording-rules/writer',
        data: { data_source_uid: 'test-datasource-uid', remote_write_path: 'test-path' },
        showSuccessAlert: false,
        showErrorAlert: false,
      });
    });
  });

  it('should disable save button if write target is not set', async () => {
    render(
      <WriteTargetConfigUnconnected
        navModel={{ node: {}, main: {} } as NavModel}
        prometheusWriteTarget={undefined}
        getPrometheusWriteTarget={() => {}}
      />
    );

    expect(screen.getByLabelText('Write target config save')).toBeDisabled();
  });
});
