const { cacheImageFromUrl } = require("./redis/linkPreviewImageCache.service")

function extractImageUrlsFromDescription(description, { max = 5 } = {}) {
  if (!description || typeof description !== "string") return []
  const regex = /<img[^>]+src=["']([^"']+)["'][^>]*>/gi
  const urls = []
  let match
  while ((match = regex.exec(description)) !== null) {
    if (match[1]) urls.push(match[1])
    if (urls.length >= max) break
  }
  return urls
}

function replaceDescriptionImageUrls(description, urlMap, { maxImages = 5 } = {}) {
  if (!description || typeof description !== "string") return description
  if (!urlMap || urlMap.size === 0) return description
  let seen = 0
  return description.replace(/<img[^>]+src=["']([^"']+)["'][^>]*>/gi, (full, src) => {
    seen += 1
    if (seen > maxImages) return ""
    const next = urlMap.get(src)
    if (!next) return full
    return full.replace(src, next)
  })
}

async function cacheLinkPreviewImages({ product, baseUrl } = {}) {
  if (!product || typeof product !== "object") return { product }
  const images = Array.isArray(product.images) ? product.images : []
  const description = product.description || ""
  const descImages = extractImageUrlsFromDescription(description, { max: 5 })
  const combined = [...images, ...descImages].filter(Boolean)
  const unique = Array.from(new Set(combined))

  const urlMap = new Map()
  for (const url of unique) {
    const cached = await cacheImageFromUrl(url, { ttlSeconds: 5 * 60 })
    if (cached?.key) {
      urlMap.set(url, `${baseUrl}/cache/deal_create/${cached.key}`)
    }
  }

  const nextImages = images.map((url) => urlMap.get(url) || url)
  const nextDescription = replaceDescriptionImageUrls(description, urlMap, { maxImages: 5 })

  return {
    product: {
      ...product,
      images: nextImages,
      description: nextDescription,
    },
  }
}

module.exports = {
  cacheLinkPreviewImages,
}
