import { chromium } from "playwright";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { parseArgs } from "node:util";

const here = path.dirname(fileURLToPath(import.meta.url));

const { values } = parseArgs({
  options: {
    url: { type: "string", short: "u" },
    out: { type: "string", short: "o" },
  },
});

const url = values.url ?? process.env.URL ?? "http://localhost:3002/dashboard";
const outRel = values.out ?? process.env.OUT ?? "public/my-aria-dashboard.png";
const out = path.isAbsolute(outRel) ? outRel : path.join(here, "..", outRel);

const browser = await chromium.launch();
const page = await browser.newPage({
  viewport: { width: 1440, height: 900 },
  colorScheme: "dark",
});
await page.goto(url, { waitUntil: "networkidle", timeout: 60_000 });
await page.waitForTimeout(1500);
await page.screenshot({ path: out, fullPage: true });
await browser.close();
console.log(`Saved ${out}`);
