#!/usr/bin/env bun
import { readdir } from "node:fs/promises";
import { join } from "node:path";

const root = join(import.meta.dir, "..", "k8s");
const documents: Record<string, any>[] = [];
for (const filename of (await readdir(root)).filter((name) => name.endsWith(".yaml")).sort()) {
  const path = join(root, filename);
  const parsed = Bun.YAML.parse(await Bun.file(path).text());
  const stream = Array.isArray(parsed) ? parsed : [parsed];
  for (const document of stream) {
    if (!document || typeof document !== "object") throw new Error(`${path}: empty/non-object document`);
    if (!document.apiVersion) throw new Error(`${path}: apiVersion missing`);
    if (!document.kind) throw new Error(`${path}: kind missing`);
    if (!document.metadata?.name) throw new Error(`${path}: metadata.name missing`);
    documents.push(document);
  }
}

if (!documents.some((d) => d.kind === "PersistentVolumeClaim")) throw new Error("PVC missing");
if (!documents.some((d) => d.kind === "NetworkPolicy" && d.metadata.name === "default-deny-egress")) {
  throw new Error("default deny missing");
}
console.log(`manifest dry parse: PASS (${documents.length} resources)`);
