import { test, expect } from '@playwright/test';
import { RecoverPage } from '../../../page-objects/RecoverPage';
import { recovery, recoveryErrors, users } from '../../../utils/test-data';

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

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

    await recoverPage.enterEmail('invalid-email');
    await expect(recoverPage.emailFormatError).toBeVisible();
  });

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

    await recoverPage.enterEmail(recovery.emailNotFound);
    await expect(recoverPage.emailFormatError).toBeVisible(); // сервер показывает ту же ошибку
  });

  test('Неверный код подтверждения', async ({ page }) => {
    const recoverPage = new RecoverPage(page);
    await recoverPage.goto();

    await recoverPage.enterEmail(recovery.emailExists);
    await expect(page).toHaveURL(/\/recoverpassword\/checkemail/);

    await recoverPage.enterVerificationCode('654321'); // неверный
    await expect(recoverPage.codeError).toBeVisible();
  });

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

    await recoverPage.enterEmail(recovery.emailExists);
    await recoverPage.enterVerificationCode(recovery.verificationCode);

    await recoverPage.enterNewPassword(users.newPassword.weakPassword, users.newPassword.weakPassword);
    await expect(recoverPage.passwordError).toBeVisible();
  });

  test('Пустое поле "Повторите пароль"', async ({ page }) => {
    const recoverPage = new RecoverPage(page);
    await recoverPage.goto();

    await recoverPage.enterEmail(recovery.emailExists);
    await recoverPage.enterVerificationCode(recovery.verificationCode);

    await recoverPage.enterNewPassword(users.newPassword.password, '');
    await expect(recoverPage.passwordRepeatError).toBeVisible();
  });

  test('Пароли не совпадают', async ({ page }) => {
    const recoverPage = new RecoverPage(page);
    await recoverPage.goto();

    await recoverPage.enterEmail(recovery.emailExists);
    await recoverPage.enterVerificationCode(recovery.verificationCode);

    await recoverPage.enterNewPassword(users.newPassword.password, users.newPassword.mismatchPassword);
    await expect(recoverPage.mismatchPasswordError).toBeVisible();
  });

});
