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

test.describe('authentication screens', () => {
  test('login page shows the sign-in form', async ({ page }) => {
    await page.goto('/auth/login');

    await expect(page).toHaveTitle('Light-E-Tech Consulting');
    await expect(page.locator('.loginbox-title')).toHaveText('Log in');
    await expect(page.getByPlaceholder('Username')).toBeFocused();
    await expect(page.getByPlaceholder('Password')).toHaveAttribute('type', 'password');
    await expect(page.getByRole('button', { name: 'Login' })).toBeVisible();
    await expect(page.getByRole('link', { name: /Forgot Password/ })).toBeVisible();
    await expect(page.getByRole('button', { name: 'Register' })).toBeVisible();
  });

  test('password visibility can be toggled', async ({ page }) => {
    await page.goto('/auth/login');

    const password = page.locator('#password');
    await password.fill('not-submitted');
    await page.getByRole('button', { name: 'Toggle password visibility' }).click();
    await expect(password).toHaveAttribute('type', 'text');
    await expect(password).toHaveValue('not-submitted');

    await page.getByRole('button', { name: 'Toggle password visibility' }).click();
    await expect(password).toHaveAttribute('type', 'password');
  });

  test('submitting without a username asks for the username', async ({ page }) => {
    await page.goto('/auth/login');
    await page.getByRole('button', { name: 'Login' }).click();

    await expect(page.getByText('Please enter the username.')).toBeVisible();
    await expect(page).toHaveURL(/\/auth\/login\/attempt$/);
  });

  test('a username without a password asks for the password', async ({ page }) => {
    await page.goto('/auth/login');
    await page.getByPlaceholder('Username').fill('playwright-not-a-user');
    await page.getByRole('button', { name: 'Login' }).click();

    await expect(page.getByText('Please enter password.')).toBeVisible();
  });

  test('forgot password explains what to enter and can return to login', async ({ page }) => {
    await page.goto('/auth/forgot_password');

    await expect(page.getByText('Forgot Password', { exact: true })).toBeVisible();
    await expect(page.getByText('Please fill the email address associated with the account')).toBeVisible();
    await expect(page.getByPlaceholder('Email')).toBeVisible();
    await expect(page.getByRole('button', { name: 'Reset Password' })).toBeVisible();

    await page.getByRole('link', { name: 'Back to Login' }).click();
    await expect(page).toHaveURL(/\/auth\/login$/);
    await expect(page.locator('.loginbox-title')).toHaveText('Log in');
  });

  test('register is reached from login and explains how the email is used', async ({ page }) => {
    await page.goto('/auth/login');
    await page.getByRole('button', { name: 'Register' }).click();

    await expect(page).toHaveURL(/\/auth\/register$/);
    await expect(page.locator('.loginbox-title')).toHaveText('Register');
    await expect(page.getByPlaceholder('Email')).toBeVisible();
    await expect(page.getByText(/This email is just to communicate about your account/)).toBeVisible();
    await expect(page.getByText(/Your email will only be used for the creation of a new account/)).toBeVisible();

    await page.getByRole('link', { name: 'Back to Login' }).click();
    await expect(page).toHaveURL(/\/auth\/login$/);
  });
});
