/**
 * Verify Auto-Dismiss Cookie Banner Works
 * Tests that form becomes usable after 8 seconds
 */

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

test('Cookie banner auto-dismisses and form works', async ({ page }) => {
  test.setTimeout(60000);

  console.log('\n🧪 Testing cookie auto-dismiss and form functionality\n');

  // Navigate
  await page.goto('http://localhost:9000/en/demo');
  await page.waitForLoadState('networkidle');

  console.log('✅ Page loaded');

  // Check if cookie banner is visible
  const cookieBanner = page.locator('text=/We Use Cookies/i');
  const bannerVisible = await cookieBanner.isVisible().catch(() => false);

  if (bannerVisible) {
    console.log('🍪 Cookie banner is visible');
    console.log('⏳ Waiting 9 seconds for auto-dismiss...');

    // Wait for auto-dismiss (8 seconds + 1 second buffer)
    await page.waitForTimeout(9000);

    // Check if banner disappeared
    const stillVisible = await cookieBanner.isVisible().catch(() => false);
    console.log(`   Banner still visible after 9s: ${stillVisible}`);

    expect(stillVisible).toBe(false);
    console.log('✅ Cookie banner auto-dismissed');
  } else {
    console.log('✅ No cookie banner (already accepted)');
  }

  // Now form should be usable - test duration button click
  console.log('\n📋 Testing form is clickable after auto-dismiss');

  const durationButton = page.getByRole('button', { name: /30 minutes/i });
  await expect(durationButton).toBeVisible();
  await expect(durationButton).toBeEnabled();

  await durationButton.click();
  console.log('✅ Duration button clicked successfully');

  // Verify selection worked
  const feedback = page.getByText(/You've selected a 30 minutes/i);
  await expect(feedback).toBeVisible({ timeout: 5000 });
  console.log('✅ Duration selection confirmed');

  console.log('\n🎉 Test PASSED: Form is usable after auto-dismiss!');
});
