import { chromium } from '@playwright/test';
import { test as setup } from '@playwright/test';

setup('create auth', async ({}) => {
  const browser = await chromium.launch({
    headless: true,
    args: [
      '--disable-web-security',
      '--disable-features=IsolateOrigins,site-per-process',
    ],
  });
  const context = await browser.newContext({});
  const page = await context.newPage();

  // Login
  await page.goto('/');
  await page.waitForLoadState('load');
  await page.click('button:has-text("Sign In")');
  await page.waitForSelector('input[placeholder="Enter email or username"]', {
    state: 'visible',
  });
  await page
    .getByPlaceholder('Enter email or username')
    .fill(process.env.E2E_TEST_USER_EMAIL || '');
  await page.click('button:has-text("Continue")');
  await page
    .getByPlaceholder('Enter your password')
    .fill(process.env.E2E_TEST_USER_PASSWORD || '');
  await page.click('button:has-text("Continue")');

  // Wait for login to complete
  await page.waitForSelector('a:has-text("Notifications")');

  // Save storage state to reuse
  await context.storageState({ path: 'auth-state.json' });
  await browser.close();
});
