import {
  verifyBookingDetailsItemsSection,
  verifyBookingInvoice,
  verifyBookingInvoiceCount,
  verifyBookingDepositInvoiceLink,
  verifyBookingPayment,
  verifyBookingPaymentCount,
} from '@/manager/modules/booking/booking-assertions';
import { getBookingCreateData } from '@/manager/modules/booking/booking-factories';
import { test } from '@/manager/modules/booking/booking-fixtures';
import { toBookingCreateTotalsSummary, toBookingItemsDetails } from '@/manager/modules/booking/booking-mappers';
import { BookingCreateData } from '@/manager/modules/booking/booking-types';
import {
  getBookingStatusOnMoveIn,
  getBookingDepositInvoiceNumber,
  getBookingNonDepositInvoiceNumber,
} from '@/manager/modules/booking/booking-utils';
import { BookingCreateDialog } from '@/manager/modules/booking/views/BookingCreateDialog';
import { BookingDetailsPage } from '@/manager/modules/booking/views/BookingDetailsPage';
import {
  bookingCreateGeneralHappyTestCases,
  bookingCreateInvoiceTotalHappyTestCases,
} from '@/manager/modules/booking/test-cases/booking-create-happy-test-cases';
import { getCustomerCreateData } from '@/manager/modules/customer/customer-factories';
import { getCustomerDisplayName } from '@/manager/modules/customer/customer-utils';
import { getDepositCreateData } from '@/manager/modules/deposit/deposit-factories';
import {
  expectDataTableChipsColumnToHaveText,
  expectDataTableTextColumnToHaveText,
} from '@/manager/modules/ui/data-table/data-table-assertions';
import {
  bookingTableColumnTestIds,
  invoiceTableColumnTestIds,
} from '@/manager/modules/ui/data-table/data/data-table-column-test-ids';
import { verifyTotalsSummarySection } from '@/manager/modules/ui/totals-summary/totals-summary-assertions';
import { getUnitCreateData } from '@/manager/modules/unit/unit-factories';
import { getUnitTypeCreateData } from '@/manager/modules/unit-type/unit-type-factories';
import { expectSingleLocatorToHaveText } from '@/manager/shared/utils/expect-utils';
import {
  getDefaultBacsPayment,
  getDefaultMastercardCardPayment,
  getDefaultSepaPayment,
  getDefaultVisaCardPayment,
  getPendingBacsPayment,
  getPendingSepaPayment,
} from '@/shared/modules/payment-method/payment-method-factories';
import { locations } from '@/shared/data/seed-locations';
import { getFutureDate } from '@/shared/utils/date-utils';
import {
  formatBillingPeriod,
  formatCurrency,
  formatDate,
  formatDateRange,
  formatId,
  formatPaymentMethod,
} from '@/shared/utils/formatters';
import { expect } from '@/shared/utils/matchers';

// Each case stands up its dependencies via API (unit-type and insurance both Stripe-sync) before
// driving the create dialog, so the default per-test timeout is too tight — allow extra headroom.
test.describe.configure({ mode: 'default', timeout: 120_000 });

// Pins the dialog totals preview against the same model used for the issued amount before submitting, so
// a later invoice-amount failure isolates a backend divergence (preview right, invoice wrong) from a
// pricing-model bug (both wrong). Not for bookings with a deposit — the totals summary doesn't model it.
async function createBookingAfterVerifyingDialogTotals(
  dialog: BookingCreateDialog,
  data: BookingCreateData
): Promise<BookingDetailsPage> {
  await dialog.goto();
  await test.step('fill booking create form', () => dialog.fill(data));
  await test.step('verify totals summary in dialog', () =>
    verifyTotalsSummarySection(
      dialog.productsFieldSet.totalsSummarySection,
      toBookingCreateTotalsSummary(data),
      data.location.currency
    ));
  return test.step('create booking', () => dialog.createAfterFill());
}

