const { chromium } = require("C:/Users/ramez/AppData/Roaming/npm/node_modules/playwright");

(async () => {
  console.log("Starting simple Dymola test script on port 3008...");
  const browser = await chromium.launch({ headless: true });
  const page = await browser.newPage({ viewport: { width: 1440, height: 900 } });

  page.on('console', msg => console.log('BROWSER LOG:', msg.text()));

  await page.goto("http://localhost:3008", { waitUntil: "domcontentloaded" });
  await page.waitForTimeout(2000);

  console.log("Clicking Bibliothèque tab...");
  await page.click("button:has-text('Bibliothèque')");
  await page.waitForTimeout(1500);

  console.log("Looking for Poser sur canvas button...");
  const btn = page.locator("button:has-text('Poser sur canvas')").first();
  await btn.waitFor({ state: "visible", timeout: 10000 });
  await btn.click();
  await page.waitForTimeout(1500);

  // Take screenshot Mode 1: Dymola Icon Mode
  const pathMode1 = "C:/Users/ramez/.gemini/antigravity-ide/brain/e16f5552-47ce-4483-9b25-8266bb395491/dymola_mode_1_icon.png";
  await page.screenshot({ path: pathMode1 });
  console.log(`Saved Mode 1 screenshot to ${pathMode1}`);

  // Click Mode Schéma toggle button on the node
  console.log("Looking for Mode Schéma button on node...");
  const schemaBtn = page.locator("button:has-text('Mode Schéma')").first();
  if (await schemaBtn.isVisible()) {
    await schemaBtn.click();
    await page.waitForTimeout(1500);
  }

  // Take screenshot Mode 2: In-Place Internal Diagram Mode
  const pathMode2 = "C:/Users/ramez/.gemini/antigravity-ide/brain/e16f5552-47ce-4483-9b25-8266bb395491/dymola_mode_2_diagram.png";
  await page.screenshot({ path: pathMode2 });
  console.log(`Saved Mode 2 screenshot to ${pathMode2}`);

  await browser.close();
})();
