import { test, expect } from '@playwright/test';
import { LoginPage } from '../../../page-objects/LoginPage';
import { users, loginErrors } from '../../../utils/test-data';

test.describe('Негативные сценарии авторизации', () => {

  test('Неверный пароль', async ({ page }) => {
    const loginPage = new LoginPage(page);
    await loginPage.goto();

    await loginPage.login(users.valid.email, users.invalid.wrongPassword);

    const passwordError = page.locator('div[name="error"]', { hasText: loginErrors.wrongPassword });
    await expect(passwordError).toBeVisible();
  });

  test('Несуществующий email', async ({ page }) => {
    const loginPage = new LoginPage(page);
    await loginPage.goto();

    await loginPage.login(users.invalid.wrongEmail, 'anyPassword123');

    const loginError = page.locator('div[name="error"]', { hasText: loginErrors.wrongLogin });
    await expect(loginError).toBeVisible();
  });

  test('Оба поля пустые', async ({ page }) => {
    const loginPage = new LoginPage(page);
    await loginPage.goto();

    await loginPage.login(users.invalid.emptyEmail, users.invalid.emptyPassword);

    await expect(page.locator('div[name="error"]', { hasText: loginErrors.emptyEmail })).toBeVisible();
    await expect(page.locator('div[name="error"]', { hasText: loginErrors.emptyPassword })).toBeVisible();
  });

  test('Только email без пароля', async ({ page }) => {
    const loginPage = new LoginPage(page);
    await loginPage.goto();

    await loginPage.login(users.valid.email, users.invalid.emptyPassword);

    await expect(page.locator('div[name="error"]', { hasText: loginErrors.emptyPassword })).toBeVisible();
  });

  test('Некорректный формат email', async ({ page }) => {
    const loginPage = new LoginPage(page);
    await loginPage.goto();

    await loginPage.emailInput.fill(users.invalid.malformedEmail);
    await loginPage.passwordInput.fill('anyPassword123');
    await loginPage.loginButton.click();

    const emailFormatError = page.locator('div[name="error"]', { hasText: loginErrors.malformedEmail });
    await expect(emailFormatError).toBeVisible();
  });

  test('Регистр в email (User@test.com ≠ user@test.com)', async ({ page }) => {
    const loginPage = new LoginPage(page);
    await loginPage.goto();

    await loginPage.login(users.invalid.upperCaseEmail, users.valid.password);

    const loginError = page.locator('div[name="error"]', { hasText: loginErrors.wrongLogin });
    await expect(loginError).toBeVisible();
  });

});
