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

test('Record item given from image-share', async ({ page }) => {

  let randomString = Math.random().toString(36).substring(2, 8);

  // Combine title prefix with the random string
  const finalTitle = `Gift ${randomString} from image-share`;

  // Create new ID using seed phrase "rigid shrug mobile…"
  await page.goto('./start');
  await page.getByText('You have a seed').click();
  await page.getByPlaceholder('Seed Phrase').fill('rigid shrug mobile smart veteran half all pond toilet brave review universe ship congress found yard skate elite apology jar uniform subway slender luggage');
  await page.getByRole('button', { name: 'Import' }).click();

  // Record something given
  await page.goto('./test');

  const fileChooserPromise = page.waitForEvent('filechooser');
  await page.getByTestId('fileInput').click();
  const fileChooser = await fileChooserPromise;
  await fileChooser.setFiles(path.join(__dirname, '..', 'public', 'img', 'icons', 'android-chrome-192x192.png'));
  await page.getByTestId('fileUploadButton').click();

  // on shared photo page, choose the gift option
  await page.getByRole('button').filter({ hasText: /gift/i }).click();

  await page.getByTestId('imagery').getByRole('img').isVisible();
  await page.getByPlaceholder('What was received').fill(finalTitle);
  await page.getByRole('spinbutton').fill('2');
  await page.getByRole('button', { name: 'Sign & Send' }).click();
  await expect(page.getByText('That gift was recorded.')).toBeVisible();


  // Refresh home view and check gift
  await page.goto('./');
  const item1 = page.locator('li').filter({ hasText: finalTitle });
  await expect(item1.getByRole('img')).toBeVisible();
});