import { readFile } from "node:fs/promises";
import path from "node:path";
import { ImageResponse } from "next/og";
import { shareImageSize } from "./seo";

// Brand tokens from src/styles/an.css (--ink, --brand-purple, --mint). The card is an image, so it can't read CSS.
const INK = "#000C38";
const PURPLE = "#6431F4";
const MINT = "#38BDF8";

/** The 1200×630 card used for link previews: logo, an eyebrow, a headline and a footer line. */
export async function shareCard({ eyebrow, title, footer }: { eyebrow: string; title: string; footer: string }) {
  const logo = await readFile(path.join(process.cwd(), "public/assets/images/logo-dark.svg"));
  const logoSrc = `data:image/svg+xml;base64,${logo.toString("base64")}`;
  const titleSize = title.length > 70 ? 56 : title.length > 45 ? 64 : 76;

  return new ImageResponse(
    (
      <div
        style={{
          width: "100%",
          height: "100%",
          display: "flex",
          flexDirection: "column",
          justifyContent: "space-between",
          padding: "72px 80px",
          background: `radial-gradient(circle at 85% 15%, #2A1580 0%, ${INK} 55%)`,
          color: "#ffffff",
        }}
      >
        {/* The logo's viewBox is 2393×349. next/og draws a plain <img>; next/image doesn't apply here. */}
        {/* eslint-disable-next-line @next/next/no-img-element */}
        <img src={logoSrc} width={330} height={48} alt="" />
        <div style={{ display: "flex", flexDirection: "column", gap: 24 }}>
          <div style={{ display: "flex", fontSize: 26, fontWeight: 600, letterSpacing: 3, textTransform: "uppercase", color: MINT }}>{eyebrow}</div>
          <div style={{ display: "flex", fontSize: titleSize, fontWeight: 700, lineHeight: 1.1, maxWidth: 1000 }}>{title}</div>
        </div>
        <div style={{ display: "flex", alignItems: "center", gap: 20 }}>
          <div style={{ display: "flex", width: 72, height: 6, borderRadius: 3, background: `linear-gradient(90deg, ${PURPLE}, ${MINT})` }} />
          <div style={{ display: "flex", fontSize: 26, color: "#C9CEE0" }}>{footer}</div>
        </div>
      </div>
    ),
    shareImageSize,
  );
}
