import {
  getProductOneTimeCreateData,
  getProductRecurringCreateData,
} from '@/manager/modules/product/product-factories';
import { test } from '@/manager/modules/product/product-fixtures';
import { expectSingleLocatorToHaveText } from '@/manager/shared/utils/expect-utils';
import { formatId } from '@/shared/utils/formatters';
import { expect } from '@/shared/utils/matchers';

test('navigates from the list to the one-time product details page', async ({
  createProductOneTime,
  productListPage,
}) => {
  const product = await createProductOneTime(getProductOneTimeCreateData());

  await productListPage.goto();

  const detailsPage = await test.step('navigate to details page', async () => {
    await productListPage.searchTextField.fill(formatId(product.id));
    return productListPage.goToProductOneTimeDetailsPage(0);
  });

  await test.step('verify one-time product details page', async () => {
    await expectSingleLocatorToHaveText(detailsPage.title, product.name);
  });
});

test('navigates from the list to the recurring product details page', async ({
  createProductRecurring,
  productListPage,
}) => {
  const product = await createProductRecurring(getProductRecurringCreateData());

  await productListPage.goto();

  const detailsPage = await test.step('navigate to details page', async () => {
    await productListPage.searchTextField.fill(formatId(product.id));
    return productListPage.goToProductRecurringDetailsPage(0);
  });

  await test.step('verify recurring product details page', async () => {
    await expectSingleLocatorToHaveText(detailsPage.title, product.name);
  });
});

test('navigates from the list to the one-time product create dialog', async ({ productListPage }) => {
  await productListPage.goto();

  const createDialog = await test.step('open create dialog', () => productListPage.openProductOneTimeCreateDialog());

  await test.step('verify one-time product create dialog is open', async () => {
    await expect(createDialog.main).toBeVisible();
  });
});

test('navigates from the list to the recurring product create dialog', async ({ productListPage }) => {
  await productListPage.goto();

  const createDialog = await test.step('open create dialog', () => productListPage.openProductRecurringCreateDialog());

  await test.step('verify recurring product create dialog is open', async () => {
    await expect(createDialog.main).toBeVisible();
  });
});
