/**
 * E2E Test Helpers
 * Shared utilities for demo booking E2E tests
 */

import { type Page, expect } from '@playwright/test';

/**
 * Wait for cookie consent to dismiss
 */
export async function waitForCookieConsent(page: Page) {
  console.log('⏳ Waiting for cookie consent auto-dismiss...');
  await page.waitForTimeout(8500); // Cookie consent auto-dismisses after 8 seconds
}

/**
 * Navigate to booking form (step 3)
 */
export async function navigateToBookingForm(page: Page) {
  await page.goto('/en/demo');
  await waitForCookieConsent(page);

  // Step 1: Select duration (30 min)
  await page.getByRole('button', { name: /30 min/i }).click();
  await page.getByRole('button', { name: 'Next' }).click();

  // Step 2: Select date and time
  await page.waitForTimeout(1500);

  // Click first enabled date button (not disabled)
  const enabledDateButton = page.locator('button:not([disabled])').filter({ hasText: /^\d+$/ }).first();
  await enabledDateButton.waitFor({ state: 'visible', timeout: 10000 });
  await enabledDateButton.click();

  console.log('   Date selected, waiting for time slots...');

  // Wait for and select first enabled time slot
  await page.waitForTimeout(2000);
  const enabledTime = page.locator('button:not([disabled])').filter({ hasText: /AM|PM|\d+:\d+/ }).first();
  await enabledTime.waitFor({ state: 'visible', timeout: 10000 });
  await enabledTime.click();

  await page.getByRole('button', { name: 'Next', exact: true }).click();

  // Now at step 3 (booking form)
}

/**
 * Complete full booking flow with given data
 */
export async function completeBooking(
  page: Page,
  data: {
    name: string;
    email: string;
    phone: string;
    message?: string;
    duration?: '30min' | '1hr';
  }
) {
  await page.goto('/en/demo');
  await waitForCookieConsent(page);

  // Step 1: Duration
  const durationButton = data.duration === '1hr'
    ? page.getByRole('button', { name: /1 hour|60 min/i })
    : page.getByRole('button', { name: /30 min/i });

  await durationButton.click();
  await page.getByRole('button', { name: 'Next' }).click();

  // Step 2: Date & Time
  await page.waitForTimeout(1500);

  // Click first enabled date button
  const enabledDateButton = page.locator('button:not([disabled])').filter({ hasText: /^\d+$/ }).first();
  await enabledDateButton.waitFor({ state: 'visible', timeout: 10000 });
  await enabledDateButton.click();

  // Wait for time slots
  await page.waitForTimeout(2000);
  const enabledTime = page.locator('button:not([disabled])').filter({ hasText: /AM|PM|\d+:\d+/ }).first();
  await enabledTime.waitFor({ state: 'visible', timeout: 10000 });
  await enabledTime.click();

  await page.getByRole('button', { name: 'Next', exact: true }).click();

  // Step 3: User Info
  await page.fill('input[name="fullName"]', data.name);
  await page.fill('input[name="email"]', data.email);
  await page.fill('input[name="telephone"]', data.phone);

  if (data.message) {
    await page.fill('textarea[name="message"]', data.message);
  }

  await page.getByRole('button', { name: 'Next' }).click();

  // Step 4: Confirm
  await expect(page.getByText(data.name)).toBeVisible();
  await expect(page.getByText(data.email)).toBeVisible();

  await page.getByRole('button', { name: /Confirm Booking/i }).click();

  // Wait for success
  await expect(page.getByText(/confirmed|success/i)).toBeVisible({ timeout: 15000 });

  // Extract booking reference
  const successText = await page.locator('body').textContent();
  const referenceMatch = successText?.match(/BK-[A-Z0-9-]+/);

  return referenceMatch ? referenceMatch[0] : null;
}

