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

test('scheduler loads and reloads correctly after refactoring', async ({ page }) => {
  console.log('\n=== Testing Scheduler Refactoring ===\n');

  // Login as business owner
  console.log('Step 1: Login');
  await page.goto('http://acme.lvh.me:5173/#/login');
  await page.getByLabel(/username/i).fill('acme_owner');
  await page.getByLabel(/password/i).fill('password123');
  await page.getByRole('button', { name: /sign in/i }).click();
  await page.waitForTimeout(2000);
  console.log('✓ Logged in');

  // Navigate to scheduler
  console.log('\nStep 2: Navigate to Scheduler');
  await page.goto('http://acme.lvh.me:5173/#/scheduler');

  // Wait for scheduler content to load (look for the "Resources" label)
  await page.waitForSelector('text=Resources', { timeout: 5000 });
  const hasTimelineContent = await page.locator('text=Resources').first().isVisible();
  console.log('✓ Scheduler content visible:', hasTimelineContent);

  // Take screenshot before reload
  await page.screenshot({ path: 'scheduler-before-reload.png' });

  // Hard reload
  console.log('\nStep 3: Hard reload scheduler page');
  await page.reload({ waitUntil: 'networkidle' });

  // Wait for scheduler to reload (look for the "Resources" label)
  await page.waitForSelector('text=Resources', { timeout: 5000 });
  const hasContentAfterReload = await page.locator('text=Resources').first().isVisible();
  console.log('✓ Scheduler visible after reload:', hasContentAfterReload);

  // Take screenshot after reload
  await page.screenshot({ path: 'scheduler-after-reload.png' });

  // Verify URL didn't change
  expect(page.url()).toContain('scheduler');
  console.log('✓ URL correct:', page.url());

  // Final check
  expect(hasContentAfterReload).toBe(true);
  console.log('\n✅ Test passed: Scheduler works after reload!');
});