for (const tc of bookingCreateGeneralHappyTestCases) {
  test(tc.description, async ({ setupBookingCreateData, bookingCreateDialog, trackBookingForTeardown }) => {
    const data = await setupBookingCreateData(tc.setup, tc.options);

    const currency = data.location.currency;
    const itemsDetails = toBookingItemsDetails(data);
    const createdAt = formatDate(new Date(), data.location.timezone);
    const moveInDate = formatDate(data.moveInDate);
    const status = getBookingStatusOnMoveIn(data.moveInDate, data.location.timezone);
    const plan = formatBillingPeriod(data.unitType.plan.period, data.unitType.plan.amount, data.unitType.plan.name);
    const currentPeriodEndDate =
      status === 'active'
        ? getFutureDate(data.unitType.plan.amount, data.unitType.plan.period === 'monthly' ? 'month' : 'week')
        : undefined;
    const currentPeriod = currentPeriodEndDate ? formatDateRange(data.moveInDate, currentPeriodEndDate) : undefined;
    const nextInvoiceDate = formatDate(currentPeriodEndDate ?? data.moveInDate);
    const paymentMethod =
      data.payment.option === 'charge_automatically' && data.payment.method
        ? formatPaymentMethod(data.payment.method, true)
        : undefined;
    const paymentOption =
      data.payment.option === 'send_invoice' ? 'Manual payment' : 'Automatic payment through Stripe';
    const customerName = getCustomerDisplayName(data.customer);

    const detailsPage = await createBookingAfterVerifyingDialogTotals(bookingCreateDialog, data);

    const bookingId = detailsPage.getBookingId();
    trackBookingForTeardown(bookingId);

    await test.step('verify new booking on details page', async () => {
      await expect(detailsPage.title).not.toBeEmpty();
      await expectSingleLocatorToHaveText(detailsPage.title, `Booking ${formatId(bookingId)}`);
      await expectSingleLocatorToHaveText(detailsPage.baseStatus, status);
      await expectSingleLocatorToHaveText(detailsPage.generalCard.customer, customerName);
      await expectSingleLocatorToHaveText(detailsPage.generalCard.location, data.location.name);
      await expectSingleLocatorToHaveText(detailsPage.generalCard.createdAt, createdAt);
      await expectSingleLocatorToHaveText(detailsPage.generalCard.moveIn, moveInDate);
      await expectSingleLocatorToHaveText(detailsPage.generalCard.moveOut, undefined);
      await expectSingleLocatorToHaveText(detailsPage.generalCard.discount, data.discount?.name);
      await expectSingleLocatorToHaveText(detailsPage.notesCard.note, data.note);
      await expectSingleLocatorToHaveText(detailsPage.billingCard.bookingPlan, plan);
      await expectSingleLocatorToHaveText(detailsPage.billingCard.currentPeriod, currentPeriod);
      await expectSingleLocatorToHaveText(
        detailsPage.billingCard.amountExclVat,
        formatCurrency(itemsDetails.totals.exclVat, currency)
      );
      await expectSingleLocatorToHaveText(detailsPage.billingCard.priceAdjustment, undefined);
      await expectSingleLocatorToHaveText(detailsPage.billingCard.nextInvoice, nextInvoiceDate);

      if (paymentMethod) {
        await expectSingleLocatorToHaveText(detailsPage.billingCard.paymentMethod, paymentMethod);
      } else {
        await expect(detailsPage.billingCard.paymentMethod).toHaveCount(0);
      }

      await expectSingleLocatorToHaveText(detailsPage.billingCard.paymentOption, paymentOption);

      await verifyBookingDetailsItemsSection(detailsPage.itemsCard.itemsSection, itemsDetails, currency);
    });

    await test.step('verify new booking on list page', async () => {
      const listPage = await detailsPage.returnToBookingListPage();

      await listPage.searchTextField.fill(bookingId.toString());

      await expect(listPage.dataTable.getRows()).toHaveCount(1);
      await expectDataTableTextColumnToHaveText(listPage.dataTable, bookingTableColumnTestIds.id, formatId(bookingId));
      await expectDataTableTextColumnToHaveText(
        listPage.dataTable,
        bookingTableColumnTestIds.location,
        data.location.name
      );
      await expectDataTableChipsColumnToHaveText(listPage.dataTable, bookingTableColumnTestIds.status, status);
      await expectDataTableTextColumnToHaveText(listPage.dataTable, bookingTableColumnTestIds.customer, customerName);
      await expectDataTableTextColumnToHaveText(
        listPage.dataTable,
        bookingTableColumnTestIds.unitType,
        data.unitType.unitType.name
      );
      await expectDataTableTextColumnToHaveText(listPage.dataTable, bookingTableColumnTestIds.unit, data.unit.name);
      await expectDataTableTextColumnToHaveText(listPage.dataTable, bookingTableColumnTestIds.billingPeriod, plan);
      await expectDataTableTextColumnToHaveText(
        listPage.dataTable,
        bookingTableColumnTestIds.nextInvoice,
        nextInvoiceDate
      );
      await expectDataTableTextColumnToHaveText(listPage.dataTable, bookingTableColumnTestIds.createdAt, createdAt);
      await expectDataTableTextColumnToHaveText(listPage.dataTable, bookingTableColumnTestIds.moveIn, moveInDate);
      await expectDataTableTextColumnToHaveText(listPage.dataTable, bookingTableColumnTestIds.moveOut, undefined);
    });
  });
}

