#!/usr/bin/env node
"use strict";

const fs = require("node:fs");
const path = require("node:path");

const root = path.resolve(__dirname, "..");
const activePath = "broadcast/ACTIVE-PIPELINE.hdlp";
const protocolPath = "broadcast/BC-006-IMAGE-FIRST-PRODUCTION.hdlp";
const requiredFiles = [
  activePath,
  protocolPath,
  "eererdan/BROADCAST.hdlp",
  "eererdan/WAKE-ROUTE.hdlp",
  "eererdan/BRAIN-LOAD.hdlp",
  "jianying/WAKE-ROUTE.hdlp",
  "CA-WAKE-ROUTE.hdlp",
  "VA-BROADCAST.hdlp",
];

const failures = [];
const read = (file) => fs.readFileSync(path.join(root, file), "utf8");

for (const file of requiredFiles) {
  if (!fs.existsSync(path.join(root, file))) failures.push(`missing: ${file}`);
}

if (!failures.length) {
  const active = read(activePath);
  if (!active.includes(protocolPath)) failures.push(`active anchor does not point to ${protocolPath}`);

  for (const file of [
    "eererdan/BROADCAST.hdlp",
    "eererdan/WAKE-ROUTE.hdlp",
    "eererdan/BRAIN-LOAD.hdlp",
    "jianying/WAKE-ROUTE.hdlp",
    "CA-WAKE-ROUTE.hdlp",
    "VA-BROADCAST.hdlp",
  ]) {
    if (!read(file).includes(activePath)) failures.push(`${file} does not load ${activePath}`);
  }
}

if (failures.length) {
  console.error("ACTIVE-PIPELINE validation failed:");
  for (const failure of failures) console.error(`- ${failure}`);
  process.exit(1);
}

console.log(`ACTIVE-PIPELINE OK: ${activePath} -> ${protocolPath}`);
