import { BasePage } from '@/manager/shared/base/BasePage';
import { IBookingListFilter } from '@/manager/modules/booking/types/booking-types';
import { BookingCreateDialog } from '@/manager/modules/booking/views/BookingCreateDialog';
import { BookingDetailsPage } from '@/manager/modules/booking/views/BookingDetailsPage';
import { DataTable } from '@/manager/modules/ui/data-table/components/DataTable';
import { DebouncedTextField } from '@/manager/shared/components/DebouncedTextField';
import { MultiAutocomplete } from '@/manager/shared/components/MultiAutocomplete';
import { RangeDatePicker } from '@/manager/shared/components/RangeDatePicker';
import { getFilterHost } from '@/manager/shared/utils/host-utils';

export class BookingListPage extends BasePage {
  readonly actionMenu = this.host.getByTestId('booking-list-action-menu');
  readonly actionMenuButton = this.header.getByTestId('booking-list-action-button');
  readonly actionMenuCreateBookingItem = this.actionMenu.locator('.v-list-item').nth(0);
  readonly searchTextField = new DebouncedTextField(getFilterHost(this.body, 'Search'));
  readonly locationAutocomplete = new MultiAutocomplete(getFilterHost(this.body, 'Location'));
  readonly statusAutocomplete = new MultiAutocomplete(getFilterHost(this.body, 'Status').nth(0));
  readonly createdAtDatePicker = new RangeDatePicker(getFilterHost(this.body, 'Created at'));
  readonly dataTable = new DataTable(this.body);

  async filter(filter: IBookingListFilter): Promise<void> {
    filter.search && (await this.searchTextField.fill(filter.search));
    filter.locations && (await this.locationAutocomplete.fill(filter.locations));
    filter.status && (await this.statusAutocomplete.fill(filter.status));
    filter.createdAt && (await this.createdAtDatePicker.fill(filter.createdAt));
  }

  async openBookingCreateDialog(): Promise<BookingCreateDialog> {
    await this.dataTable.waitForLoading();
    await this.actionMenuButton.click();
    await Promise.all([this.host.waitForURL('/bookings/create'), this.actionMenuCreateBookingItem.click()]);
    return new BookingCreateDialog(this.host);
  }

  async goToBookingDetailsPage(index: number): Promise<BookingDetailsPage> {
    await Promise.all([this.host.waitForURL('**/bookings/*'), this.dataTable.clickRow(index)]);
    return new BookingDetailsPage(this.host);
  }
}
