import { BasePage } from '@/manager/shared/base/BasePage';
import { CustomerDeleteDialog } from '@/manager/modules/customer/views/CustomerDeleteDialog';
import { CustomerDetailsBookingsCard } from '@/manager/modules/customer/components/CustomerDetailsBookingsCard';
import { CustomerDetailsDetailsCard } from '@/manager/modules/customer/components/CustomerDetailsDetailsCard';
import { CustomerDetailsGeneralCard } from '@/manager/modules/customer/components/CustomerDetailsGeneralCard';
import { CustomerDetailsInvoicesCard } from '@/manager/modules/customer/components/CustomerDetailsInvoicesCard';
import { CustomerEditDetailsDialog } from '@/manager/modules/customer/views/CustomerEditDetailsDialog';
import { CustomerEditGeneralDialog } from '@/manager/modules/customer/views/CustomerEditGeneralDialog';
import { CustomerListPage } from '@/manager/modules/customer/views/CustomerListPage';
import { CustomerSetPasswordDialog } from '@/manager/modules/customer/views/CustomerSetPasswordDialog';
import { getPageHeaderStatusHost, getPageHeaderBackButtonHost } from '@/manager/shared/utils/locator-utils';

export class CustomerDetailsPage extends BasePage {
  readonly status = getPageHeaderStatusHost(this.header);
  readonly backButton = getPageHeaderBackButtonHost(this.header);
  readonly actionMenu = this.host.getByTestId('customer-action-menu');
  readonly actionMenuButton = this.header.getByTestId('customer-action-button');
  readonly actionMenuDeleteItem = this.actionMenu.getByTestId('customer-action-delete');
  readonly actionMenuSetPasswordItem = this.actionMenu.getByTestId('customer-action-set-password');
  readonly createBookingMenuItem = this.actionMenu.getByTestId('customer-action-create-booking');
  readonly generalCard = new CustomerDetailsGeneralCard(this.body);
  readonly detailsCard = new CustomerDetailsDetailsCard(this.body);
  readonly bookingsCard = new CustomerDetailsBookingsCard(this.body);
  readonly invoicesCard = new CustomerDetailsInvoicesCard(this.body);

  async goto(id: number): Promise<void> {
    await this.host.goto(`/customers/${id}`);
  }

  getCustomerId(): number {
    return Number(this.host.url().match(/\/customers\/(\d+)/)?.[1]);
  }

  async openCustomerEditGeneralDialog(): Promise<CustomerEditGeneralDialog> {
    await Promise.all([this.host.waitForURL('**/customers/*/edit/general'), this.generalCard.editButton.click()]);
    return new CustomerEditGeneralDialog(this.host);
  }

  async openCustomerEditDetailsDialog(): Promise<CustomerEditDetailsDialog> {
    await Promise.all([this.host.waitForURL('**/customers/*/edit/details'), this.detailsCard.editButton.click()]);
    return new CustomerEditDetailsDialog(this.host);
  }

  async openCustomerDeleteDialog(): Promise<CustomerDeleteDialog> {
    await this.actionMenuButton.click();
    await this.actionMenuDeleteItem.click();
    return new CustomerDeleteDialog(this.host);
  }

  async openCustomerSetPasswordDialog(): Promise<CustomerSetPasswordDialog> {
    await this.actionMenuButton.click();
    await this.actionMenuSetPasswordItem.click();
    return new CustomerSetPasswordDialog(this.host);
  }

  async returnToCustomerListPage(): Promise<CustomerListPage> {
    await Promise.all([this.host.waitForURL('/customers'), this.backButton.click()]);
    return new CustomerListPage(this.host);
  }
}
