import { App as AntApp, ConfigProvider } from 'antd';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { describe, expect, it, vi } from 'vitest';

import { AddDomainDrawer } from '../../src/components/domain/AddDomainDrawer';
import { I18nProvider } from '../../src/frontend/i18n/react';
import { mailhubTheme } from '../../src/frontend/theme';
import type { RuntimeConfig } from '../../src/frontend/types';

describe('AddDomainDrawer', () => {
  it('progressively collects a domain and submits the three-step review', async () => {
    const user = userEvent.setup();
    const onSubmit = vi.fn(async () => undefined);
    render(
      <ConfigProvider theme={mailhubTheme}>
        <AntApp>
          <I18nProvider>
            <AddDomainDrawer
              open
              config={runtimeConfig}
              dnsCredentials={[]}
              smtpRelays={[]}
              onClose={vi.fn()}
              onSubmit={onSubmit}
            />
          </I18nProvider>
        </AntApp>
      </ConfigProvider>
    );

    await user.type(screen.getByLabelText('域名'), 'Example.COM');
    await user.click(screen.getByRole('button', { name: '下一步' }));
    expect(await screen.findByText('手动配置 DNS')).toBeTruthy();
    await user.click(screen.getByRole('button', { name: '下一步' }));
    expect(await screen.findByText('检查并创建')).toBeTruthy();
    await user.click(screen.getByRole('button', { name: /创建并验证/ }));

    await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1));
    expect(onSubmit.mock.calls[0][0]).toMatchObject({
      domain: 'example.com',
      senderHost: 'mail.example.test',
      sendingIp: '192.0.2.10',
      immediateCheck: true
    });
  });
});

const runtimeConfig: RuntimeConfig = {
  appBaseUrl: 'https://mail.example.test',
  mailHostname: 'mail.example.test',
  sendingIp: '192.0.2.10',
  defaultSpfMechanisms: '',
  dmarcPolicy: 'none',
  dmarcRua: '',
  registrationRequiresApproval: false,
  sendRequiresVerified: true,
  engagementTrackingEnabled: true,
  listUnsubscribeMailto: '',
  listUnsubscribeUrl: '',
  listUnsubscribePostEnabled: false,
  feedbackIdEnabled: false,
  reportAbuseTo: '',
  csaComplaintsTo: '',
  bounceAddress: '',
  bounceEnvelopeEnabled: false
};