// FIXME: quarantined — these assert invoices/payments the backend issues asynchronously via Stripe
// webhooks, and the test-env queue worker pool can't drain the reseed backlog and issue within the
// 60s assertion window, so they fail on job throughput rather than a product or test defect. Re-enable
// once the test-env queue worker throughput is raised.
for (const tc of bookingCreateInvoiceTotalHappyTestCases) {
  test.fixme(tc.description, async ({ setupBookingCreateData, bookingCreateDialog, trackBookingForTeardown }) => {
    const data = await setupBookingCreateData(tc.setup, tc.options);

    // A non-upfront deposit folds into the first invoice as a one-time line, but the totals summary
    // doesn't model deposits yet (see the toBookingCreateTotalsSummary TODO), so add it here; the
    // default deposit is 0% tax, so its incl-VAT equals its exclusive price.
    const depositInTotal = data.deposit && !data.deposit.chargeUpfront ? data.deposit.deposit.priceExclVat : 0;
    const total = formatCurrency(
      toBookingCreateTotalsSummary(data).totalInclVat + depositInTotal,
      data.location.currency
    );

    // The dialog totals preview models every line except the deposit, so pin it against the issued
    // amount only when there's no deposit; deposit cases just create and check the issued invoice.
    let detailsPage: BookingDetailsPage;

    if (data.deposit) {
      await bookingCreateDialog.goto();
      detailsPage = await test.step('create booking', () => bookingCreateDialog.create(data));
    } else {
      detailsPage = await createBookingAfterVerifyingDialogTotals(bookingCreateDialog, data);
    }

    trackBookingForTeardown(detailsPage.getBookingId());

    await test.step('verify the invoice total', async () => {
      await verifyBookingInvoiceCount(detailsPage, 1);
      await expectDataTableTextColumnToHaveText(
        detailsPage.invoicesCard.dataTable,
        invoiceTableColumnTestIds.amount,
        total
      );
    });

    if (data.deposit) {
      // Reloads, so run last: the deposit field links its amount to the first invoice it folded into.
      const depositAmount = formatCurrency(data.deposit.deposit.priceExclVat, data.location.currency);
      await test.step('verify deposit on details page', () =>
        verifyBookingDepositInvoiceLink(detailsPage, depositAmount));
    }
  });
}

test('creates a booking for a customer having multiple locations', async ({
  createCustomer,
  createUnitType,
  createUnit,
  bookingCreateDialog,
  trackBookingForTeardown,
}) => {
  const customer = await createCustomer(
    getCustomerCreateData({ locations: [locations.viennaSouth, locations.viennaNorth] })
  );
  const unitType = await createUnitType(getUnitTypeCreateData({ location: locations.viennaSouth }));
  const unit = await createUnit(getUnitCreateData(unitType));

  const data = getBookingCreateData({ customer, unitType, unit });

  await bookingCreateDialog.goto();

  const detailsPage = await test.step('create booking', () => bookingCreateDialog.create(data));

  trackBookingForTeardown(detailsPage.getBookingId());

  await test.step('verify new booking on details page', async () => {
    await expectSingleLocatorToHaveText(detailsPage.generalCard.customer, getCustomerDisplayName(data.customer));
    await expectSingleLocatorToHaveText(detailsPage.generalCard.location, data.location.name);
  });
});

// TODO: a backdated move-in prorates the first-invoice amount, the current period, and the next-invoice
// date from the backdated start rather than today — so this only asserts the move-in date renders.
// Asserting the prorated amounts/dates needs the backend's proration rules verified against the SPA
// first; tracked as a follow-up.
test('creates a backdated booking', async ({
  createCustomer,
  createUnitType,
  createUnit,
  bookingCreateDialog,
  trackBookingForTeardown,
}) => {
  const customer = await createCustomer(getCustomerCreateData());
  const unitType = await createUnitType(getUnitTypeCreateData());
  const unit = await createUnit(getUnitCreateData(unitType));
  const data = getBookingCreateData({ customer, unitType, unit }, { moveInDaysInTheFuture: -10 });
  const moveInDate = formatDate(data.moveInDate);

  await bookingCreateDialog.goto();

  const detailsPage = await test.step('create booking', () => bookingCreateDialog.create(data));

  trackBookingForTeardown(detailsPage.getBookingId());

  await test.step('verify new booking on details page', async () => {
    await expectSingleLocatorToHaveText(detailsPage.generalCard.customer, getCustomerDisplayName(data.customer));
    await expectSingleLocatorToHaveText(detailsPage.generalCard.moveIn, moveInDate);
  });
});

test('creates a booking using the most recently added payment method', async ({
  createCustomer,
  createUnitType,
  createUnit,
  bookingCreateDialog,
  trackBookingForTeardown,
}) => {
  const customer = await createCustomer(getCustomerCreateData());
  const unitType = await createUnitType(getUnitTypeCreateData());
  const unit = await createUnit(getUnitCreateData(unitType));
  // the first method goes in via the normal fill (added and selected); the second is added afterwards
  const data = getBookingCreateData(
    { customer, unitType, unit },
    { paymentOption: 'charge_automatically', paymentMethod: getDefaultVisaCardPayment() }
  );
  const lastAddedMethod = getDefaultMastercardCardPayment();
  const lastAddedLabel = formatPaymentMethod(lastAddedMethod, true);

  await bookingCreateDialog.goto();

  await test.step('add a second payment method after the first', async () => {
    await bookingCreateDialog.fill(data);
    await bookingCreateDialog.paymentFieldSet.radioGroup.paymentMethodAddForm.add(lastAddedMethod);
  });

  const detailsPage = await test.step('create booking', () => bookingCreateDialog.createAfterFill());

  trackBookingForTeardown(detailsPage.getBookingId());

  await test.step('verify the booking uses the most recently added method', async () => {
    // the second method is never clicked — the dialog auto-selects the most recently added one, so the
    // booking carrying it confirms both that it was auto-selected and that it was the method charged
    await expectSingleLocatorToHaveText(detailsPage.billingCard.paymentMethod, lastAddedLabel);
    await expectSingleLocatorToHaveText(detailsPage.billingCard.paymentOption, 'Automatic payment through Stripe');
  });
});

