import { test, expectFixture as expect } from '../helpers/test-fixtures';

test.describe('Marketing auth flows', () => {
  test('registers a new account and captures welcome email', async ({ page, clearTestMailbox, getTestMailbox }) => {
    await clearTestMailbox();

    const stamp = Date.now();
    const email = `playwright-register-${stamp}@example.test`;
    const username = `playwright-${stamp}`;
    const password = 'Password123!';

    await page.goto('/register');

    await page.getByLabel(/Vorname/i).fill('Playwright');
    await page.getByLabel(/Nachname/i).fill('Tester');
    await page.getByLabel(/^E-Mail/i).fill(email);
    await page.getByLabel(/Telefon/i).fill('+49123456789');
    await page.getByLabel(/Adresse/i).fill('Teststr. 1, 12345 Berlin');
    await page.getByLabel(/Username/i).fill(username);
    await page.getByLabel(/^Passwort$/i).fill(password);
    await page.getByLabel(/Passwort bestätigen/i).fill(password);
    await page.locator('#privacy_consent').check();

    await page.getByRole('button', { name: /^Registrieren$/i }).click();

    await expect.poll(() => page.url()).not.toContain('/register');

    const messages = await getTestMailbox();
    const hasWelcome = messages.some((message) =>
      message.to.some((recipient) => recipient.email === email)
    );

    expect(hasWelcome).toBe(true);
  });

  test('shows inline error on invalid login', async ({ page }) => {
    await page.goto('/login');

    await page.fill('input[name="login"]', `unknown-${Date.now()}@example.test`);
    await page.fill('input[name="password"]', 'totally-wrong');
    await page.getByRole('button', { name: /^Anmelden$/i }).click();

    await expect(
      page.getByText(/Diese Anmeldedaten wurden nicht gefunden/i).first()
    ).toBeVisible();
  });

  test('sends password reset email notice', async ({ page }) => {
    await page.goto('/forgot-password');

    await page.getByLabel(/Email address/i).fill(`ghost-${Date.now()}@example.test`);
    await page.getByRole('button', { name: /Email password reset link/i }).click();

    await expect(
      page.getByText(/reset link will be sent if the account exists/i)
    ).toBeVisible();
  });
});
