// ─── Vendor branding masker ───────────────────────────────────────────────────
// Removes or blurs vendor logos/branding from screenshots using sharp.

import sharp from "sharp";

// Regions to black-out for known vendor UIs (top-left corner typically has logo).
// Coordinates as fractions of image dimensions [x, y, w, h].
const VENDOR_MASK_REGIONS: Partial<Record<string, [number, number, number, number][]>> = {
  Perplexity: [[0, 0, 0.25, 0.07]],  // top-left logo area
  Gemini:     [[0, 0, 0.20, 0.06]],  // top-left logo area
};

export async function maskVendorBranding(
  imageBuffer: Buffer,
  engineInternalName: string,
): Promise<Buffer> {
  const regions = VENDOR_MASK_REGIONS[engineInternalName];
  if (!regions?.length) return imageBuffer;

  const img = sharp(imageBuffer);
  const meta = await img.metadata();
  const { width = 1280, height = 800 } = meta;

  const overlays = regions.map(([xFrac, yFrac, wFrac, hFrac]) => ({
    input: {
      create: {
        width: Math.round(width * wFrac),
        height: Math.round(height * hFrac),
        channels: 4 as const,
        background: { r: 30, g: 30, b: 30, alpha: 1 },
      },
    },
    left: Math.round(width * xFrac),
    top: Math.round(height * yFrac),
  }));

  return img.composite(overlays).png().toBuffer();
}
