/**
 * Demo Bookings Management Tests
 */

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

test.describe('Demo Bookings Management', () => {
  test.beforeEach(async ({ page }) => {
    // Login as staff
    await page.goto('/staff/login');
    await page.fill('input[type="text"]', 'asim');
    await page.fill('input[type="password"]', 'AsimMawidi2025!');
    await page.click('button[type="submit"]');
    await page.waitForURL('/staff/dashboard');

    // Navigate to demos page
    await page.click('nav >> text=Demo Bookings');
    await page.waitForURL('/staff/dashboard/demos');
  });

  test('should display demo bookings page', async ({ page }) => {
    await expect(page.locator('h1')).toContainText('Demo Bookings');
    await expect(page.locator('text=Manage and track all demo appointment requests')).toBeVisible();
  });

  test('should have filter controls', async ({ page }) => {
    // Check search input
    await expect(page.locator('input[placeholder*="Search"]')).toBeVisible();

    // Check status filter
    const statusFilter = page.locator('select').first();
    await expect(statusFilter).toBeVisible();
    await expect(statusFilter).toContainText('All Status');

    // Check date range filter
    const dateFilter = page.locator('select').nth(1);
    await expect(dateFilter).toBeVisible();

    // Check clear button
    await expect(page.locator('button:has-text("Clear")')).toBeVisible();
  });

  test('should filter by status', async ({ page }) => {
    // Wait for bookings to load
    await page.waitForSelector('text=/booking/i', { timeout: 10000 });

    // Select "Pending" status
    const statusFilter = page.locator('select').first();
    await statusFilter.selectOption('pending');

    // Wait for filter to apply
    await page.waitForTimeout(1000);

    // All visible bookings should have pending status
    const statusBadges = page.locator('span:has-text("pending")');
    const count = await statusBadges.count();

    // If there are pending bookings, count should be > 0
    // If no pending bookings, should show "No bookings found"
    if (count === 0) {
      await expect(page.locator('text=No bookings found')).toBeVisible();
    }
  });

  test('should search bookings by name', async ({ page }) => {
    await page.waitForTimeout(2000);

    const searchInput = page.locator('input[placeholder*="Search"]');
    await searchInput.fill('Demo Customer');
    await page.waitForTimeout(1000);

    // Results should update (either show matches or "no bookings found")
    const resultsText = page.locator('text=/\\d+ booking/');
    await expect(resultsText).toBeVisible();
  });

  test('should display booking details', async ({ page }) => {
    await page.waitForTimeout(2000);

    // Check if any bookings exist
    const bookingCards = page.locator('[class*="rounded-xl"]:has-text("@")');
    const count = await bookingCards.count();

    if (count > 0) {
      const firstBooking = bookingCards.first();

      // Should display contact information
      await expect(firstBooking.locator('svg.lucide-mail')).toBeVisible();
      await expect(firstBooking.locator('svg.lucide-phone')).toBeVisible();
      await expect(firstBooking.locator('svg.lucide-calendar')).toBeVisible();
    }
  });

  test('should have action buttons for pending bookings', async ({ page }) => {
    await page.waitForTimeout(2000);

    // Filter to pending
    await page.locator('select').first().selectOption('pending');
    await page.waitForTimeout(1000);

    // Check if pending bookings have action buttons
    const confirmButton = page.locator('button:has-text("Confirm")').first();
    if (await confirmButton.isVisible()) {
      await expect(confirmButton).toBeVisible();
      await expect(page.locator('button:has-text("Add Link")').first()).toBeVisible();
      await expect(page.locator('button:has-text("Cancel")').first()).toBeVisible();
      await expect(page.locator('a:has-text("Email")').first()).toBeVisible();
    }
  });

  test('should open meeting link modal', async ({ page }) => {
    await page.waitForTimeout(2000);

    // Filter to pending
    await page.locator('select').first().selectOption('pending');
    await page.waitForTimeout(1000);

    // Click "Add Link" button if exists
    const addLinkButton = page.locator('button:has-text("Add Link")').first();
    if (await addLinkButton.isVisible()) {
      await addLinkButton.click();

      // Modal should appear
      await expect(page.locator('text=Add Meeting Link')).toBeVisible();
      await expect(page.locator('input[name="meetingLink"]')).toBeVisible();
      await expect(page.locator('button:has-text("Cancel")')).toBeVisible();
      await expect(page.locator('button:has-text("Save Link")')).toBeVisible();

      // Close modal
      await page.locator('button:has-text("Cancel")').click();
      await expect(page.locator('text=Add Meeting Link')).not.toBeVisible();
    }
  });

  test('should refresh bookings list', async ({ page }) => {
    await page.waitForTimeout(2000);

    // Click refresh button
    const refreshButton = page.locator('button:has-text("Refresh")');
    await refreshButton.click();

    // Should trigger API call
    await page.waitForResponse((response) =>
      response.url().includes('/api/staff/dashboard/demos')
    );
  });

  test('should clear filters', async ({ page }) => {
    await page.waitForTimeout(2000);

    // Apply some filters
    await page.locator('select').first().selectOption('confirmed');
    await page.locator('input[placeholder*="Search"]').fill('test');

    // Click clear
    await page.click('button:has-text("Clear")');

    // Filters should reset
    const statusFilter = page.locator('select').first();
    expect(await statusFilter.inputValue()).toBe('all');

    const searchInput = page.locator('input[placeholder*="Search"]');
    expect(await searchInput.inputValue()).toBe('');
  });
});
