/**
 * Regression check for on-image copy guards, using the exact strings that
 * appeared as defects on generated posters.
 *
 *   npx tsx scripts/check-copy-sanitize.ts
 */
import {
  looksLikeSlogan,
  serviceForInlineCopy,
  stripNarrativeScaffolding,
} from "../src/lib/generation/copy-sanitize";
import { stripPlatformCaptionLabels } from "../src/lib/generation/format-caption";
import { detectSolutionCategory } from "../src/lib/generation/dynamic-product-config";

let failures = 0;

function expect(label: string, actual: string, wanted: string) {
  const ok = actual === wanted;
  if (!ok) failures += 1;
  console.log(`${ok ? "PASS" : "FAIL"}  ${label}`);
  if (!ok) console.log(`      got: ${JSON.stringify(actual)}\n      want: ${JSON.stringify(wanted)}`);
}

// Scaffolding that leaked onto the posters as feature headings.
expect(
  "why-it-persists prefix",
  stripNarrativeScaffolding("Why it persists: High communication and telecom costs"),
  "High communication and telecom costs",
);
expect(
  "problem-introduction prefix",
  stripNarrativeScaffolding("Problem introduction: High communication and telecom costs"),
  "High communication and telecom costs",
);
expect(
  "character-role prefix",
  stripNarrativeScaffolding(
    "For operations teams at mid-size firms, a realistic situation is dealing with: missed routes",
  ),
  "Missed routes",
);
expect(
  "insight-role prefix",
  stripNarrativeScaffolding(
    "The useful reframe: high telecom cost is usually a process issue",
  ),
  "High telecom cost is usually a process issue",
);
expect("clean text untouched", stripNarrativeScaffolding("Calls reach the wrong team"), "Calls reach the wrong team");
expect("empty stays empty", stripNarrativeScaffolding(undefined), "");

// The tagline that produced "With smarter. faster. more intelligent.:" on the poster.
expect("slogan rejected as service", serviceForInlineCopy("Smarter. Faster. More Intelligent."), "");
expect("real service kept", serviceForInlineCopy("intelligent call routing"), "intelligent call routing");
expect("trailing punctuation trimmed", serviceForInlineCopy("online faxing."), "online faxing");

expect(
  "linkedin label stripped",
  stripPlatformCaptionLabels("**LinkedIn Post:**\n\nHow businesses can improve their online presence.\n\n#WebsiteDevelopment"),
  "How businesses can improve their online presence.\n\n#WebsiteDevelopment",
);
expect(
  "heading label stripped",
  stripPlatformCaptionLabels("### LinkedIn Post\n\nHow businesses can improve their online presence."),
  "How businesses can improve their online presence.",
);
expect(
  "second platform section dropped",
  stripPlatformCaptionLabels("How businesses can improve.\n\n---\n\n**Facebook Post:**\n\nA shorter version."),
  "How businesses can improve.",
);

const dental = detectSolutionCategory({
  brand: { companyName: "Bright Smile", industry: "Dental", tagline: "", brandVoice: "", targetAudience: "", productsServices: "dental website", logoDataUrl: null, logoFileName: "", contactPhone: "", contactEmail: "", colors: [] } as never,
  project: { name: "Bright Smile", industry: "Dental clinic", website: "https://brightsmile.example" } as never,
  website: { description: "A dental clinic website for appointments", title: "Bright Smile Dental", services: [{ label: "Dental website", detail: "Online booking" }] } as never,
});
expect("dental site is not cms", dental, "generic");

console.log("\nslogan detection:");
for (const s of [
  "Smarter. Faster. More Intelligent.",
  "Revolutionize your business",
  "online faxing",
  "intelligent call routing",
]) {
  console.log(`  ${looksLikeSlogan(s) ? "slogan " : "service"}  ${JSON.stringify(s)}`);
}

console.log(failures ? `\n${failures} failing case(s)` : "\nAll cases passed");
process.exit(failures ? 1 : 0);