// FIXME: same async-issuance / queue-throughput quarantine as the invoice-total loop above.
test.fixme('issues an open invoice for an auto-charged-after-first-invoice active booking', async ({
  createCustomer,
  createUnitType,
  createUnit,
  bookingCreateDialog,
  trackBookingForTeardown,
}) => {
  const customer = await createCustomer(getCustomerCreateData());
  const unitType = await createUnitType(getUnitTypeCreateData());
  const unit = await createUnit(getUnitCreateData(unitType));
  const data = getBookingCreateData({ customer, unitType, unit });
  const timezone = data.location.timezone;
  const total = formatCurrency(toBookingCreateTotalsSummary(data).totalInclVat, data.location.currency);
  const today = formatDate(new Date(), timezone);

  await bookingCreateDialog.goto();

  const detailsPage = await test.step('create booking', () => bookingCreateDialog.create(data));

  trackBookingForTeardown(detailsPage.getBookingId());

  await test.step('verify booking invoices', async () => {
    await verifyBookingInvoiceCount(detailsPage, 1);

    // charging only after the first invoice leaves that first invoice open and manual, with the full amount due
    await verifyBookingInvoice(detailsPage, {
      status: 'Open',
      paymentOption: 'Manual',
      unit: data.unit.name,
      amount: total,
      amountDue: total,
      issueDate: today,
      dueDate: formatDate(getFutureDate(data.location.invoiceDaysUntilDue, 'day'), timezone),
    });
  });

  await test.step('verify booking payments', async () => {
    await verifyBookingPaymentCount(detailsPage, 1);

    // the manual first invoice has a single unpaid payment for the full amount
    await verifyBookingPayment(detailsPage, { amount: total, status: 'Not paid yet', date: today });
  });

  await test.step('verify billing payment option', async () => {
    // the booking still auto-charges through Stripe, just starting from the second invoice; no method is held yet
    await expectSingleLocatorToHaveText(detailsPage.billingCard.paymentOption, 'Automatic payment through Stripe');
    await expect(detailsPage.billingCard.paymentMethod).toHaveCount(0);
  });
});

test.fixme('issues an open invoice for a send-invoice active booking', async ({
  createCustomer,
  createUnitType,
  createUnit,
  bookingCreateDialog,
  trackBookingForTeardown,
}) => {
  const customer = await createCustomer(getCustomerCreateData());
  const unitType = await createUnitType(getUnitTypeCreateData());
  const unit = await createUnit(getUnitCreateData(unitType));
  const data = getBookingCreateData({ customer, unitType, unit }, { paymentOption: 'send_invoice' });
  const timezone = data.location.timezone;
  const total = formatCurrency(toBookingCreateTotalsSummary(data).totalInclVat, data.location.currency);
  const today = formatDate(new Date(), timezone);

  await bookingCreateDialog.goto();

  const detailsPage = await test.step('create booking', () => bookingCreateDialog.create(data));

  trackBookingForTeardown(detailsPage.getBookingId());

  await test.step('verify booking invoices', async () => {
    await verifyBookingInvoiceCount(detailsPage, 1);

    // a manually-paid booking issues an open invoice the customer settles via payment link, with the full amount due
    await verifyBookingInvoice(detailsPage, {
      status: 'Open',
      paymentOption: 'Manual',
      unit: data.unit.name,
      amount: total,
      amountDue: total,
      issueDate: today,
      dueDate: formatDate(getFutureDate(data.location.invoiceDaysUntilDue, 'day'), timezone),
    });
  });

  await test.step('verify booking payments', async () => {
    await verifyBookingPaymentCount(detailsPage, 1);

    await verifyBookingPayment(detailsPage, { amount: total, status: 'Not paid yet', date: today });
  });

  await test.step('verify billing payment option', async () => {
    // the manual payment option is the only thing distinguishing this from an after-first-invoice booking at creation
    await expectSingleLocatorToHaveText(detailsPage.billingCard.paymentOption, 'Manual payment');
    await expect(detailsPage.billingCard.paymentMethod).toHaveCount(0);
  });
});

