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

test('production login redirects to correct domain', async ({ page }) => {
  console.log('\n=== Testing Production Login Flow ===\n');

  // Step 1: Navigate to login
  console.log('Step 1: Navigating to https://smoothschedule.com/login');
  await page.goto('https://smoothschedule.com/login');
  await page.waitForLoadState('networkidle');
  console.log('   Current URL:', page.url());

  // Step 2: Fill credentials
  console.log('\nStep 2: Filling credentials (poduck@gmail.com / starry)');
  await page.fill('input[name="username"]', 'poduck@gmail.com');
  await page.fill('input[name="password"]', 'starry');

  // Step 3: Click login
  console.log('\nStep 3: Clicking login button');
  await page.click('button[type="submit"]');

  // Wait for navigation
  await page.waitForTimeout(3000);
  const finalUrl = page.url();
  console.log('   URL after login:', finalUrl);

  // Check if we're on the correct domain
  if (finalUrl.includes('platform.smoothschedule.com')) {
    console.log('\n✅ SUCCESS! Redirected to platform.smoothschedule.com');
  } else if (finalUrl.includes('platform.lvh.me')) {
    console.log('\n❌ FAILED! Redirected to platform.lvh.me (wrong domain)');
    throw new Error('Redirect went to lvh.me instead of smoothschedule.com');
  } else if (finalUrl.includes('smoothschedule.com/login')) {
    console.log('\n⚠️  Still on login page - may indicate login failure');
    throw new Error('Still on login page after submitting credentials');
  } else {
    console.log('\n⚠️  Unexpected URL:', finalUrl);
  }

  // Verify we're actually on platform.smoothschedule.com
  expect(finalUrl).toContain('platform.smoothschedule.com');

  console.log('\n✅ Test PASSED!');
});
