import { test, expect } from '@playwright/test';
import { deleteContact, generateNewEthrUser, generateRandomString, importUser, switchToUser } from './testUtils';

test('Check User 0 can invite someone', async ({ page }) => {
  const newDid = await generateNewEthrUser(page);

  await importUser(page, '00');
  await page.goto('./invite-one');
  await page.locator('button > svg.fa-plus').click();
  const neighborNum = await generateRandomString(5);
  await page.getByPlaceholder('Notes', { exact: true }).fill(`Neighbor ${neighborNum}`);
  // get the expiration date input and set to 14 days from now
  const expirationDate = new Date(Date.now() + 14 * 24 * 60 * 60 * 1000);
  await page.locator('input[type="date"]').fill(expirationDate.toISOString().split('T')[0]);
  await page.locator('button:has-text("Save")').click();
  await expect(page.locator('div[role="alert"] button:has-text("Yes")')).toBeHidden();

  // check that the invite is in the list
  const newInviteLine = await page.locator(`td:has-text("Neighbor ${neighborNum}")`);
  await expect(newInviteLine).toBeVisible();
  // retrieve the link from the title
  const inviteLink = await newInviteLine.getAttribute('data-testId');
  await expect(inviteLink).not.toBeNull();

  // become the new user and accept the invite
  await switchToUser(page, newDid);
  await page.goto(inviteLink as string);
  await page.getByPlaceholder('Name', { exact: true }).fill(`My pal User #0`);
  await page.locator('button:has-text("Save")').click();
  await expect(page.locator('button:has-text("Save")')).toBeHidden();
  await expect(page.locator(`li:has-text("My pal User #0")`)).toBeVisible();
});