test.fixme('issues a paid invoice for a card auto-charged active booking', async ({
  createCustomer,
  createUnitType,
  createUnit,
  bookingCreateDialog,
  trackBookingForTeardown,
}) => {
  const customer = await createCustomer(getCustomerCreateData());
  const unitType = await createUnitType(getUnitTypeCreateData());
  const unit = await createUnit(getUnitCreateData(unitType));
  const data = getBookingCreateData(
    { customer, unitType, unit },
    { paymentOption: 'charge_automatically', paymentMethod: getDefaultVisaCardPayment() }
  );
  const timezone = data.location.timezone;
  const total = formatCurrency(toBookingCreateTotalsSummary(data).totalInclVat, data.location.currency);
  const today = formatDate(new Date(), timezone);
  const paymentMethod = data.payment.method ? formatPaymentMethod(data.payment.method, true) : undefined;

  await bookingCreateDialog.goto();

  const detailsPage = await test.step('create booking', () => bookingCreateDialog.create(data));

  trackBookingForTeardown(detailsPage.getBookingId());

  await test.step('verify booking invoices', async () => {
    await verifyBookingInvoiceCount(detailsPage, 1);

    // a card auto-charge settles synchronously, so the first invoice is paid with nothing left due
    await verifyBookingInvoice(detailsPage, {
      status: 'Paid',
      paymentOption: 'Auto charge',
      unit: data.unit.name,
      amount: total,
      amountDue: formatCurrency(0, data.location.currency),
      issueDate: today,
      dueDate: formatDate(getFutureDate(data.location.invoiceDaysUntilDue, 'day'), timezone),
    });
  });

  await test.step('verify booking payments', async () => {
    await verifyBookingPaymentCount(detailsPage, 1);

    await verifyBookingPayment(detailsPage, { amount: total, status: 'Succeeded', paymentMethod, date: today });
  });

  await test.step('verify billing payment method', async () => {
    await expectSingleLocatorToHaveText(detailsPage.billingCard.paymentOption, 'Automatic payment through Stripe');
    await expectSingleLocatorToHaveText(detailsPage.billingCard.paymentMethod, paymentMethod);
  });
});

test.fixme('issues a paid invoice for a sepa auto-charged active booking', async ({
  createCustomer,
  createUnitType,
  createUnit,
  bookingCreateDialog,
  trackBookingForTeardown,
}) => {
  // a sepa direct-debit charge can settle a little after creation, so give the settlement extra headroom
  test.setTimeout(180_000);

  const customer = await createCustomer(getCustomerCreateData());
  const unitType = await createUnitType(getUnitTypeCreateData());
  const unit = await createUnit(getUnitCreateData(unitType));
  const data = getBookingCreateData(
    { customer, unitType, unit },
    { paymentOption: 'charge_automatically', paymentMethod: getDefaultSepaPayment() }
  );
  const timezone = data.location.timezone;
  const total = formatCurrency(toBookingCreateTotalsSummary(data).totalInclVat, data.location.currency);
  const today = formatDate(new Date(), timezone);
  const paymentMethod = data.payment.method ? formatPaymentMethod(data.payment.method, true) : undefined;

  await bookingCreateDialog.goto();

  const detailsPage = await test.step('create booking', () => bookingCreateDialog.create(data));

  trackBookingForTeardown(detailsPage.getBookingId());

  await test.step('wait for the sepa charge to settle', async () => {
    await verifyBookingInvoiceCount(detailsPage, 1);

    // a sepa direct-debit charge can take a moment to settle and the invoices card doesn't reflect the
    // settlement on its own, so reload-poll until the first invoice flips from pending to paid.
    await expect(async () => {
      await detailsPage.reload();
      await expectSingleLocatorToHaveText(
        detailsPage.invoicesCard.dataTable.getRowColumnByIndex(0, invoiceTableColumnTestIds.status),
        'Paid'
      );
    }).toPass({ timeout: 120_000 });
  });

  await test.step('verify booking invoices', async () => {
    // once the charge settles the first invoice is paid, with nothing left due
    await verifyBookingInvoice(detailsPage, {
      status: 'Paid',
      paymentOption: 'Auto charge',
      unit: data.unit.name,
      amount: total,
      amountDue: formatCurrency(0, data.location.currency),
      issueDate: today,
      dueDate: formatDate(getFutureDate(data.location.invoiceDaysUntilDue, 'day'), timezone),
    });
  });

  await test.step('verify booking payments', async () => {
    await verifyBookingPaymentCount(detailsPage, 1);

    await verifyBookingPayment(detailsPage, { amount: total, status: 'Succeeded', paymentMethod, date: today });
  });

  await test.step('verify billing payment method', async () => {
    await expectSingleLocatorToHaveText(detailsPage.billingCard.paymentOption, 'Automatic payment through Stripe');
    await expectSingleLocatorToHaveText(detailsPage.billingCard.paymentMethod, paymentMethod);
  });
});

