#!/usr/bin/env node
/** Validates app/delivery routes are thin re-exports (≤20 lines). */
import { readFileSync, readdirSync, statSync } from "node:fs";
import { join, relative } from "node:path";
import { fileURLToPath } from "node:url";

const ROOT = join(fileURLToPath(new URL(".", import.meta.url)), "..");
const APP = join(ROOT, "app", "delivery");
const MAX_LINES = 25;

function walk(dir, files = []) {
  for (const e of readdirSync(dir)) {
    const full = join(dir, e);
    if (statSync(full).isDirectory()) walk(full, files);
    else if (e === "page.tsx") files.push(full);
  }
  return files;
}

const violations = [];
for (const file of walk(APP)) {
  const content = readFileSync(file, "utf8");
  const lines = content.split("\n").length;
  const rel = relative(ROOT, file);
  if (lines > MAX_LINES) violations.push(`${rel}: ${lines} lines`);
  if (!content.includes("@/modules/delivery/")) {
    violations.push(`${rel}: must re-export from @/modules/delivery/`);
  }
}

if (violations.length) {
  console.error("Delivery route validation failed:\n" + violations.join("\n"));
  process.exit(1);
}
console.log("Delivery routes OK.");
