import { readFileSync, statSync } from "node:fs";
import { rootPath } from "./src/config";

const checks: Array<{ readonly path: string; readonly contains: readonly string[]; readonly minBytes: number }> = [
  {
    path: "scripts/assets/web-probe-sentinel-monitor-web/monitor-web.js",
    minBytes: 8_000,
    contains: [
      "createApp",
      "/api/overview",
      "data-monitor-trend-curve",
      "data-monitor-independent-scroll",
      "rootCause",
    ],
  },
  {
    path: "scripts/assets/web-probe-sentinel-monitor-web/monitor-web.css",
    minBytes: 8_000,
    contains: [
      ".trend-stage",
      ".workspace-grid",
      "overflow: hidden",
      "overflow: auto",
      ".trend-red",
      ".trend-warning",
    ],
  },
  {
    path: "scripts/assets/web-probe-sentinel-monitor-web/vendor/vue.esm-browser.prod.js",
    minBytes: 150_000,
    contains: ["createApp", "compiler"],
  },
  {
    path: "scripts/assets/web-probe-sentinel-monitor-web/vendor/VUE-LICENSE",
    minBytes: 500,
    contains: ["MIT License"],
  },
];

const failures: string[] = [];
for (const check of checks) {
  const absolutePath = rootPath(check.path);
  let text = "";
  try {
    const stat = statSync(absolutePath);
    if (stat.size < check.minBytes) failures.push(`${check.path} is too small: ${stat.size} < ${check.minBytes}`);
    text = readFileSync(absolutePath, "utf8");
  } catch (error) {
    failures.push(`${check.path} is missing: ${String(error)}`);
    continue;
  }
  for (const needle of check.contains) {
    if (!text.includes(needle)) failures.push(`${check.path} does not contain ${needle}`);
  }
}

if (failures.length > 0) {
  console.error(JSON.stringify({ ok: false, component: "web-probe-sentinel-monitor-web", failures, valuesRedacted: true }, null, 2));
  process.exit(1);
}

console.log(JSON.stringify({
  ok: true,
  component: "web-probe-sentinel-monitor-web",
  stack: "vue3-vendored-browser-build",
  assets: checks.map((check) => check.path),
  valuesRedacted: true,
}));