test.fixme('issues a paid invoice for a bacs auto-charged active booking', async ({
  createCustomer,
  createUnitType,
  createUnit,
  bookingCreateDialog,
  trackBookingForTeardown,
}) => {
  // bacs direct debit settles slower than a card or sepa charge, so give the settlement extra headroom
  test.setTimeout(180_000);

  const customer = await createCustomer(getCustomerCreateData({ locations: [locations.selfstorageUk] }));
  const unitType = await createUnitType(getUnitTypeCreateData({ location: locations.selfstorageUk }));
  const unit = await createUnit(getUnitCreateData(unitType));
  const data = getBookingCreateData(
    { customer, unitType, unit },
    { paymentOption: 'charge_automatically', paymentMethod: getDefaultBacsPayment() }
  );
  const timezone = data.location.timezone;
  const total = formatCurrency(toBookingCreateTotalsSummary(data).totalInclVat, data.location.currency);
  const today = formatDate(new Date(), timezone);
  const paymentMethod = data.payment.method ? formatPaymentMethod(data.payment.method, true) : undefined;

  await bookingCreateDialog.goto();

  const detailsPage = await test.step('create booking', () => bookingCreateDialog.create(data));

  trackBookingForTeardown(detailsPage.getBookingId());

  await test.step('wait for the bacs charge to settle', async () => {
    await verifyBookingInvoiceCount(detailsPage, 1);

    // bacs direct debit settles asynchronously and the invoices card doesn't reflect the settlement on its
    // own, so reload-poll until the first invoice flips from pending to paid.
    await expect(async () => {
      await detailsPage.reload();
      await expectSingleLocatorToHaveText(
        detailsPage.invoicesCard.dataTable.getRowColumnByIndex(0, invoiceTableColumnTestIds.status),
        'Paid'
      );
    }).toPass({ timeout: 120_000 });
  });

  await test.step('verify booking invoices', async () => {
    // once the charge settles the first invoice is paid, with nothing left due
    await verifyBookingInvoice(detailsPage, {
      status: 'Paid',
      paymentOption: 'Auto charge',
      unit: data.unit.name,
      amount: total,
      amountDue: formatCurrency(0, data.location.currency),
      issueDate: today,
      dueDate: formatDate(getFutureDate(data.location.invoiceDaysUntilDue, 'day'), timezone),
    });
  });

  await test.step('verify booking payments', async () => {
    await verifyBookingPaymentCount(detailsPage, 1);

    await verifyBookingPayment(detailsPage, { amount: total, status: 'Succeeded', paymentMethod, date: today });
  });

  await test.step('verify billing payment method', async () => {
    await expectSingleLocatorToHaveText(detailsPage.billingCard.paymentOption, 'Automatic payment through Stripe');
    await expectSingleLocatorToHaveText(detailsPage.billingCard.paymentMethod, paymentMethod);
  });
});

test.fixme('issues a pending invoice for a sepa auto-charged active booking', async ({
  createCustomer,
  createUnitType,
  createUnit,
  bookingCreateDialog,
  trackBookingForTeardown,
}) => {
  const customer = await createCustomer(getCustomerCreateData());
  const unitType = await createUnitType(getUnitTypeCreateData());
  const unit = await createUnit(getUnitCreateData(unitType));
  const data = getBookingCreateData(
    { customer, unitType, unit },
    { paymentOption: 'charge_automatically', paymentMethod: getPendingSepaPayment() }
  );
  const timezone = data.location.timezone;
  const total = formatCurrency(toBookingCreateTotalsSummary(data).totalInclVat, data.location.currency);
  const today = formatDate(new Date(), timezone);
  const paymentMethod = data.payment.method ? formatPaymentMethod(data.payment.method, true) : undefined;

  await bookingCreateDialog.goto();

  const detailsPage = await test.step('create booking', () => bookingCreateDialog.create(data));

  trackBookingForTeardown(detailsPage.getBookingId());

  await test.step('verify booking invoices', async () => {
    await verifyBookingInvoiceCount(detailsPage, 1);

    // this sepa mandate never settles, so the first invoice stays pending with the full amount due
    await verifyBookingInvoice(detailsPage, {
      status: 'Pending payment',
      paymentOption: 'Auto charge',
      unit: data.unit.name,
      amount: total,
      amountDue: total,
      issueDate: today,
      dueDate: formatDate(getFutureDate(data.location.invoiceDaysUntilDue, 'day'), timezone),
    });
  });

  await test.step('verify booking payments', async () => {
    await verifyBookingPaymentCount(detailsPage, 1);

    await verifyBookingPayment(detailsPage, { amount: total, status: 'Pending', paymentMethod, date: today });
  });

  await test.step('verify billing payment method', async () => {
    await expectSingleLocatorToHaveText(detailsPage.billingCard.paymentOption, 'Automatic payment through Stripe');
    await expectSingleLocatorToHaveText(detailsPage.billingCard.paymentMethod, paymentMethod);
  });
});

