import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
import path from "node:path";
import { founders, locales, metrics, pageSlugs, publishedArticleSlugs } from "../src/content/site";

const projectRoot = path.resolve(import.meta.dirname, "..");
const sourceRoot = path.join(projectRoot, "src");
const outputRoot = path.join(projectRoot, "out");

function walk(directory: string): string[] {
  return readdirSync(directory).flatMap((entry) => {
    const fullPath = path.join(directory, entry);
    return statSync(fullPath).isDirectory() ? walk(fullPath) : [fullPath];
  });
}

function textFiles(directory: string): string {
  if (!existsSync(directory)) return "";
  return walk(directory)
    .filter((file) => /\.(?:html|js|json|txt|xml|ts|tsx)$/i.test(file))
    .map((file) => readFileSync(file, "utf8"))
    .join("\n");
}

const sourceText = textFiles(sourceRoot);
const outputText = textFiles(outputRoot);
const failures: string[] = [];

if (JSON.stringify(locales) !== JSON.stringify(["fr", "en", "ar"])) {
  failures.push("Locales must remain fr, en, ar.");
}
if (pageSlugs.includes("industries" as never)) {
  failures.push("An Industries route must not exist.");
}
if (publishedArticleSlugs.length !== 0) {
  failures.push("Draft/template articles must not be published.");
}
if (sourceText.includes("contact@yashalstudios.com") || outputText.includes("contact@yashalstudios.com")) {
  failures.push("The planned contact@yashalstudios.com address must not be published.");
}
if (sourceText.includes("testimonial") && /<AudioTestimonial|<Testimonial/i.test(sourceText)) {
  failures.push("Testimonial components must remain unmounted.");
}
for (const productionNote of [
  "media pending approval",
  "média en attente d’approbation",
  "الوسائط بانتظار الاعتماد",
  "coming soon",
  "bientôt disponible",
  "قريباً",
  "awaiting asset",
  "upload required",
  "rights pending",
  "actif en attente",
  "téléversement requis",
  "حقوق النشر معلقة",
]) {
  if (outputText.toLocaleLowerCase().includes(productionNote)) {
    failures.push(`Public production note found: ${productionNote}`);
  }
}
for (const locale of locales) {
  const workPage = path.join(outputRoot, locale, "work", "index.html");
  if (existsSync(workPage) && /<video[\s>]/i.test(readFileSync(workPage, "utf8"))) {
    failures.push(`Portfolio video must remain unmounted on /${locale}/work/.`);
  }
}
if (!existsSync(path.join(projectRoot, "public", "assets", "videos", "hero.mp4"))) {
  failures.push("The approved Hero video is missing.");
}
for (const identifier of ["ICE", "registre de commerce", "tax identifier"]) {
  if (outputText.includes(identifier) && !outputText.includes("No RC, ICE")) {
    failures.push(`Potential unconfirmed legal identifier found: ${identifier}`);
  }
}
if (founders.length !== 3) {
  failures.push("The three confirmed founders must be preserved.");
}
if (metrics.map(({ value }) => value).join("|") !== "20+|20+|5M+|50+|5+|5") {
  failures.push("Confirmed metrics changed.");
}

if (existsSync(outputRoot)) {
  for (const locale of locales) {
    if (!existsSync(path.join(outputRoot, locale, "index.html"))) {
      failures.push(`Missing exported /${locale}/ route.`);
    }
  }
  for (const slug of ["industries", "industry", "solution"]) {
    if (locales.some((locale) => existsSync(path.join(outputRoot, locale, slug)))) {
      failures.push(`Forbidden route exported: ${slug}`);
    }
  }
  if (locales.some((locale) => existsSync(path.join(outputRoot, locale, "knowledge", "article")))) {
    failures.push("Draft Knowledge article route exported.");
  }
  if (!existsSync(path.join(outputRoot, ".htaccess"))) {
    failures.push("out/.htaccess is missing.");
  }
  if (!existsSync(path.join(outputRoot, "contact.php"))) {
    failures.push("out/contact.php is missing.");
  }
}

if (failures.length > 0) {
  console.error(failures.join("\n"));
  process.exit(1);
}

console.log("Content and publication constraints validated.");
