import { chromium } from 'playwright';
import { loginAsAdmin } from '/workspace/coderflow/packages/server/tests/helpers/auth-helper.js';

(async () => {
  const browser = await chromium.launch({ headless: true });
  const context = await browser.newContext();
  const page = await context.newPage();

  console.log('Logging in as admin...');
  await loginAsAdmin(page);

  console.log('Navigating to teams page...');
  await page.goto('http://localhost:3000/teams.html');

  // Wait for teams to load and click on an edit team button
  console.log('Waiting for teams table...');
  await page.waitForSelector('#teams-table-body tr');

  // Let's create a new team to ensure we have one, or just open the create modal
  console.log('Opening Create Team modal...');
  await page.click('#create-team-btn');

  // Wait for the modal to be visible
  await page.waitForSelector('#team-modal', { state: 'visible' });

  // Take screenshot
  console.log('Taking screenshot...');
  // Ensure we capture the whole modal
  const modal = await page.locator('#team-modal .modal-content');
  await modal.screenshot({ path: '/task-output/attachments/result-screenshot.png' });

  await browser.close();
  console.log('Done!');
})();
