/**
 * Renders branded social cards to PNG for visual review of typography/layout.
 * Run: npx tsx scripts/preview-banner.ts
 */
import { promises as fs } from "fs";
import path from "path";
import { Resvg } from "@resvg/resvg-js";
import { buildBrandedSocialCardSvg, buildSocialImageContext } from "../src/lib/generation/social-image";
import type { WebsiteIntelligence } from "../src/lib/context/website-intelligence";
import type { BrandProfile, PostSequenceItem, Project } from "../src/types/workflow";

const OUT_DIR = path.join(process.cwd(), "data", "media", "_preview");

const story = {
  problem: "Manual document sends stall deals and leave no audit trail.",
  impact: "Teams recover hours each week and can prove every delivery.",
  brandResponse: "Encrypted, tracked document delivery your compliance team trusts.",
};

const website: WebsiteIntelligence = {
  url: "https://www.b2befax.com",
  fetchedAt: new Date().toISOString(),
  ok: true,
  title: "B2BEFAX",
  description: "B2B fax broadcasting for growing businesses",
  tagline: "B2B fax broadcasting for growing businesses",
  companyName: "B2BEFAX",
  primaryService: "B2B Fax Broadcasting",
  services: [
    { label: "Send & Receive Online", detail: "Fax documents in a few clicks." },
    { label: "Secure Delivery", detail: "Encrypted end to end." },
  ],
  about: "B2B fax broadcasting",
  products: [],
  features: [],
  targetAudience: "",
  industries: [],
  locations: [],
  benefits: ["AUDIT READY", "ENCRYPTED", "NO HARDWARE"],
  valuePropositions: ["B2B fax broadcasting for growing businesses"],
  differentiators: [],
  websiteClaims: [],
  faqs: [],
  keywords: [],
  themeColor: "#1e3a5f",
  accentColor: "#e87121",
  vertical: "fax",
  productVisualHint: "secure document delivery",
  headings: ["B2B Fax Broadcasting"],
  logoUrl: null,
  contactPhone: "+1 (800) 555-0199",
  contactEmail: "sales@b2befax.com",
  address: "missing",
  analysisMarkdown: "Primary service: B2B Fax Broadcasting",
};

const project: Project = {
  id: "preview",
  name: "B2BEFAX",
  website: "https://www.b2befax.com",
  industry: "B2B Communications",
  timezone: "America/New_York",
  countries: ["US"],
  status: "active",
  createdAt: "",
  updatedAt: "",
};

const brand: BrandProfile = {
  companyName: "B2BEFAX",
  tagline: "B2B fax broadcasting for growing businesses",
  industry: "B2B Communications",
  brandVoice: "Professional",
  logoDataUrl: null,
  logoFileName: null,
  contactPhone: "+1 (800) 555-0199",
  contactEmail: "sales@b2befax.com",
};

const sequenceItem: PostSequenceItem = {
  id: "seq-1",
  title: "Post 1 — Awareness",
  summary: "Introduce the challenge",
  tag: "Awareness",
  content: "Problem and impact content",
};

async function main() {
  await fs.mkdir(OUT_DIR, { recursive: true });

  // Seeds hit different layout + typography branches.
  for (const seed of [11, 24, 37]) {
    const ctx = buildSocialImageContext({
      project,
      brand,
      sequenceItem,
      caption: "**Send every document securely** with full delivery proof.",
      storySections: { currentStory: "", keyTheme: "Awareness · Trust", ...story },
      website,
      layoutSeed: seed,
    });

    const svg = decodeURIComponent(buildBrandedSocialCardSvg(ctx).split(",")[1] ?? "");
    const pngPath = path.join(OUT_DIR, `banner-seed-${seed}.png`);
    const resvg = new Resvg(svg, {
      fitTo: { mode: "width", value: 1080 },
      font: { loadSystemFonts: true },
    });
    await fs.writeFile(pngPath, resvg.render().asPng());
    console.log(`rendered ${pngPath}`);
  }
}

void main();
