import { test, expect } from '@playwright/test';

test.describe('contact form', () => {
  test.beforeEach(async ({ page }) => {
    page.on('dialog', (dialog) => dialog.accept());
  });

  test('the form shows the contact fields', async ({ page }) => {
    await page.goto('/company/contact');

    await expect(page.getByRole('heading', { name: 'Contact', exact: true }).first()).toBeVisible();
    await expect(page.getByRole('heading', { name: 'Send Us A Message' }).first()).toBeVisible();
    await expect(page.getByPlaceholder('First Name (mandatory)')).toBeVisible();
    await expect(page.getByPlaceholder('Last Name')).toBeVisible();
    await expect(page.getByPlaceholder('Company')).toBeVisible();
    await expect(page.getByPlaceholder('Email (mandatory)')).toBeVisible();
    await expect(page.getByPlaceholder('Phone')).toBeVisible();
    await expect(page.getByPlaceholder('Your comments (mandatory)')).toBeVisible();
    await expect(page.getByRole('button', { name: 'Send Message' })).toBeVisible();
  });

  test('sending stays on the contact page and reports that delivery failed', async ({ page }) => {
    await page.goto('/company/contact');

    await page.getByPlaceholder('First Name (mandatory)').fill('Playwright');
    await page.getByPlaceholder('Email (mandatory)').fill('not-an-email');
    await page.getByPlaceholder('Your comments (mandatory)').fill('Automated check only. Do not deliver.');
    await page.getByRole('button', { name: 'Send Message' }).click();

    await expect(page.locator('#message')).toBeVisible();
    await expect(page.locator('#message')).toContainText(
      'There is an issue sending messages at this time. Please user alternate ways to contact us',
    );
    await expect(page).toHaveURL(/\/company\/contact$/);
    await expect(page.getByPlaceholder('First Name (mandatory)')).toHaveValue('Playwright');
  });
});
