/**
 * @file Gift Recording Test Suite
 * @description Tests TimeSafari's core gift recording functionality, ensuring proper creation,
 * validation, and verification of gift records
 * 
 * This test verifies:
 * 1. Gift Creation
 *    - Random gift title generation
 *    - Random non-zero amount assignment
 *    - Proper recording and signing
 * 
 * 2. Gift Verification
 *    - Gift appears in home view
 *    - Details match input data
 *    - Verifiable claim details accessible
 * 
 * 3. Public Verification
 *    - Gift viewable on public server
 *    - Claim details properly exposed
 * 
 * Test Flow:
 * 1. Data Generation
 *    - Generate random 4-char string for unique gift ID
 *    - Generate random amount (1-99)
 *    - Combine with standard "Gift" prefix
 * 
 * 2. Gift Recording
 *    - Import User 00 (test account)
 *    - Navigate to home
 *    - Close onboarding dialog
 *    - Select recipient
 *    - Fill gift details
 *    - Sign and submit
 * 
 * 3. Verification
 *    - Check success notification
 *    - Refresh home view
 *    - Locate gift in list
 *    - Verify gift details
 *    - Check public server view
 * 
 * Test Data:
 * - Gift Title: "Gift [4-char-random]"
 * - Amount: Random 1-99
 * - Recipient: "Unnamed/Unknown"
 * 
 * Key Selectors:
 * - Gift title: '[data-testid="giftTitle"]'
 * - Amount input: 'input[type="number"]'
 * - Submit button: 'button[name="Sign & Send"]'
 * - Success alert: 'div[role="alert"]'
 * - Details section: 'h2[name="Details"]'
 * 
 * Alert Handling:
 * - Closes onboarding dialog
 * - Verifies success message
 * - Dismisses info alerts
 * 
 * State Requirements:
 * - Clean database state
 * - User 00 imported
 * - Available API rate limits
 * 
 * Related Files:
 * - Gift recording view: src/views/RecordGiftView.vue
 * - JWT creation: sw_scripts/safari-notifications.js
 * - Endorser API: src/libs/endorserServer.ts
 * 
 * @see Documentation in usage-guide.md for gift recording workflows
 * @requires @playwright/test
 * @requires ./testUtils - For user management utilities
 * 
 * @example Basic gift recording
 * ```typescript
 * await page.getByPlaceholder('What was given').fill('Gift abc123');
 * await page.getByRole('spinbutton').fill('42');
 * await page.getByRole('button', { name: 'Sign & Send' }).click();
 * await expect(page.getByText('That gift was recorded.')).toBeVisible();
 * ```
 */
import { test, expect } from '@playwright/test';
import { importUser } from './testUtils';

test('Record something given', async ({ page }) => {
  // Generate a random string of a few characters
  const randomString = Math.random().toString(36).substring(2, 6);

  // Generate a random non-zero single-digit number
  const randomNonZeroNumber = Math.floor(Math.random() * 99) + 1;

  // Standard title prefix
  const standardTitle = 'Gift ';

  // Combine title prefix with the random string
  const finalTitle = standardTitle + randomString;

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

  // Record something given
  await page.goto('./');
  await page.getByTestId('closeOnboardingAndFinish').click();
  await page.getByRole('heading', { name: 'Unnamed/Unknown' }).click();
  await page.getByPlaceholder('What was given').fill(finalTitle);
  await page.getByRole('spinbutton').fill(randomNonZeroNumber.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 page.locator('li').filter({ hasText: finalTitle }).locator('a').click();
  await expect(page.getByRole('heading', { name: 'Verifiable Claim Details' })).toBeVisible();
  await expect(page.getByText(finalTitle, { exact: true })).toBeVisible();
  const page1Promise = page.waitForEvent('popup');
  // expand the Details section to see the extended details
  await page.getByRole('heading', { name: 'Details', exact: true }).click();
  await page.getByRole('link', { name: 'View on the Public Server' }).click();
  const page1 = await page1Promise;
});