import { BasePage } from '@/manager/shared/base/BasePage';
import { InsuranceDetailsGeneralCard } from '@/manager/modules/insurance/components/InsuranceDetailsGeneralCard';
import { InsuranceDetailsBookingPortalCard } from '@/manager/modules/insurance/components/InsuranceDetailsBookingPortalCard';
import { TaxCard } from '@/manager/modules/ui/tax/components/TaxCard';
import { TaxEditDialog } from '@/manager/modules/ui/tax/views/TaxEditDialog';
import { BookingPlanCard } from '@/manager/modules/ui/booking-plan/components/BookingPlanCard';
import { InsuranceDeleteDialog } from '@/manager/modules/insurance/views/InsuranceDeleteDialog';
import { InsuranceEditBookingPortalDialog } from '@/manager/modules/insurance/views/InsuranceEditBookingPortalDialog';
import { InsuranceEditGeneralDialog } from '@/manager/modules/insurance/views/InsuranceEditGeneralDialog';
import { InsuranceListPage } from '@/manager/modules/insurance/views/InsuranceListPage';
import { getPageHeaderBackButtonHost } from '@/manager/shared/utils/locator-utils';

export class InsuranceDetailsPage extends BasePage {
  readonly backButton = getPageHeaderBackButtonHost(this.header);
  readonly actionMenu = this.host.getByTestId('insurance-action-menu');
  readonly actionMenuButton = this.header.getByTestId('insurance-action-button');
  readonly actionMenuDeleteItem = this.actionMenu.getByTestId('insurance-action-delete');
  readonly generalCard = new InsuranceDetailsGeneralCard(this.body);
  readonly bookingPortalCard = new InsuranceDetailsBookingPortalCard(this.body);
  readonly taxCard = new TaxCard(this.body);
  readonly bookingPlanCard = new BookingPlanCard(this.body);

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

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

  async openInsuranceEditBookingPortalDialog(): Promise<InsuranceEditBookingPortalDialog> {
    await Promise.all([
      this.host.waitForURL('**/insurances/*/edit/storefront'),
      this.bookingPortalCard.editButton.click(),
    ]);
    return new InsuranceEditBookingPortalDialog(this.host);
  }

  async openTaxEditDialog(): Promise<TaxEditDialog> {
    return this.taxCard.openTaxEditDialog();
  }

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

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

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