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

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

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

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

  // Drag BaseChiller onto canvas
  const card = page.locator("h4:has-text('BaseChiller')").first();
  const canvas = page.locator(".react-flow__pane").first();

  const cBox = await card.boundingBox();
  const cvBox = await canvas.boundingBox();

  if (cBox && cvBox) {
    console.log("Dragging BaseChiller module onto canvas...");
    await page.mouse.move(cBox.x + cBox.width / 2, cBox.y + cBox.height / 2);
    await page.mouse.down();
    await page.mouse.move(cvBox.x + cvBox.width / 2, cvBox.y + cvBox.height / 2, { steps: 15 });
    await page.mouse.up();
  }

  await page.waitForTimeout(1200);

  // 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" });

  // Click Mode Schéma button inside node header
  const modeBtn = page.locator("button:has-text('Mode Schéma')").first();
  if (await modeBtn.isVisible()) {
    console.log("Clicking Mode Schéma button...");
    await modeBtn.click();
    await page.waitForTimeout(1000);
  }

  // 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("Screenshots captured!");
  await browser.close();
})();
