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

test.describe('Guest Profile Flow', () => {
  test('should require name setup on first event join and persist it', async ({ page }) => {
    // Assume Vite dev server is running on localhost:5173
    await page.goto('http://localhost:5173/');

    // Enter event slug manually
    await page.fill('input[placeholder*="Event-Code"]', 'test-event');
    await page.click('button:has-text("Event beitreten")');

    // Should redirect to setup if no name
    await expect(page).toHaveURL(/.*\/e\/test-event\/setup/);

    // Fill name and submit
    await page.fill('input[placeholder*="Dein Name"]', 'Test User');
    await page.click('button:has-text("LET\'S GO! ✨")');

    // Should navigate to home
    await expect(page).toHaveURL(/.*\/e\/test-event$/);

    // Check localStorage
    const storedName = await page.evaluate(() => localStorage.getItem('guestName_test-event'));
    expect(storedName).toBe('Test User');

    // Reload to test persistence - should stay on home, not redirect to setup
    await page.reload();
    await expect(page).toHaveURL(/.*\/e\/test-event$/);

    // Re-nav to landing and join again - should go directly to home
    await page.goto('http://localhost:5173/');
    await page.fill('input[placeholder*="Event-Code"]', 'test-event');
    await page.click('button:has-text("Event beitreten")');
    await expect(page).toHaveURL(/.*\/e\/test-event$/);
  });

  test('should go directly to home if name already stored', async ({ page }) => {
    // Pre-set name in localStorage
    await page.addInitScript(() => {
      localStorage.setItem('guestName_test-event', 'Existing User');
    });

    await page.goto('http://localhost:5173/');

    // Join
    await page.fill('input[placeholder*="Event-Code"]', 'test-event');
    await page.click('button:has-text("Event beitreten")');

    // Should go directly to home
    await expect(page).toHaveURL(/.*\/e\/test-event$/);
  });
});