import { getBookingCreateData } from '@/manager/modules/booking/booking-factories';
import { getCustomerCreateData } from '@/manager/modules/customer/customer-factories';
import { unitDeleteHappyTestCases } from '@/manager/modules/unit/test-cases/unit-delete-happy-test-cases';
import { getUnitCreateData } from '@/manager/modules/unit/unit-factories';
import { test } from '@/manager/modules/unit/unit-fixtures';
import { getUnitTypeCreateData } from '@/manager/modules/unit-type/unit-type-factories';
import { expectSingleLocatorToHaveText } from '@/manager/shared/utils/expect-utils';
import { formatId } from '@/shared/utils/formatters';
import { expect } from '@/shared/utils/matchers';

for (const tc of unitDeleteHappyTestCases) {
  test(tc.description, async ({ createUnit, createUnitType, unitDetailsPage }) => {
    const unitType = await createUnitType(getUnitTypeCreateData());
    const unit = await createUnit(getUnitCreateData(unitType, { status: tc.status }));
    const id = formatId(unit.id);

    await unitDetailsPage.goto(unit.id);

    const listPage = await test.step('delete unit', async () => {
      const dialog = await unitDetailsPage.openUnitDeleteDialog();
      return dialog.delete();
    });

    await test.step('verify unit is no longer on list page', async () => {
      await listPage.searchTextField.fill(id);
      await expect(listPage.dataTable.noDataRow).toBeVisible();
    });
  });
}

test('hides the delete action for a unit with an active booking', async ({
  createBooking,
  createCustomer,
  createUnit,
  createUnitType,
  unitDetailsPage,
}) => {
  const unitType = await createUnitType(getUnitTypeCreateData());
  const [unit, customer] = await Promise.all([
    createUnit(getUnitCreateData(unitType)),
    createCustomer(getCustomerCreateData({ locations: [unitType.location] })),
  ]);
  await createBooking(getBookingCreateData({ customer, unitType, unit }));

  await unitDetailsPage.goto(unit.id);

  await test.step('verify unit is rented', async () => {
    await expectSingleLocatorToHaveText(unitDetailsPage.status, 'rented');
  });

  await test.step('verify delete action is absent', async () => {
    await expect(unitDetailsPage.actionMenuButton).toHaveCount(0);
  });
});
