import { unitTableColumnTestIds } 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 { IUnit } from '@/manager/modules/unit/types/unit-types';
import { UnitDetailsPage } from '@/manager/modules/unit/views/UnitDetailsPage';
import { UnitListPage } from '@/manager/modules/unit/views/UnitListPage';
import { expectSingleLocatorToHaveText } from '@/manager/shared/utils/expect-utils';
import { formatId, formatSize, formatUnit } from '@/shared/utils/format-utils';
import { expect } from '@playwright/test';

export async function expectUnitDetailsPageAndThenListPageToShowCorrectData(
  startPage: UnitDetailsPage,
  unit: IUnit
): Promise<UnitListPage> {
  await expectUnitDetailsPageToShowCorrectData(startPage, unit);
  const listPage = await startPage.returnToUnitListPage();
  await expectUnitListPageToShowCorrectData(listPage, unit);
  return listPage;
}

export async function expectUnitListPageAndThenDetailsPageToShowCorrectData(
  startPage: UnitListPage,
  unit: IUnit
): Promise<UnitDetailsPage> {
  await expectUnitListPageToShowCorrectData(startPage, unit);
  const detailPage = await startPage.goToUnitDetailsPage(0);
  await expectUnitDetailsPageToShowCorrectData(detailPage, unit);
  return detailPage;
}

export async function expectUnitDetailsPageToShowCorrectData(page: UnitDetailsPage, unit: IUnit): Promise<void> {
  await expectSingleLocatorToHaveText(page.title, unit.name);
  await expectSingleLocatorToHaveText(page.status, unit.status);
  await expectSingleLocatorToHaveText(page.generalCard.name, unit.name);
  await expectSingleLocatorToHaveText(page.generalCard.location, unit.location.name);
  await expectSingleLocatorToHaveText(page.generalCard.unitType, unit.unitType.name);
  await expectSingleLocatorToHaveText(page.measurementsCard.area, formatUnit(unit.measurements.area, 'm²'));
  await expectSingleLocatorToHaveText(
    page.measurementsCard.ceilingHeight,
    formatUnit(unit.measurements.ceilingHeight, 'cm')
  );
  await expectSingleLocatorToHaveText(page.measurementsCard.width, formatUnit(unit.measurements.width, 'cm'));
  await expectSingleLocatorToHaveText(page.measurementsCard.depth, formatUnit(unit.measurements.depth, 'cm'));
  await expectSingleLocatorToHaveText(page.measurementsCard.volume, formatUnit(unit.measurements.volume, 'm³'));
  await expectSingleLocatorToHaveText(page.measurementsCard.doorType, unit.measurements.doorType);
  await expectSingleLocatorToHaveText(page.measurementsCard.doorWidth, formatUnit(unit.measurements.doorWidth, 'cm'));
  await expectSingleLocatorToHaveText(page.measurementsCard.doorHeight, formatUnit(unit.measurements.doorHeight, 'cm'));
  await expectSingleLocatorToHaveText(
    page.measurementsCard.floor,
    unit.measurements.floor != null ? `${unit.measurements.floor}` : undefined
  );
  // TODO: unit bookings data table checks
}

export async function expectUnitListPageToShowCorrectData(page: UnitListPage, unit: IUnit): Promise<void> {
  const unitId = formatId(unit.id);

  await page.filter({
    search: unitId,
  });

  await expect(page.dataTable.getRows()).toHaveCount(1);
  await expectDataTableTextColumnToHaveText(page.dataTable, unitTableColumnTestIds.id, unitId);
  await expectDataTableTextColumnToHaveText(page.dataTable, unitTableColumnTestIds.name, unit.name);
  await expectDataTableTextColumnToHaveText(page.dataTable, unitTableColumnTestIds.location, unit.location.name);
  await expectDataTableTextColumnToHaveText(page.dataTable, unitTableColumnTestIds.unitType, unit.unitType.name);
  await expectDataTableTextColumnToHaveText(page.dataTable, unitTableColumnTestIds.status, unit.status);
  await expectDataTableTextColumnToHaveText(
    page.dataTable,
    unitTableColumnTestIds.unitTypeSize,
    formatSize(unit.unitType.size, unit.location.sizeFormat, 2)
  );
  await expectDataTableTextColumnToHaveText(
    page.dataTable,
    unitTableColumnTestIds.area,
    formatUnit(unit.measurements.area, 'm²')
  );
  await expectDataTableTextColumnToHaveText(
    page.dataTable,
    unitTableColumnTestIds.width,
    formatUnit(unit.measurements.width, 'cm')
  );
  await expectDataTableTextColumnToHaveText(
    page.dataTable,
    unitTableColumnTestIds.depth,
    formatUnit(unit.measurements.depth, 'cm')
  );
  // TODO: further unit data table checks
}
