import { test, expect } from '@playwright/test';
import { importUser, createUniqueStringsArray, createRandomNumbersArray  } from './testUtils';

test('Record 9 new gifts', async ({ page }) => {
  const giftCount = 9; // because 10 has taken us above 30 seconds

  // Standard text
  const standardTitle = 'Gift ';

  // Field value arrays
  const finalTitles = [];
  const finalNumbers = [];

  // Create arrays for field input
  const uniqueStrings = await createUniqueStringsArray(giftCount);
  const randomNumbers = await createRandomNumbersArray(giftCount);

  // Populate array with titles
  for (let i = 0; i < giftCount; i++) {
    let loopTitle = standardTitle + uniqueStrings[i];
    finalTitles.push(loopTitle);
    let loopNumber = randomNumbers[i];
    finalNumbers.push(loopNumber);
  }

  // Import user 00
  await importUser(page, '00');

  // Record new gifts
  for (let i = 0; i < giftCount; i++) {
    // Record something given
    await page.goto('./');
    if (i === 0) {
      await page.getByTestId('closeOnboardingAndFinish').click();
    }
    await page.getByRole('heading', { name: 'Unnamed/Unknown' }).click();
    await page.getByPlaceholder('What was given').fill(finalTitles[i]);
    await page.getByRole('spinbutton').fill(finalNumbers[i].toString());
    await page.getByRole('button', { name: 'Sign & Send' }).click();
    await expect(page.getByText('That gift was recorded.')).toBeVisible();
    await page.locator('div[role="alert"] button > svg.fa-xmark').click(); // dismiss info alert

    // Refresh home view and check gift
    await page.goto('./');
    await expect(page.locator('li').filter({ hasText: finalTitles[i] })).toBeVisible();
  }
});