import { IDiscountCreateData } from '@/manager/modules/discount/types/discount-types';
import {
  expectDiscountBillingPlanTableToShowCorrectData,
  expectDiscountProductTableToShowCorrectData,
} from '@/manager/modules/discount/utils/discount-entity-expect-utils';
import { DiscountListPage } from '@/manager/modules/discount/views/DiscountListPage';
import { discountTableColumnTestIds } from '@/manager/modules/ui/data-table/data/data-table-column-test-ids';
import { expectDataTableTextColumnToHaveText } from '@/manager/modules/ui/data-table/utils/data-table-expect-utils';
import { expectSingleLocatorToHaveText } from '@/manager/shared/utils/expect-utils';
import { formatDiscount, formatDateTime, formatPercentage, formatCurrency } from '@/shared/utils/format-utils';
import { expect } from '@/shared/utils/matcher-utils';

export async function expectDiscountToBeCreated(startPage: DiscountListPage, data: IDiscountCreateData): Promise<void> {
  const createDialog = await startPage.openDiscountCreateDialog();
  const detailPage = await createDialog.create(data);

  const discountValue =
    data.type === 'percentage' ? formatPercentage(data.value) : formatCurrency(data.value, data.location.currency);
  const discountTerms = formatDiscount(data.duration, discountValue);
  const discountRedemptions = `0${data.usageLimit ? `/${data.usageLimit}` : ''}`;
  const discountExpirationDate = data.expirationDate ? formatDateTime(data.expirationDate) : undefined;

  await expectSingleLocatorToHaveText(detailPage.title, data.name);
  await expectSingleLocatorToHaveText(detailPage.generalCard.name, data.name);
  await expectSingleLocatorToHaveText(detailPage.generalCard.location, data.location.name);
  await expectSingleLocatorToHaveText(detailPage.generalCard.codes, data.code.value);
  await expectSingleLocatorToHaveText(detailPage.generalCard.terms, discountTerms);
  await expectSingleLocatorToHaveText(detailPage.generalCard.redemptions, discountRedemptions);
  await expectSingleLocatorToHaveText(detailPage.generalCard.expiryDate, discountExpirationDate);

  await expectDiscountProductTableToShowCorrectData(
    detailPage.productCard.List.dataTable,
    data.productsSelection.items,
    data.location.currency
  );

  await expectDiscountBillingPlanTableToShowCorrectData(
    detailPage.billingPlanCard.List.dataTable,
    data.billingPlansSelection.items,
    data.location.currency
  );

  const listPage = await detailPage.returnToDiscountListPage();
  await listPage.searchTextField.fill(data.name);
  await listPage.locationAutocomplete.fill(data.location.name);

  await expect(listPage.dataTable.getRows()).toHaveCount(1);
  await expectDataTableTextColumnToHaveText(listPage.dataTable, discountTableColumnTestIds.name, data.name);
  await expectDataTableTextColumnToHaveText(
    listPage.dataTable,
    discountTableColumnTestIds.location,
    data.location.name
  );
  await expectDataTableTextColumnToHaveText(listPage.dataTable, discountTableColumnTestIds.terms, discountTerms);
  await expectDataTableTextColumnToHaveText(listPage.dataTable, discountTableColumnTestIds.codes, data.code.value);
  await expectDataTableTextColumnToHaveText(
    listPage.dataTable,
    discountTableColumnTestIds.redemptions,
    discountRedemptions
  );
  await expectDataTableTextColumnToHaveText(
    listPage.dataTable,
    discountTableColumnTestIds.expiryDate,
    discountExpirationDate
  );
}

export async function expectDiscountToNotBeCreated(page: DiscountListPage, data: IDiscountCreateData): Promise<void> {
  const createDialog = await page.openDiscountCreateDialog();
  await createDialog.fill(data);

  if (await createDialog.submitButton.isEnabled()) await createDialog.submitButton.click();

  await expect(createDialog.errors).toHaveCountGreaterThan(0);
}
