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

async function openBrowser() {
  const browser = await chromium.launch({
    headless: false,
    slowMo: 100
  });

  const context = await browser.newContext();
  const page = await context.newPage();

  // Navigate to app
  await page.goto('http://localhost:2100');
  await page.waitForTimeout(2000);

  // Check if we need to login
  const loginInput = page.locator('input[name="username"]');
  if (await loginInput.isVisible({ timeout: 3000 }).catch(() => false)) {
    console.log('Logging in as admin...');
    await page.fill('input[name="username"]', 'admin');
    await page.fill('input[name="password"]', 'admin');
    await page.click('button[type="submit"]');
    await page.waitForTimeout(2000);
    console.log('Logged in! Browser is open.');
  } else {
    console.log('Already logged in or different page. Browser is open.');
  }

  // Keep browser open
  console.log('Press Ctrl+C to close the browser.');
  await new Promise(() => {}); // Keep running indefinitely
}

openBrowser().catch(console.error);
