import { expect, test } from '@playwright/test';
import mockGroupsAPI from './mocks/groups';
import mockGuestsAPI from './mocks/guests';

test('should allow CRUD on guests', async ({ page }) => {
  await mockGuestsAPI({ page });
  await mockGroupsAPI({ page });

  await page.goto('/default/dashboard/guests');

  await expect(page.getByRole('button', { name: 'Add new' })).toBeVisible();

  await expect(page.getByRole('tab', { name: 'Guests' })).toBeVisible();
  await expect(page.getByRole('tab', { name: 'Groups' })).toBeVisible();
  await expect(page.getByRole('tab', { name: 'Invitations' })).toBeVisible();

  // List all guests
  await expect(page.getByText('There are 2 elements in the list')).toBeVisible();
  await expect(page.getByRole('row')).toHaveCount(3); // 1 header row + 2 data rows

  await expect(page.getByRole('row').nth(1).getByRole('cell', { name: 'Kristofer Rohan DVM' })).toBeVisible();
  await expect(page.getByRole('row').nth(1).getByRole('cell', { name: "Pam's family" })).toBeVisible();
  await expect(page.getByRole('row').nth(1).getByRole('cell', { name: 'Tentative' })).toBeVisible();
  await expect(page.getByRole('row').nth(1).locator('svg')).toHaveCount(2);

  await expect(page.getByRole('row').nth(2).getByRole('cell', { name: 'Olevia Quigley Jr.' })).toBeVisible();
  await expect(page.getByRole('row').nth(2).getByRole('cell', { name: "Pam's work" })).toBeVisible();
  await expect(page.getByRole('row').nth(2).getByRole('cell', { name: 'Invited' })).toBeVisible();
  await expect(page.getByRole('row').nth(2).locator('svg')).toHaveCount(2);

  // Add a new guest
  await page.getByRole('button', { name: 'Add new' }).click();

  await page.getByRole('dialog').getByLabel('Name').fill('John Snow');

  await page.locator('#group').click();
  await page.getByRole('option', { name: "Pam's work" }).click();

  await page.locator('#status').click();
  await page.getByRole('option', { name: 'Invited' }).click();

  await page.getByRole('dialog').getByRole('button', { name: 'Create' }).click();

  await expect(page.getByText('There are 3 elements in the list')).toBeVisible();

  await expect(page.getByRole('row').nth(1).getByRole('cell', { name: 'John Snow' })).toBeVisible();
  await expect(page.getByRole('row').nth(1).getByRole('cell', { name: "Pam\'s work" })).toBeVisible();
  await expect(page.getByRole('row').nth(1).getByRole('cell', { name: 'Invited' })).toBeVisible();
  await expect(page.getByRole('row').nth(1).locator('svg')).toHaveCount(2);

  // Edit the just-added John Snow
  await page.getByRole('row').nth(1).locator('svg').nth(1).click(); // Click edit icon

  const dialog = page.getByRole('dialog');
  await expect(dialog.getByLabel('Name')).toHaveValue('John Snow');
  await dialog.getByLabel('Name').fill('John Fire');

  await dialog.locator('#group').click();
  await page.getByRole('option', { name: "Pam's family" }).click();

  await dialog.locator('#status').click();
  await page.getByRole('option', { name: 'Declined' }).click();

  await dialog.getByRole('button', { name: 'Update' }).click();

  await expect(page.getByRole('row').nth(1).getByRole('cell', { name: 'John Fire' })).toBeVisible();
  await expect(page.getByRole('row').nth(1).getByRole('cell', { name: 'Pam\'s Family' })).toBeVisible();
  await expect(page.getByRole('row').nth(1).getByRole('cell', { name: 'Declined' })).toBeVisible();

  await expect(page.getByText('There are 3 elements in the list')).toBeVisible();

  // Delete John Fire
  page.on('dialog', dialog => dialog.accept());
  await page.getByRole('row').nth(1).locator('svg').nth(0).click(); // Click delete icon
  await expect(page.getByText('There are 2 elements in the list')).toBeVisible();
});