test.fixme('issues a pending invoice for a bacs auto-charged active booking', async ({
  createCustomer,
  createUnitType,
  createUnit,
  bookingCreateDialog,
  trackBookingForTeardown,
}) => {
  const customer = await createCustomer(getCustomerCreateData({ locations: [locations.selfstorageUk] }));
  const unitType = await createUnitType(getUnitTypeCreateData({ location: locations.selfstorageUk }));
  const unit = await createUnit(getUnitCreateData(unitType));
  const data = getBookingCreateData(
    { customer, unitType, unit },
    { paymentOption: 'charge_automatically', paymentMethod: getPendingBacsPayment() }
  );
  const timezone = data.location.timezone;
  const total = formatCurrency(toBookingCreateTotalsSummary(data).totalInclVat, data.location.currency);
  const today = formatDate(new Date(), timezone);
  const paymentMethod = data.payment.method ? formatPaymentMethod(data.payment.method, true) : undefined;

  await bookingCreateDialog.goto();

  const detailsPage = await test.step('create booking', () => bookingCreateDialog.create(data));

  trackBookingForTeardown(detailsPage.getBookingId());

  await test.step('verify booking invoices', async () => {
    await verifyBookingInvoiceCount(detailsPage, 1);

    // this bacs mandate never settles, so the first invoice stays pending with the full amount due
    await verifyBookingInvoice(detailsPage, {
      status: 'Pending payment',
      paymentOption: 'Auto charge',
      unit: data.unit.name,
      amount: total,
      amountDue: total,
      issueDate: today,
      dueDate: formatDate(getFutureDate(data.location.invoiceDaysUntilDue, 'day'), timezone),
    });
  });

  await test.step('verify booking payments', async () => {
    await verifyBookingPaymentCount(detailsPage, 1);

    await verifyBookingPayment(detailsPage, { amount: total, status: 'Pending', paymentMethod, date: today });
  });

  await test.step('verify billing payment method', async () => {
    await expectSingleLocatorToHaveText(detailsPage.billingCard.paymentOption, 'Automatic payment through Stripe');
    await expectSingleLocatorToHaveText(detailsPage.billingCard.paymentMethod, paymentMethod);
  });
});

test.fixme('issues no invoice for a scheduled booking', async ({
  createCustomer,
  createUnitType,
  createUnit,
  bookingCreateDialog,
  trackBookingForTeardown,
}) => {
  const customer = await createCustomer(getCustomerCreateData());
  const unitType = await createUnitType(getUnitTypeCreateData());
  const unit = await createUnit(getUnitCreateData(unitType));
  const data = getBookingCreateData({ customer, unitType, unit }, { moveInDaysInTheFuture: 10 });

  await bookingCreateDialog.goto();

  const detailsPage = await test.step('create booking', () => bookingCreateDialog.create(data));

  trackBookingForTeardown(detailsPage.getBookingId());

  await test.step('verify booking invoices', async () => {
    await verifyBookingInvoiceCount(detailsPage, 0);
  });
});

test.fixme('issues a separate invoice for an upfront deposit', async ({
  createCustomer,
  createUnitType,
  createUnit,
  createDeposit,
  bookingCreateDialog,
  trackBookingForTeardown,
}) => {
  const customer = await createCustomer(getCustomerCreateData());
  const deposit = await createDeposit(getDepositCreateData());
  const unitType = await createUnitType(getUnitTypeCreateData({ deposit }));
  const unit = await createUnit(getUnitCreateData(unitType));
  const data = getBookingCreateData({ customer, unitType, unit, deposit }, { chargeDepositUpfront: true });
  const depositAmount = formatCurrency(deposit.priceExclVat, data.location.currency);
  const firstInvoiceAmount = formatCurrency(toBookingCreateTotalsSummary(data).totalInclVat, data.location.currency);
  const today = formatDate(new Date(), data.location.timezone);
  // status, payment option, and dates match across both invoices; amount and amount due differ (they
  // equal each other per invoice, since neither is paid). The unit differs too: only the first-period
  // invoice is tied to a unit — the upfront-deposit invoice has none.
  const invoiceBase = {
    status: 'Open',
    paymentOption: 'Manual',
    issueDate: today,
    dueDate: formatDate(getFutureDate(data.location.invoiceDaysUntilDue, 'day'), data.location.timezone),
  };

  await bookingCreateDialog.goto();

  const detailsPage = await test.step('create booking', () => bookingCreateDialog.create(data));

  trackBookingForTeardown(detailsPage.getBookingId());

  await test.step('verify booking invoices', async () => {
    // active first-period invoice (the booking total) + the upfront deposit invoice
    await verifyBookingInvoiceCount(detailsPage, 2);
  });

  // Identify both invoices without using their amounts, so the checks hold even if the deposit and
  // first-period amounts happen to match: the deposit invoice by its chip, the first-period one as the
  // remaining (non-deposit) number.
  const invoices = detailsPage.invoicesCard.dataTable;
  const depositInvoiceNumber = await getBookingDepositInvoiceNumber(invoices);
  const firstInvoiceNumber = await getBookingNonDepositInvoiceNumber(invoices, depositInvoiceNumber);

  await test.step('verify booking invoices in detail', async () => {
    await verifyBookingInvoice(
      detailsPage,
      {
        ...invoiceBase,
        amount: depositAmount,
        amountDue: depositAmount,
      },
      'Deposit'
    );
    await verifyBookingInvoice(
      detailsPage,
      {
        ...invoiceBase,
        unit: data.unit.name,
        amount: firstInvoiceAmount,
        amountDue: firstInvoiceAmount,
      },
      firstInvoiceNumber
    );
  });

  await test.step('verify booking payments', async () => {
    await verifyBookingPaymentCount(detailsPage, 2);

    // each invoice has its own unpaid payment dated today, keyed by invoice number
    await verifyBookingPayment(
      detailsPage,
      { amount: depositAmount, status: 'Not paid yet', date: today },
      depositInvoiceNumber
    );
    await verifyBookingPayment(
      detailsPage,
      { amount: firstInvoiceAmount, status: 'Not paid yet', date: today },
      firstInvoiceNumber
    );
  });

  // Reloads, so run last: the deposit field links its amount to the upfront deposit invoice.
  await test.step('verify deposit on details page', () => verifyBookingDepositInvoiceLink(detailsPage, depositAmount));
});

