/**
 * Visual Annotation — Blob Storage
 * ──────────────────────────────────
 * Uploads screenshot buffers to Vercel Blob with a 7-day cache-aware prefix.
 *
 * Internal module — vendor names permitted in code; forbidden in user-facing output.
 */

import { put } from "@vercel/blob";

const BLOB_PREFIX = "visual-annotation";

function slugify(s: string): string {
  return s.toLowerCase().replace(/[^a-z0-9]+/g, "-").slice(0, 40);
}

function dateSuffix(): string {
  const d = new Date();
  return `${d.getUTCFullYear()}${String(d.getUTCMonth() + 1).padStart(2, "0")}${String(d.getUTCDate()).padStart(2, "0")}`;
}

export async function uploadScreenshot(
  buffer: Buffer,
  label: string,
  variant: "desktop" | "desktop-annotated" | "mobile" | "side-by-side",
): Promise<string | null> {
  if (!process.env.BLOB_READ_WRITE_TOKEN) return null;
  try {
    const filename = `${BLOB_PREFIX}/${dateSuffix()}/${slugify(label)}-${variant}.png`;
    const { url } = await put(filename, buffer, {
      access: "public",
      contentType: "image/png",
    });
    return url;
  } catch {
    return null;
  }
}
