import { test, expect } from '@playwright/test';
import { LoginPage } from '../../../page-objects/LoginPage';
import fs from 'fs';
import {
  waitForConfirmationCode
} from '../../../utils/mailTmApi';

test('UI: восстановление пароля', async ({ page }) => {

  const loginPage = new LoginPage(page);
  const user = JSON.parse(fs.readFileSync('temp/user.json', 'utf-8'));
  const newPassword = '!Test12345678';


  await loginPage.goto();


  await page.getByText('Забыли пароль?').click();
  await expect(page).toHaveURL(/.*confirmation-code/);


  await page.fill('input[name="email"]', user.email);
  await page.getByRole('button', { name: 'Далее' }).click();

  const code = await waitForConfirmationCode(user.email, user.mailToken, 'recover');
  expect(code).toMatch(/^\d{6}$/);
  console.log('✅ Код получен:', code);

  await page.fill('input[name="code"]', code);
  await page.getByRole('button', { name: 'Далее' }).click();

  await page.fill('input[name="password"]', newPassword);
  await page.fill('input[name="passwordRepeat"]', newPassword);
  await page.getByRole('button', { name: 'Далее' }).click();

  await expect(page).toHaveURL(/.*\/account\/profile/);

});
