const { chromium } = require("C:/Users/ramez/AppData/Roaming/npm/node_modules/playwright");
const path = require("path");
const outDir = process.argv[2] || process.cwd();

(async () => {
  const browser = await chromium.launch({ headless: true });
  const page = await browser.newPage({ viewport: { width: 1600, height: 1000 } });
  const snap = async (name) => {
    const p = path.join(outDir, name);
    await page.screenshot({ path: p, fullPage: false });
    console.log("saved", p);
  };
  try {
    await page.goto("http://localhost:3006", { waitUntil: "networkidle", timeout: 20000 });
    await page.waitForTimeout(2500);
    await page.selectOption("select[title*='Exemple']", "chiller_flooded_4port_watercooled.json");
    await page.waitForTimeout(2500);
    // click on canvas center to maybe select a node
    await page.click(".react-flow__pane", { position: { x: 600, y: 450 } });
    await page.waitForTimeout(500);
    await page.click(".react-flow__pane", { position: { x: 800, y: 450 } });
    await page.waitForTimeout(600);
    await snap("08-properties-panel.png");

    // click on comp node roughly
    await page.click(".react-flow__pane", { position: { x: 500, y: 450 } });
    await page.waitForTimeout(600);
    await snap("09-properties-comp.png");

    // Open new model modal via toolbar button
    await page.click("button:has-text('Nouveau Modèle')");
    await page.waitForTimeout(800);
    await snap("10-new-model-modal.png");
  } catch (e) {
    console.error("Error:", e.message);
    await snap("zz-error2.png");
  } finally {
    await browser.close();
  }
})();
