import { getBrandCopy } from "@/lib/brand";
import { readFile } from "node:fs/promises";
import path from "node:path";
import { ImageResponse } from "next/og";
import { isLocale } from "@alterra/shared";
import { getLayoutCopy } from "@/lib/site";

const loadLogo = async () => {
  const svg = await readFile(path.join(process.cwd(), "public/brand/logo-stacked-dark.svg"));
  return `data:image/svg+xml;base64,${svg.toString("base64")}`;
};

/** Default 1200×630 share image with the official logo (pages with their own photo override it). */
export async function GET(_req: Request, { params }: { params: Promise<{ lang: string }> }) {
  const { lang } = await params;
  const copy = getLayoutCopy(isLocale(lang) ? lang : "en");
  const logo = await loadLogo();
  const brand = getBrandCopy(isLocale(lang) ? lang : "en");
  return new ImageResponse(
    (
      <div
        style={{
          width: "100%",
          height: "100%",
          display: "flex",
          flexDirection: "column",
          alignItems: "center",
          justifyContent: "center",
          background: "linear-gradient(135deg, #102B1E 0%, #163626 55%, #2B4D3F 100%)",
          color: "#F4EFE6",
        }}
      >
        {/* eslint-disable-next-line @next/next/no-img-element, jsx-a11y/alt-text */}
        <img src={logo} width={486} height={301} />
        <div style={{ marginTop: 44, width: 110, height: 2, background: "#CC9E42" }} />
        <div style={{ marginTop: 30, fontSize: 30, letterSpacing: 2, textAlign: "center" }}>{copy.tagline}</div>
        <div style={{ marginTop: 14, fontSize: 18, letterSpacing: 7, textTransform: "uppercase", color: "#DDB868" }}>{brand.signature}</div>
        <div style={{ marginTop: 10, fontSize: 20, color: "#E6DDCE", opacity: 0.75 }}>Antioquia · Colombia</div>
      </div>
    ),
    { width: 1200, height: 630, headers: { "Cache-Control": "public, max-age=86400, immutable" } },
  );
}
