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

(async () => {
  console.log("Testing Dymola Dual Modes with Playwright...");
  const browser = await chromium.launch({ headless: true });
  const page = await browser.newPage({ viewport: { width: 1440, height: 900 } });

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

  // 1. Open Bibliothèque tab
  console.log("Opening Bibliothèque tab...");
  await page.click("button:has-text('Bibliothèque')");
  await page.waitForTimeout(800);

  // 2. Drag BaseChiller to canvas
  console.log("Dragging BaseChiller module onto canvas...");
  const moduleCard = page.locator("div:has-text('BaseChiller')").first();
  const canvas = page.locator(".react-flow__pane");

  const cardBounds = await moduleCard.boundingBox();
  const canvasBounds = await canvas.boundingBox();

  if (cardBounds && canvasBounds) {
    await page.mouse.move(cardBounds.x + cardBounds.width / 2, cardBounds.y + cardBounds.height / 2);
    await page.mouse.down();
    await page.mouse.move(canvasBounds.x + canvasBounds.width / 2, canvasBounds.y + canvasBounds.height / 2, { steps: 10 });
    await page.mouse.up();
    await page.waitForTimeout(1000);
  }

  // Screenshot Mode 1: Icon View
  await page.screenshot({ path: "C:/Users/ramez/.gemini/antigravity-ide/brain/e16f5552-47ce-4483-9b25-8266bb395491/dymola_mode_1_icon.png" });

  // Toggle Mode 2: Internal Diagram Mode
  const schemaToggle = page.locator("button:has-text('Mode Schéma')").first();
  if (await schemaToggle.isVisible()) {
    console.log("Clicking Mode Schéma toggle...");
    await schemaToggle.click();
    await page.waitForTimeout(800);
  }

  // Screenshot Mode 2: Internal Diagram View
  await page.screenshot({ path: "C:/Users/ramez/.gemini/antigravity-ide/brain/e16f5552-47ce-4483-9b25-8266bb395491/dymola_mode_2_diagram.png" });

  console.log("Both Dymola modes captured successfully!");
  await browser.close();
})();