test.fixme('issues only a deposit invoice for a scheduled upfront-deposit booking', async ({
  createCustomer,
  createUnitType,
  createUnit,
  createDeposit,
  bookingCreateDialog,
  trackBookingForTeardown,
}) => {
  const customer = await createCustomer(getCustomerCreateData());
  const deposit = await createDeposit(getDepositCreateData());
  const unitType = await createUnitType(getUnitTypeCreateData({ deposit }));
  const unit = await createUnit(getUnitCreateData(unitType));
  const data = getBookingCreateData(
    { customer, unitType, unit, deposit },
    { chargeDepositUpfront: true, moveInDaysInTheFuture: 10 }
  );
  const depositAmount = formatCurrency(deposit.priceExclVat, data.location.currency);
  const today = formatDate(new Date(), data.location.timezone);

  await bookingCreateDialog.goto();

  const detailsPage = await test.step('create booking', () => bookingCreateDialog.create(data));

  trackBookingForTeardown(detailsPage.getBookingId());

  await test.step('verify booking invoices', async () => {
    // scheduled, so no first-period invoice yet — only the upfront deposit invoice
    await verifyBookingInvoiceCount(detailsPage, 1);

    // the upfront-deposit invoice is not tied to a unit, so its unit cell is left empty
    await verifyBookingInvoice(
      detailsPage,
      {
        status: 'Open',
        paymentOption: 'Manual',
        amount: depositAmount,
        amountDue: depositAmount,
        issueDate: today,
        dueDate: formatDate(getFutureDate(data.location.invoiceDaysUntilDue, 'day'), data.location.timezone),
      },
      'Deposit'
    );
  });

  await test.step('verify booking payments', async () => {
    await verifyBookingPaymentCount(detailsPage, 1);

    await verifyBookingPayment(
      detailsPage,
      { amount: depositAmount, status: 'Not paid yet', date: today },
      await getBookingDepositInvoiceNumber(detailsPage.invoicesCard.dataTable)
    );
  });

  // Reloads, so run last: the deposit field links its amount to the upfront deposit invoice.
  await test.step('verify deposit on details page', () => verifyBookingDepositInvoiceLink(detailsPage, depositAmount));
});

test('rejects a sepa booking over the payment amount limit', async ({
  createCustomer,
  createUnitType,
  createUnit,
  bookingCreateDialog,
}) => {
  const customer = await createCustomer(getCustomerCreateData());
  const unitType = await createUnitType(
    getUnitTypeCreateData({
      bookingPlans: [
        {
          period: 'monthly',
          amount: 1,
          priceExclVat: 11000,
          discount: undefined,
          publish: true,
          name: 'Basic',
          shortDescription: { EN: 'Basic description' },
        },
      ],
    })
  );
  const unit = await createUnit(getUnitCreateData(unitType));
  // a sepa subscription over the SEPA per-transaction limit is rejected on submit with an error snackbar
  // (no booking is created, so nothing to tear down).
  const data = getBookingCreateData(
    { customer, unitType, unit },
    { paymentOption: 'charge_automatically', paymentMethod: getDefaultSepaPayment() }
  );

  await bookingCreateDialog.goto();

  await test.step('submit booking', async () => {
    await bookingCreateDialog.fill(data);
    await bookingCreateDialog.submit();
  });

  await test.step('verify the payment limit error', async () => {
    await expect(bookingCreateDialog.paymentLimitSnackbar).toBeVisible();
  });
});

test('rejects a booking without all required fields', async ({ bookingCreateDialog }) => {
  await bookingCreateDialog.goto();

  await test.step('submit invalid booking create form', () => bookingCreateDialog.submit());

  await test.step('verify errors on form', async () => {
    await expect(bookingCreateDialog.errors).toHaveCountGreaterThan(0);
  });
});
